pxa2xx_udc.c 67 KB

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