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. return mach->gpio_vbus_inverted ? !value : value;
  121. }
  122. if (mach->udc_is_connected)
  123. return mach->udc_is_connected();
  124. return 1;
  125. }
  126. /* one GPIO should control a D+ pullup, so host sees this device (or not) */
  127. static void pullup_off(void)
  128. {
  129. struct pxa2xx_udc_mach_info *mach = the_controller->mach;
  130. int off_level = mach->gpio_pullup_inverted;
  131. if (mach->gpio_pullup)
  132. gpio_set_value(mach->gpio_pullup, off_level);
  133. else if (mach->udc_command)
  134. mach->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
  135. }
  136. static void pullup_on(void)
  137. {
  138. struct pxa2xx_udc_mach_info *mach = the_controller->mach;
  139. int on_level = !mach->gpio_pullup_inverted;
  140. if (mach->gpio_pullup)
  141. gpio_set_value(mach->gpio_pullup, on_level);
  142. else if (mach->udc_command)
  143. mach->udc_command(PXA2XX_UDC_CMD_CONNECT);
  144. }
  145. static void pio_irq_enable(int bEndpointAddress)
  146. {
  147. bEndpointAddress &= 0xf;
  148. if (bEndpointAddress < 8)
  149. UICR0 &= ~(1 << bEndpointAddress);
  150. else {
  151. bEndpointAddress -= 8;
  152. UICR1 &= ~(1 << bEndpointAddress);
  153. }
  154. }
  155. static void pio_irq_disable(int bEndpointAddress)
  156. {
  157. bEndpointAddress &= 0xf;
  158. if (bEndpointAddress < 8)
  159. UICR0 |= 1 << bEndpointAddress;
  160. else {
  161. bEndpointAddress -= 8;
  162. UICR1 |= 1 << bEndpointAddress;
  163. }
  164. }
  165. /* The UDCCR reg contains mask and interrupt status bits,
  166. * so using '|=' isn't safe as it may ack an interrupt.
  167. */
  168. #define UDCCR_MASK_BITS (UDCCR_REM | UDCCR_SRM | UDCCR_UDE)
  169. static inline void udc_set_mask_UDCCR(int mask)
  170. {
  171. UDCCR = (UDCCR & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS);
  172. }
  173. static inline void udc_clear_mask_UDCCR(int mask)
  174. {
  175. UDCCR = (UDCCR & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS);
  176. }
  177. static inline void udc_ack_int_UDCCR(int mask)
  178. {
  179. /* udccr contains the bits we dont want to change */
  180. __u32 udccr = UDCCR & UDCCR_MASK_BITS;
  181. UDCCR = udccr | (mask & ~UDCCR_MASK_BITS);
  182. }
  183. /*
  184. * endpoint enable/disable
  185. *
  186. * we need to verify the descriptors used to enable endpoints. since pxa25x
  187. * endpoint configurations are fixed, and are pretty much always enabled,
  188. * there's not a lot to manage here.
  189. *
  190. * because pxa25x can't selectively initialize bulk (or interrupt) endpoints,
  191. * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
  192. * for a single interface (with only the default altsetting) and for gadget
  193. * drivers that don't halt endpoints (not reset by set_interface). that also
  194. * means that if you use ISO, you must violate the USB spec rule that all
  195. * iso endpoints must be in non-default altsettings.
  196. */
  197. static int pxa25x_ep_enable (struct usb_ep *_ep,
  198. const struct usb_endpoint_descriptor *desc)
  199. {
  200. struct pxa25x_ep *ep;
  201. struct pxa25x_udc *dev;
  202. ep = container_of (_ep, struct pxa25x_ep, ep);
  203. if (!_ep || !desc || ep->desc || _ep->name == ep0name
  204. || desc->bDescriptorType != USB_DT_ENDPOINT
  205. || ep->bEndpointAddress != desc->bEndpointAddress
  206. || ep->fifo_size < le16_to_cpu
  207. (desc->wMaxPacketSize)) {
  208. DMSG("%s, bad ep or descriptor\n", __func__);
  209. return -EINVAL;
  210. }
  211. /* xfer types must match, except that interrupt ~= bulk */
  212. if (ep->bmAttributes != desc->bmAttributes
  213. && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
  214. && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
  215. DMSG("%s, %s type mismatch\n", __func__, _ep->name);
  216. return -EINVAL;
  217. }
  218. /* hardware _could_ do smaller, but driver doesn't */
  219. if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
  220. && le16_to_cpu (desc->wMaxPacketSize)
  221. != BULK_FIFO_SIZE)
  222. || !desc->wMaxPacketSize) {
  223. DMSG("%s, bad %s maxpacket\n", __func__, _ep->name);
  224. return -ERANGE;
  225. }
  226. dev = ep->dev;
  227. if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
  228. DMSG("%s, bogus device state\n", __func__);
  229. return -ESHUTDOWN;
  230. }
  231. ep->desc = desc;
  232. ep->stopped = 0;
  233. ep->pio_irqs = 0;
  234. ep->ep.maxpacket = le16_to_cpu (desc->wMaxPacketSize);
  235. /* flush fifo (mostly for OUT buffers) */
  236. pxa25x_ep_fifo_flush (_ep);
  237. /* ... reset halt state too, if we could ... */
  238. DBG(DBG_VERBOSE, "enabled %s\n", _ep->name);
  239. return 0;
  240. }
  241. static int pxa25x_ep_disable (struct usb_ep *_ep)
  242. {
  243. struct pxa25x_ep *ep;
  244. unsigned long flags;
  245. ep = container_of (_ep, struct pxa25x_ep, ep);
  246. if (!_ep || !ep->desc) {
  247. DMSG("%s, %s not enabled\n", __func__,
  248. _ep ? ep->ep.name : NULL);
  249. return -EINVAL;
  250. }
  251. local_irq_save(flags);
  252. nuke (ep, -ESHUTDOWN);
  253. /* flush fifo (mostly for IN buffers) */
  254. pxa25x_ep_fifo_flush (_ep);
  255. ep->desc = NULL;
  256. ep->stopped = 1;
  257. local_irq_restore(flags);
  258. DBG(DBG_VERBOSE, "%s disabled\n", _ep->name);
  259. return 0;
  260. }
  261. /*-------------------------------------------------------------------------*/
  262. /* for the pxa25x, these can just wrap kmalloc/kfree. gadget drivers
  263. * must still pass correctly initialized endpoints, since other controller
  264. * drivers may care about how it's currently set up (dma issues etc).
  265. */
  266. /*
  267. * pxa25x_ep_alloc_request - allocate a request data structure
  268. */
  269. static struct usb_request *
  270. pxa25x_ep_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
  271. {
  272. struct pxa25x_request *req;
  273. req = kzalloc(sizeof(*req), gfp_flags);
  274. if (!req)
  275. return NULL;
  276. INIT_LIST_HEAD (&req->queue);
  277. return &req->req;
  278. }
  279. /*
  280. * pxa25x_ep_free_request - deallocate a request data structure
  281. */
  282. static void
  283. pxa25x_ep_free_request (struct usb_ep *_ep, struct usb_request *_req)
  284. {
  285. struct pxa25x_request *req;
  286. req = container_of (_req, struct pxa25x_request, req);
  287. WARN_ON(!list_empty (&req->queue));
  288. kfree(req);
  289. }
  290. /*-------------------------------------------------------------------------*/
  291. /*
  292. * done - retire a request; caller blocked irqs
  293. */
  294. static void done(struct pxa25x_ep *ep, struct pxa25x_request *req, int status)
  295. {
  296. unsigned stopped = ep->stopped;
  297. list_del_init(&req->queue);
  298. if (likely (req->req.status == -EINPROGRESS))
  299. req->req.status = status;
  300. else
  301. status = req->req.status;
  302. if (status && status != -ESHUTDOWN)
  303. DBG(DBG_VERBOSE, "complete %s req %p stat %d len %u/%u\n",
  304. ep->ep.name, &req->req, status,
  305. req->req.actual, req->req.length);
  306. /* don't modify queue heads during completion callback */
  307. ep->stopped = 1;
  308. req->req.complete(&ep->ep, &req->req);
  309. ep->stopped = stopped;
  310. }
  311. static inline void ep0_idle (struct pxa25x_udc *dev)
  312. {
  313. dev->ep0state = EP0_IDLE;
  314. }
  315. static int
  316. write_packet(volatile u32 *uddr, struct pxa25x_request *req, unsigned max)
  317. {
  318. u8 *buf;
  319. unsigned length, count;
  320. buf = req->req.buf + req->req.actual;
  321. prefetch(buf);
  322. /* how big will this packet be? */
  323. length = min(req->req.length - req->req.actual, max);
  324. req->req.actual += length;
  325. count = length;
  326. while (likely(count--))
  327. *uddr = *buf++;
  328. return length;
  329. }
  330. /*
  331. * write to an IN endpoint fifo, as many packets as possible.
  332. * irqs will use this to write the rest later.
  333. * caller guarantees at least one packet buffer is ready (or a zlp).
  334. */
  335. static int
  336. write_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
  337. {
  338. unsigned max;
  339. max = le16_to_cpu(ep->desc->wMaxPacketSize);
  340. do {
  341. unsigned count;
  342. int is_last, is_short;
  343. count = write_packet(ep->reg_uddr, req, max);
  344. /* last packet is usually short (or a zlp) */
  345. if (unlikely (count != max))
  346. is_last = is_short = 1;
  347. else {
  348. if (likely(req->req.length != req->req.actual)
  349. || req->req.zero)
  350. is_last = 0;
  351. else
  352. is_last = 1;
  353. /* interrupt/iso maxpacket may not fill the fifo */
  354. is_short = unlikely (max < ep->fifo_size);
  355. }
  356. DBG(DBG_VERY_NOISY, "wrote %s %d bytes%s%s %d left %p\n",
  357. ep->ep.name, count,
  358. is_last ? "/L" : "", is_short ? "/S" : "",
  359. req->req.length - req->req.actual, req);
  360. /* let loose that packet. maybe try writing another one,
  361. * double buffering might work. TSP, TPC, and TFS
  362. * bit values are the same for all normal IN endpoints.
  363. */
  364. *ep->reg_udccs = UDCCS_BI_TPC;
  365. if (is_short)
  366. *ep->reg_udccs = UDCCS_BI_TSP;
  367. /* requests complete when all IN data is in the FIFO */
  368. if (is_last) {
  369. done (ep, req, 0);
  370. if (list_empty(&ep->queue))
  371. pio_irq_disable (ep->bEndpointAddress);
  372. return 1;
  373. }
  374. // TODO experiment: how robust can fifo mode tweaking be?
  375. // double buffering is off in the default fifo mode, which
  376. // prevents TFS from being set here.
  377. } while (*ep->reg_udccs & UDCCS_BI_TFS);
  378. return 0;
  379. }
  380. /* caller asserts req->pending (ep0 irq status nyet cleared); starts
  381. * ep0 data stage. these chips want very simple state transitions.
  382. */
  383. static inline
  384. void ep0start(struct pxa25x_udc *dev, u32 flags, const char *tag)
  385. {
  386. UDCCS0 = flags|UDCCS0_SA|UDCCS0_OPR;
  387. USIR0 = USIR0_IR0;
  388. dev->req_pending = 0;
  389. DBG(DBG_VERY_NOISY, "%s %s, %02x/%02x\n",
  390. __func__, tag, UDCCS0, flags);
  391. }
  392. static int
  393. write_ep0_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
  394. {
  395. unsigned count;
  396. int is_short;
  397. count = write_packet(&UDDR0, req, EP0_FIFO_SIZE);
  398. ep->dev->stats.write.bytes += count;
  399. /* last packet "must be" short (or a zlp) */
  400. is_short = (count != EP0_FIFO_SIZE);
  401. DBG(DBG_VERY_NOISY, "ep0in %d bytes %d left %p\n", count,
  402. req->req.length - req->req.actual, req);
  403. if (unlikely (is_short)) {
  404. if (ep->dev->req_pending)
  405. ep0start(ep->dev, UDCCS0_IPR, "short IN");
  406. else
  407. UDCCS0 = UDCCS0_IPR;
  408. count = req->req.length;
  409. done (ep, req, 0);
  410. ep0_idle(ep->dev);
  411. #ifndef CONFIG_ARCH_IXP4XX
  412. #if 1
  413. /* This seems to get rid of lost status irqs in some cases:
  414. * host responds quickly, or next request involves config
  415. * change automagic, or should have been hidden, or ...
  416. *
  417. * FIXME get rid of all udelays possible...
  418. */
  419. if (count >= EP0_FIFO_SIZE) {
  420. count = 100;
  421. do {
  422. if ((UDCCS0 & UDCCS0_OPR) != 0) {
  423. /* clear OPR, generate ack */
  424. UDCCS0 = UDCCS0_OPR;
  425. break;
  426. }
  427. count--;
  428. udelay(1);
  429. } while (count);
  430. }
  431. #endif
  432. #endif
  433. } else if (ep->dev->req_pending)
  434. ep0start(ep->dev, 0, "IN");
  435. return is_short;
  436. }
  437. /*
  438. * read_fifo - unload packet(s) from the fifo we use for usb OUT
  439. * transfers and put them into the request. caller should have made
  440. * sure there's at least one packet ready.
  441. *
  442. * returns true if the request completed because of short packet or the
  443. * request buffer having filled (and maybe overran till end-of-packet).
  444. */
  445. static int
  446. read_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
  447. {
  448. for (;;) {
  449. u32 udccs;
  450. u8 *buf;
  451. unsigned bufferspace, count, is_short;
  452. /* make sure there's a packet in the FIFO.
  453. * UDCCS_{BO,IO}_RPC are all the same bit value.
  454. * UDCCS_{BO,IO}_RNE are all the same bit value.
  455. */
  456. udccs = *ep->reg_udccs;
  457. if (unlikely ((udccs & UDCCS_BO_RPC) == 0))
  458. break;
  459. buf = req->req.buf + req->req.actual;
  460. prefetchw(buf);
  461. bufferspace = req->req.length - req->req.actual;
  462. /* read all bytes from this packet */
  463. if (likely (udccs & UDCCS_BO_RNE)) {
  464. count = 1 + (0x0ff & *ep->reg_ubcr);
  465. req->req.actual += min (count, bufferspace);
  466. } else /* zlp */
  467. count = 0;
  468. is_short = (count < ep->ep.maxpacket);
  469. DBG(DBG_VERY_NOISY, "read %s %02x, %d bytes%s req %p %d/%d\n",
  470. ep->ep.name, udccs, count,
  471. is_short ? "/S" : "",
  472. req, req->req.actual, req->req.length);
  473. while (likely (count-- != 0)) {
  474. u8 byte = (u8) *ep->reg_uddr;
  475. if (unlikely (bufferspace == 0)) {
  476. /* this happens when the driver's buffer
  477. * is smaller than what the host sent.
  478. * discard the extra data.
  479. */
  480. if (req->req.status != -EOVERFLOW)
  481. DMSG("%s overflow %d\n",
  482. ep->ep.name, count);
  483. req->req.status = -EOVERFLOW;
  484. } else {
  485. *buf++ = byte;
  486. bufferspace--;
  487. }
  488. }
  489. *ep->reg_udccs = UDCCS_BO_RPC;
  490. /* RPC/RSP/RNE could now reflect the other packet buffer */
  491. /* iso is one request per packet */
  492. if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
  493. if (udccs & UDCCS_IO_ROF)
  494. req->req.status = -EHOSTUNREACH;
  495. /* more like "is_done" */
  496. is_short = 1;
  497. }
  498. /* completion */
  499. if (is_short || req->req.actual == req->req.length) {
  500. done (ep, req, 0);
  501. if (list_empty(&ep->queue))
  502. pio_irq_disable (ep->bEndpointAddress);
  503. return 1;
  504. }
  505. /* finished that packet. the next one may be waiting... */
  506. }
  507. return 0;
  508. }
  509. /*
  510. * special ep0 version of the above. no UBCR0 or double buffering; status
  511. * handshaking is magic. most device protocols don't need control-OUT.
  512. * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
  513. * protocols do use them.
  514. */
  515. static int
  516. read_ep0_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
  517. {
  518. u8 *buf, byte;
  519. unsigned bufferspace;
  520. buf = req->req.buf + req->req.actual;
  521. bufferspace = req->req.length - req->req.actual;
  522. while (UDCCS0 & UDCCS0_RNE) {
  523. byte = (u8) UDDR0;
  524. if (unlikely (bufferspace == 0)) {
  525. /* this happens when the driver's buffer
  526. * is smaller than what the host sent.
  527. * discard the extra data.
  528. */
  529. if (req->req.status != -EOVERFLOW)
  530. DMSG("%s overflow\n", ep->ep.name);
  531. req->req.status = -EOVERFLOW;
  532. } else {
  533. *buf++ = byte;
  534. req->req.actual++;
  535. bufferspace--;
  536. }
  537. }
  538. UDCCS0 = UDCCS0_OPR | UDCCS0_IPR;
  539. /* completion */
  540. if (req->req.actual >= req->req.length)
  541. return 1;
  542. /* finished that packet. the next one may be waiting... */
  543. return 0;
  544. }
  545. /*-------------------------------------------------------------------------*/
  546. static int
  547. pxa25x_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
  548. {
  549. struct pxa25x_request *req;
  550. struct pxa25x_ep *ep;
  551. struct pxa25x_udc *dev;
  552. unsigned long flags;
  553. req = container_of(_req, struct pxa25x_request, req);
  554. if (unlikely (!_req || !_req->complete || !_req->buf
  555. || !list_empty(&req->queue))) {
  556. DMSG("%s, bad params\n", __func__);
  557. return -EINVAL;
  558. }
  559. ep = container_of(_ep, struct pxa25x_ep, ep);
  560. if (unlikely (!_ep || (!ep->desc && ep->ep.name != ep0name))) {
  561. DMSG("%s, bad ep\n", __func__);
  562. return -EINVAL;
  563. }
  564. dev = ep->dev;
  565. if (unlikely (!dev->driver
  566. || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
  567. DMSG("%s, bogus device state\n", __func__);
  568. return -ESHUTDOWN;
  569. }
  570. /* iso is always one packet per request, that's the only way
  571. * we can report per-packet status. that also helps with dma.
  572. */
  573. if (unlikely (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
  574. && req->req.length > le16_to_cpu
  575. (ep->desc->wMaxPacketSize)))
  576. return -EMSGSIZE;
  577. DBG(DBG_NOISY, "%s queue req %p, len %d buf %p\n",
  578. _ep->name, _req, _req->length, _req->buf);
  579. local_irq_save(flags);
  580. _req->status = -EINPROGRESS;
  581. _req->actual = 0;
  582. /* kickstart this i/o queue? */
  583. if (list_empty(&ep->queue) && !ep->stopped) {
  584. if (ep->desc == NULL/* ep0 */) {
  585. unsigned length = _req->length;
  586. switch (dev->ep0state) {
  587. case EP0_IN_DATA_PHASE:
  588. dev->stats.write.ops++;
  589. if (write_ep0_fifo(ep, req))
  590. req = NULL;
  591. break;
  592. case EP0_OUT_DATA_PHASE:
  593. dev->stats.read.ops++;
  594. /* messy ... */
  595. if (dev->req_config) {
  596. DBG(DBG_VERBOSE, "ep0 config ack%s\n",
  597. dev->has_cfr ? "" : " raced");
  598. if (dev->has_cfr)
  599. UDCCFR = UDCCFR_AREN|UDCCFR_ACM
  600. |UDCCFR_MB1;
  601. done(ep, req, 0);
  602. dev->ep0state = EP0_END_XFER;
  603. local_irq_restore (flags);
  604. return 0;
  605. }
  606. if (dev->req_pending)
  607. ep0start(dev, UDCCS0_IPR, "OUT");
  608. if (length == 0 || ((UDCCS0 & UDCCS0_RNE) != 0
  609. && read_ep0_fifo(ep, req))) {
  610. ep0_idle(dev);
  611. done(ep, req, 0);
  612. req = NULL;
  613. }
  614. break;
  615. default:
  616. DMSG("ep0 i/o, odd state %d\n", dev->ep0state);
  617. local_irq_restore (flags);
  618. return -EL2HLT;
  619. }
  620. /* can the FIFO can satisfy the request immediately? */
  621. } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
  622. if ((*ep->reg_udccs & UDCCS_BI_TFS) != 0
  623. && write_fifo(ep, req))
  624. req = NULL;
  625. } else if ((*ep->reg_udccs & UDCCS_BO_RFS) != 0
  626. && read_fifo(ep, req)) {
  627. req = NULL;
  628. }
  629. if (likely (req && ep->desc))
  630. pio_irq_enable(ep->bEndpointAddress);
  631. }
  632. /* pio or dma irq handler advances the queue. */
  633. if (likely(req != NULL))
  634. list_add_tail(&req->queue, &ep->queue);
  635. local_irq_restore(flags);
  636. return 0;
  637. }
  638. /*
  639. * nuke - dequeue ALL requests
  640. */
  641. static void nuke(struct pxa25x_ep *ep, int status)
  642. {
  643. struct pxa25x_request *req;
  644. /* called with irqs blocked */
  645. while (!list_empty(&ep->queue)) {
  646. req = list_entry(ep->queue.next,
  647. struct pxa25x_request,
  648. queue);
  649. done(ep, req, status);
  650. }
  651. if (ep->desc)
  652. pio_irq_disable (ep->bEndpointAddress);
  653. }
  654. /* dequeue JUST ONE request */
  655. static int pxa25x_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  656. {
  657. struct pxa25x_ep *ep;
  658. struct pxa25x_request *req;
  659. unsigned long flags;
  660. ep = container_of(_ep, struct pxa25x_ep, ep);
  661. if (!_ep || ep->ep.name == ep0name)
  662. return -EINVAL;
  663. local_irq_save(flags);
  664. /* make sure it's actually queued on this endpoint */
  665. list_for_each_entry (req, &ep->queue, queue) {
  666. if (&req->req == _req)
  667. break;
  668. }
  669. if (&req->req != _req) {
  670. local_irq_restore(flags);
  671. return -EINVAL;
  672. }
  673. done(ep, req, -ECONNRESET);
  674. local_irq_restore(flags);
  675. return 0;
  676. }
  677. /*-------------------------------------------------------------------------*/
  678. static int pxa25x_ep_set_halt(struct usb_ep *_ep, int value)
  679. {
  680. struct pxa25x_ep *ep;
  681. unsigned long flags;
  682. ep = container_of(_ep, struct pxa25x_ep, ep);
  683. if (unlikely (!_ep
  684. || (!ep->desc && ep->ep.name != ep0name))
  685. || ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
  686. DMSG("%s, bad ep\n", __func__);
  687. return -EINVAL;
  688. }
  689. if (value == 0) {
  690. /* this path (reset toggle+halt) is needed to implement
  691. * SET_INTERFACE on normal hardware. but it can't be
  692. * done from software on the PXA UDC, and the hardware
  693. * forgets to do it as part of SET_INTERFACE automagic.
  694. */
  695. DMSG("only host can clear %s halt\n", _ep->name);
  696. return -EROFS;
  697. }
  698. local_irq_save(flags);
  699. if ((ep->bEndpointAddress & USB_DIR_IN) != 0
  700. && ((*ep->reg_udccs & UDCCS_BI_TFS) == 0
  701. || !list_empty(&ep->queue))) {
  702. local_irq_restore(flags);
  703. return -EAGAIN;
  704. }
  705. /* FST bit is the same for control, bulk in, bulk out, interrupt in */
  706. *ep->reg_udccs = UDCCS_BI_FST|UDCCS_BI_FTF;
  707. /* ep0 needs special care */
  708. if (!ep->desc) {
  709. start_watchdog(ep->dev);
  710. ep->dev->req_pending = 0;
  711. ep->dev->ep0state = EP0_STALL;
  712. /* and bulk/intr endpoints like dropping stalls too */
  713. } else {
  714. unsigned i;
  715. for (i = 0; i < 1000; i += 20) {
  716. if (*ep->reg_udccs & UDCCS_BI_SST)
  717. break;
  718. udelay(20);
  719. }
  720. }
  721. local_irq_restore(flags);
  722. DBG(DBG_VERBOSE, "%s halt\n", _ep->name);
  723. return 0;
  724. }
  725. static int pxa25x_ep_fifo_status(struct usb_ep *_ep)
  726. {
  727. struct pxa25x_ep *ep;
  728. ep = container_of(_ep, struct pxa25x_ep, ep);
  729. if (!_ep) {
  730. DMSG("%s, bad ep\n", __func__);
  731. return -ENODEV;
  732. }
  733. /* pxa can't report unclaimed bytes from IN fifos */
  734. if ((ep->bEndpointAddress & USB_DIR_IN) != 0)
  735. return -EOPNOTSUPP;
  736. if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN
  737. || (*ep->reg_udccs & UDCCS_BO_RFS) == 0)
  738. return 0;
  739. else
  740. return (*ep->reg_ubcr & 0xfff) + 1;
  741. }
  742. static void pxa25x_ep_fifo_flush(struct usb_ep *_ep)
  743. {
  744. struct pxa25x_ep *ep;
  745. ep = container_of(_ep, struct pxa25x_ep, ep);
  746. if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) {
  747. DMSG("%s, bad ep\n", __func__);
  748. return;
  749. }
  750. /* toggle and halt bits stay unchanged */
  751. /* for OUT, just read and discard the FIFO contents. */
  752. if ((ep->bEndpointAddress & USB_DIR_IN) == 0) {
  753. while (((*ep->reg_udccs) & UDCCS_BO_RNE) != 0)
  754. (void) *ep->reg_uddr;
  755. return;
  756. }
  757. /* most IN status is the same, but ISO can't stall */
  758. *ep->reg_udccs = UDCCS_BI_TPC|UDCCS_BI_FTF|UDCCS_BI_TUR
  759. | (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
  760. ? 0 : UDCCS_BI_SST;
  761. }
  762. static struct usb_ep_ops pxa25x_ep_ops = {
  763. .enable = pxa25x_ep_enable,
  764. .disable = pxa25x_ep_disable,
  765. .alloc_request = pxa25x_ep_alloc_request,
  766. .free_request = pxa25x_ep_free_request,
  767. .queue = pxa25x_ep_queue,
  768. .dequeue = pxa25x_ep_dequeue,
  769. .set_halt = pxa25x_ep_set_halt,
  770. .fifo_status = pxa25x_ep_fifo_status,
  771. .fifo_flush = pxa25x_ep_fifo_flush,
  772. };
  773. /* ---------------------------------------------------------------------------
  774. * device-scoped parts of the api to the usb controller hardware
  775. * ---------------------------------------------------------------------------
  776. */
  777. static int pxa25x_udc_get_frame(struct usb_gadget *_gadget)
  778. {
  779. return ((UFNRH & 0x07) << 8) | (UFNRL & 0xff);
  780. }
  781. static int pxa25x_udc_wakeup(struct usb_gadget *_gadget)
  782. {
  783. /* host may not have enabled remote wakeup */
  784. if ((UDCCS0 & UDCCS0_DRWF) == 0)
  785. return -EHOSTUNREACH;
  786. udc_set_mask_UDCCR(UDCCR_RSM);
  787. return 0;
  788. }
  789. static void stop_activity(struct pxa25x_udc *, struct usb_gadget_driver *);
  790. static void udc_enable (struct pxa25x_udc *);
  791. static void udc_disable(struct pxa25x_udc *);
  792. /* We disable the UDC -- and its 48 MHz clock -- whenever it's not
  793. * in active use.
  794. */
  795. static int pullup(struct pxa25x_udc *udc)
  796. {
  797. int is_active = udc->vbus && udc->pullup && !udc->suspended;
  798. DMSG("%s\n", is_active ? "active" : "inactive");
  799. if (is_active) {
  800. if (!udc->active) {
  801. udc->active = 1;
  802. /* Enable clock for USB device */
  803. clk_enable(udc->clk);
  804. udc_enable(udc);
  805. }
  806. } else {
  807. if (udc->active) {
  808. if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
  809. DMSG("disconnect %s\n", udc->driver
  810. ? udc->driver->driver.name
  811. : "(no driver)");
  812. stop_activity(udc, udc->driver);
  813. }
  814. udc_disable(udc);
  815. /* Disable clock for USB device */
  816. clk_disable(udc->clk);
  817. udc->active = 0;
  818. }
  819. }
  820. return 0;
  821. }
  822. /* VBUS reporting logically comes from a transceiver */
  823. static int pxa25x_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
  824. {
  825. struct pxa25x_udc *udc;
  826. udc = container_of(_gadget, struct pxa25x_udc, gadget);
  827. udc->vbus = (is_active != 0);
  828. DMSG("vbus %s\n", is_active ? "supplied" : "inactive");
  829. pullup(udc);
  830. return 0;
  831. }
  832. /* drivers may have software control over D+ pullup */
  833. static int pxa25x_udc_pullup(struct usb_gadget *_gadget, int is_active)
  834. {
  835. struct pxa25x_udc *udc;
  836. udc = container_of(_gadget, struct pxa25x_udc, gadget);
  837. /* not all boards support pullup control */
  838. if (!udc->mach->gpio_pullup && !udc->mach->udc_command)
  839. return -EOPNOTSUPP;
  840. udc->pullup = (is_active != 0);
  841. pullup(udc);
  842. return 0;
  843. }
  844. static const struct usb_gadget_ops pxa25x_udc_ops = {
  845. .get_frame = pxa25x_udc_get_frame,
  846. .wakeup = pxa25x_udc_wakeup,
  847. .vbus_session = pxa25x_udc_vbus_session,
  848. .pullup = pxa25x_udc_pullup,
  849. // .vbus_draw ... boards may consume current from VBUS, up to
  850. // 100-500mA based on config. the 500uA suspend ceiling means
  851. // that exclusively vbus-powered PXA designs violate USB specs.
  852. };
  853. /*-------------------------------------------------------------------------*/
  854. #ifdef CONFIG_USB_GADGET_DEBUG_FS
  855. static int
  856. udc_seq_show(struct seq_file *m, void *_d)
  857. {
  858. struct pxa25x_udc *dev = m->private;
  859. unsigned long flags;
  860. int i;
  861. u32 tmp;
  862. local_irq_save(flags);
  863. /* basic device status */
  864. seq_printf(m, DRIVER_DESC "\n"
  865. "%s version: %s\nGadget driver: %s\nHost %s\n\n",
  866. driver_name, DRIVER_VERSION SIZE_STR "(pio)",
  867. dev->driver ? dev->driver->driver.name : "(none)",
  868. is_vbus_present() ? "full speed" : "disconnected");
  869. /* registers for device and ep0 */
  870. seq_printf(m,
  871. "uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
  872. UICR1, UICR0, USIR1, USIR0, UFNRH, UFNRL);
  873. tmp = UDCCR;
  874. seq_printf(m,
  875. "udccr %02X =%s%s%s%s%s%s%s%s\n", tmp,
  876. (tmp & UDCCR_REM) ? " rem" : "",
  877. (tmp & UDCCR_RSTIR) ? " rstir" : "",
  878. (tmp & UDCCR_SRM) ? " srm" : "",
  879. (tmp & UDCCR_SUSIR) ? " susir" : "",
  880. (tmp & UDCCR_RESIR) ? " resir" : "",
  881. (tmp & UDCCR_RSM) ? " rsm" : "",
  882. (tmp & UDCCR_UDA) ? " uda" : "",
  883. (tmp & UDCCR_UDE) ? " ude" : "");
  884. tmp = UDCCS0;
  885. seq_printf(m,
  886. "udccs0 %02X =%s%s%s%s%s%s%s%s\n", tmp,
  887. (tmp & UDCCS0_SA) ? " sa" : "",
  888. (tmp & UDCCS0_RNE) ? " rne" : "",
  889. (tmp & UDCCS0_FST) ? " fst" : "",
  890. (tmp & UDCCS0_SST) ? " sst" : "",
  891. (tmp & UDCCS0_DRWF) ? " dwrf" : "",
  892. (tmp & UDCCS0_FTF) ? " ftf" : "",
  893. (tmp & UDCCS0_IPR) ? " ipr" : "",
  894. (tmp & UDCCS0_OPR) ? " opr" : "");
  895. if (dev->has_cfr) {
  896. tmp = UDCCFR;
  897. seq_printf(m,
  898. "udccfr %02X =%s%s\n", tmp,
  899. (tmp & UDCCFR_AREN) ? " aren" : "",
  900. (tmp & UDCCFR_ACM) ? " acm" : "");
  901. }
  902. if (!is_vbus_present() || !dev->driver)
  903. goto done;
  904. seq_printf(m, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n",
  905. dev->stats.write.bytes, dev->stats.write.ops,
  906. dev->stats.read.bytes, dev->stats.read.ops,
  907. dev->stats.irqs);
  908. /* dump endpoint queues */
  909. for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
  910. struct pxa25x_ep *ep = &dev->ep [i];
  911. struct pxa25x_request *req;
  912. if (i != 0) {
  913. const struct usb_endpoint_descriptor *desc;
  914. desc = ep->desc;
  915. if (!desc)
  916. continue;
  917. tmp = *dev->ep [i].reg_udccs;
  918. seq_printf(m,
  919. "%s max %d %s udccs %02x irqs %lu\n",
  920. ep->ep.name, le16_to_cpu(desc->wMaxPacketSize),
  921. "pio", tmp, ep->pio_irqs);
  922. /* TODO translate all five groups of udccs bits! */
  923. } else /* ep0 should only have one transfer queued */
  924. seq_printf(m, "ep0 max 16 pio irqs %lu\n",
  925. ep->pio_irqs);
  926. if (list_empty(&ep->queue)) {
  927. seq_printf(m, "\t(nothing queued)\n");
  928. continue;
  929. }
  930. list_for_each_entry(req, &ep->queue, queue) {
  931. seq_printf(m,
  932. "\treq %p len %d/%d buf %p\n",
  933. &req->req, req->req.actual,
  934. req->req.length, req->req.buf);
  935. }
  936. }
  937. done:
  938. local_irq_restore(flags);
  939. return 0;
  940. }
  941. static int
  942. udc_debugfs_open(struct inode *inode, struct file *file)
  943. {
  944. return single_open(file, udc_seq_show, inode->i_private);
  945. }
  946. static const struct file_operations debug_fops = {
  947. .open = udc_debugfs_open,
  948. .read = seq_read,
  949. .llseek = seq_lseek,
  950. .release = single_release,
  951. .owner = THIS_MODULE,
  952. };
  953. #define create_debug_files(dev) \
  954. do { \
  955. dev->debugfs_udc = debugfs_create_file(dev->gadget.name, \
  956. S_IRUGO, NULL, dev, &debug_fops); \
  957. } while (0)
  958. #define remove_debug_files(dev) \
  959. do { \
  960. if (dev->debugfs_udc) \
  961. debugfs_remove(dev->debugfs_udc); \
  962. } while (0)
  963. #else /* !CONFIG_USB_GADGET_DEBUG_FILES */
  964. #define create_debug_files(dev) do {} while (0)
  965. #define remove_debug_files(dev) do {} while (0)
  966. #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
  967. /*-------------------------------------------------------------------------*/
  968. /*
  969. * udc_disable - disable USB device controller
  970. */
  971. static void udc_disable(struct pxa25x_udc *dev)
  972. {
  973. /* block all irqs */
  974. udc_set_mask_UDCCR(UDCCR_SRM|UDCCR_REM);
  975. UICR0 = UICR1 = 0xff;
  976. UFNRH = UFNRH_SIM;
  977. /* if hardware supports it, disconnect from usb */
  978. pullup_off();
  979. udc_clear_mask_UDCCR(UDCCR_UDE);
  980. ep0_idle (dev);
  981. dev->gadget.speed = USB_SPEED_UNKNOWN;
  982. }
  983. /*
  984. * udc_reinit - initialize software state
  985. */
  986. static void udc_reinit(struct pxa25x_udc *dev)
  987. {
  988. u32 i;
  989. /* device/ep0 records init */
  990. INIT_LIST_HEAD (&dev->gadget.ep_list);
  991. INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
  992. dev->ep0state = EP0_IDLE;
  993. /* basic endpoint records init */
  994. for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
  995. struct pxa25x_ep *ep = &dev->ep[i];
  996. if (i != 0)
  997. list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
  998. ep->desc = NULL;
  999. ep->stopped = 0;
  1000. INIT_LIST_HEAD (&ep->queue);
  1001. ep->pio_irqs = 0;
  1002. }
  1003. /* the rest was statically initialized, and is read-only */
  1004. }
  1005. /* until it's enabled, this UDC should be completely invisible
  1006. * to any USB host.
  1007. */
  1008. static void udc_enable (struct pxa25x_udc *dev)
  1009. {
  1010. udc_clear_mask_UDCCR(UDCCR_UDE);
  1011. /* try to clear these bits before we enable the udc */
  1012. udc_ack_int_UDCCR(UDCCR_SUSIR|/*UDCCR_RSTIR|*/UDCCR_RESIR);
  1013. ep0_idle(dev);
  1014. dev->gadget.speed = USB_SPEED_UNKNOWN;
  1015. dev->stats.irqs = 0;
  1016. /*
  1017. * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
  1018. * - enable UDC
  1019. * - if RESET is already in progress, ack interrupt
  1020. * - unmask reset interrupt
  1021. */
  1022. udc_set_mask_UDCCR(UDCCR_UDE);
  1023. if (!(UDCCR & UDCCR_UDA))
  1024. udc_ack_int_UDCCR(UDCCR_RSTIR);
  1025. if (dev->has_cfr /* UDC_RES2 is defined */) {
  1026. /* pxa255 (a0+) can avoid a set_config race that could
  1027. * prevent gadget drivers from configuring correctly
  1028. */
  1029. UDCCFR = UDCCFR_ACM | UDCCFR_MB1;
  1030. } else {
  1031. /* "USB test mode" for pxa250 errata 40-42 (stepping a0, a1)
  1032. * which could result in missing packets and interrupts.
  1033. * supposedly one bit per endpoint, controlling whether it
  1034. * double buffers or not; ACM/AREN bits fit into the holes.
  1035. * zero bits (like USIR0_IRx) disable double buffering.
  1036. */
  1037. UDC_RES1 = 0x00;
  1038. UDC_RES2 = 0x00;
  1039. }
  1040. /* enable suspend/resume and reset irqs */
  1041. udc_clear_mask_UDCCR(UDCCR_SRM | UDCCR_REM);
  1042. /* enable ep0 irqs */
  1043. UICR0 &= ~UICR0_IM0;
  1044. /* if hardware supports it, pullup D+ and wait for reset */
  1045. pullup_on();
  1046. }
  1047. /* when a driver is successfully registered, it will receive
  1048. * control requests including set_configuration(), which enables
  1049. * non-control requests. then usb traffic follows until a
  1050. * disconnect is reported. then a host may connect again, or
  1051. * the driver might get unbound.
  1052. */
  1053. int usb_gadget_register_driver(struct usb_gadget_driver *driver)
  1054. {
  1055. struct pxa25x_udc *dev = the_controller;
  1056. int retval;
  1057. if (!driver
  1058. || driver->speed < USB_SPEED_FULL
  1059. || !driver->bind
  1060. || !driver->disconnect
  1061. || !driver->setup)
  1062. return -EINVAL;
  1063. if (!dev)
  1064. return -ENODEV;
  1065. if (dev->driver)
  1066. return -EBUSY;
  1067. /* first hook up the driver ... */
  1068. dev->driver = driver;
  1069. dev->gadget.dev.driver = &driver->driver;
  1070. dev->pullup = 1;
  1071. retval = device_add (&dev->gadget.dev);
  1072. if (retval) {
  1073. fail:
  1074. dev->driver = NULL;
  1075. dev->gadget.dev.driver = NULL;
  1076. return retval;
  1077. }
  1078. retval = driver->bind(&dev->gadget);
  1079. if (retval) {
  1080. DMSG("bind to driver %s --> error %d\n",
  1081. driver->driver.name, retval);
  1082. device_del (&dev->gadget.dev);
  1083. goto fail;
  1084. }
  1085. /* ... then enable host detection and ep0; and we're ready
  1086. * for set_configuration as well as eventual disconnect.
  1087. */
  1088. DMSG("registered gadget driver '%s'\n", driver->driver.name);
  1089. pullup(dev);
  1090. dump_state(dev);
  1091. return 0;
  1092. }
  1093. EXPORT_SYMBOL(usb_gadget_register_driver);
  1094. static void
  1095. stop_activity(struct pxa25x_udc *dev, struct usb_gadget_driver *driver)
  1096. {
  1097. int i;
  1098. /* don't disconnect drivers more than once */
  1099. if (dev->gadget.speed == USB_SPEED_UNKNOWN)
  1100. driver = NULL;
  1101. dev->gadget.speed = USB_SPEED_UNKNOWN;
  1102. /* prevent new request submissions, kill any outstanding requests */
  1103. for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
  1104. struct pxa25x_ep *ep = &dev->ep[i];
  1105. ep->stopped = 1;
  1106. nuke(ep, -ESHUTDOWN);
  1107. }
  1108. del_timer_sync(&dev->timer);
  1109. /* report disconnect; the driver is already quiesced */
  1110. if (driver)
  1111. driver->disconnect(&dev->gadget);
  1112. /* re-init driver-visible data structures */
  1113. udc_reinit(dev);
  1114. }
  1115. int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  1116. {
  1117. struct pxa25x_udc *dev = the_controller;
  1118. if (!dev)
  1119. return -ENODEV;
  1120. if (!driver || driver != dev->driver || !driver->unbind)
  1121. return -EINVAL;
  1122. local_irq_disable();
  1123. dev->pullup = 0;
  1124. pullup(dev);
  1125. stop_activity(dev, driver);
  1126. local_irq_enable();
  1127. driver->unbind(&dev->gadget);
  1128. dev->gadget.dev.driver = NULL;
  1129. dev->driver = NULL;
  1130. device_del (&dev->gadget.dev);
  1131. DMSG("unregistered gadget driver '%s'\n", driver->driver.name);
  1132. dump_state(dev);
  1133. return 0;
  1134. }
  1135. EXPORT_SYMBOL(usb_gadget_unregister_driver);
  1136. /*-------------------------------------------------------------------------*/
  1137. #ifdef CONFIG_ARCH_LUBBOCK
  1138. /* Lubbock has separate connect and disconnect irqs. More typical designs
  1139. * use one GPIO as the VBUS IRQ, and another to control the D+ pullup.
  1140. */
  1141. static irqreturn_t
  1142. lubbock_vbus_irq(int irq, void *_dev)
  1143. {
  1144. struct pxa25x_udc *dev = _dev;
  1145. int vbus;
  1146. dev->stats.irqs++;
  1147. switch (irq) {
  1148. case LUBBOCK_USB_IRQ:
  1149. vbus = 1;
  1150. disable_irq(LUBBOCK_USB_IRQ);
  1151. enable_irq(LUBBOCK_USB_DISC_IRQ);
  1152. break;
  1153. case LUBBOCK_USB_DISC_IRQ:
  1154. vbus = 0;
  1155. disable_irq(LUBBOCK_USB_DISC_IRQ);
  1156. enable_irq(LUBBOCK_USB_IRQ);
  1157. break;
  1158. default:
  1159. return IRQ_NONE;
  1160. }
  1161. pxa25x_udc_vbus_session(&dev->gadget, vbus);
  1162. return IRQ_HANDLED;
  1163. }
  1164. #endif
  1165. static irqreturn_t udc_vbus_irq(int irq, void *_dev)
  1166. {
  1167. struct pxa25x_udc *dev = _dev;
  1168. int vbus = gpio_get_value(dev->mach->gpio_vbus);
  1169. if (dev->mach->gpio_vbus_inverted)
  1170. vbus = !vbus;
  1171. pxa25x_udc_vbus_session(&dev->gadget, vbus);
  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. .bus_id = "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, "UDCCLK");
  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");