net2280.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897
  1. /*
  2. * Driver for the PLX NET2280 USB device controller.
  3. * Specs and errata are available from <http://www.plxtech.com>.
  4. *
  5. * PLX Technology Inc. (formerly NetChip Technology) supported the
  6. * development of this driver.
  7. *
  8. *
  9. * CODE STATUS HIGHLIGHTS
  10. *
  11. * This driver should work well with most "gadget" drivers, including
  12. * the File Storage, Serial, and Ethernet/RNDIS gadget drivers
  13. * as well as Gadget Zero and Gadgetfs.
  14. *
  15. * DMA is enabled by default. Drivers using transfer queues might use
  16. * DMA chaining to remove IRQ latencies between transfers. (Except when
  17. * short OUT transfers happen.) Drivers can use the req->no_interrupt
  18. * hint to completely eliminate some IRQs, if a later IRQ is guaranteed
  19. * and DMA chaining is enabled.
  20. *
  21. * Note that almost all the errata workarounds here are only needed for
  22. * rev1 chips. Rev1a silicon (0110) fixes almost all of them.
  23. */
  24. /*
  25. * Copyright (C) 2003 David Brownell
  26. * Copyright (C) 2003-2005 PLX Technology, Inc.
  27. *
  28. * Modified Seth Levy 2005 PLX Technology, Inc. to provide compatibility
  29. * with 2282 chip
  30. *
  31. * This program is free software; you can redistribute it and/or modify
  32. * it under the terms of the GNU General Public License as published by
  33. * the Free Software Foundation; either version 2 of the License, or
  34. * (at your option) any later version.
  35. */
  36. #undef DEBUG /* messages on error and most fault paths */
  37. #undef VERBOSE /* extra debug messages (success too) */
  38. #include <linux/module.h>
  39. #include <linux/pci.h>
  40. #include <linux/dma-mapping.h>
  41. #include <linux/kernel.h>
  42. #include <linux/delay.h>
  43. #include <linux/ioport.h>
  44. #include <linux/slab.h>
  45. #include <linux/errno.h>
  46. #include <linux/init.h>
  47. #include <linux/timer.h>
  48. #include <linux/list.h>
  49. #include <linux/interrupt.h>
  50. #include <linux/moduleparam.h>
  51. #include <linux/device.h>
  52. #include <linux/usb/ch9.h>
  53. #include <linux/usb/gadget.h>
  54. #include <linux/prefetch.h>
  55. #include <asm/byteorder.h>
  56. #include <asm/io.h>
  57. #include <asm/irq.h>
  58. #include <asm/system.h>
  59. #include <asm/unaligned.h>
  60. #define DRIVER_DESC "PLX NET228x USB Peripheral Controller"
  61. #define DRIVER_VERSION "2005 Sept 27"
  62. #define DMA_ADDR_INVALID (~(dma_addr_t)0)
  63. #define EP_DONTUSE 13 /* nonzero */
  64. #define USE_RDK_LEDS /* GPIO pins control three LEDs */
  65. static const char driver_name [] = "net2280";
  66. static const char driver_desc [] = DRIVER_DESC;
  67. static const char ep0name [] = "ep0";
  68. static const char *const ep_name [] = {
  69. ep0name,
  70. "ep-a", "ep-b", "ep-c", "ep-d",
  71. "ep-e", "ep-f",
  72. };
  73. /* use_dma -- general goodness, fewer interrupts, less cpu load (vs PIO)
  74. * use_dma_chaining -- dma descriptor queueing gives even more irq reduction
  75. *
  76. * The net2280 DMA engines are not tightly integrated with their FIFOs;
  77. * not all cases are (yet) handled well in this driver or the silicon.
  78. * Some gadget drivers work better with the dma support here than others.
  79. * These two parameters let you use PIO or more aggressive DMA.
  80. */
  81. static bool use_dma = 1;
  82. static bool use_dma_chaining = 0;
  83. /* "modprobe net2280 use_dma=n" etc */
  84. module_param (use_dma, bool, S_IRUGO);
  85. module_param (use_dma_chaining, bool, S_IRUGO);
  86. /* mode 0 == ep-{a,b,c,d} 1K fifo each
  87. * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
  88. * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
  89. */
  90. static ushort fifo_mode = 0;
  91. /* "modprobe net2280 fifo_mode=1" etc */
  92. module_param (fifo_mode, ushort, 0644);
  93. /* enable_suspend -- When enabled, the driver will respond to
  94. * USB suspend requests by powering down the NET2280. Otherwise,
  95. * USB suspend requests will be ignored. This is acceptable for
  96. * self-powered devices
  97. */
  98. static bool enable_suspend = 0;
  99. /* "modprobe net2280 enable_suspend=1" etc */
  100. module_param (enable_suspend, bool, S_IRUGO);
  101. #define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
  102. #if defined(CONFIG_USB_GADGET_DEBUG_FILES) || defined (DEBUG)
  103. static char *type_string (u8 bmAttributes)
  104. {
  105. switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
  106. case USB_ENDPOINT_XFER_BULK: return "bulk";
  107. case USB_ENDPOINT_XFER_ISOC: return "iso";
  108. case USB_ENDPOINT_XFER_INT: return "intr";
  109. };
  110. return "control";
  111. }
  112. #endif
  113. #include "net2280.h"
  114. #define valid_bit cpu_to_le32 (1 << VALID_BIT)
  115. #define dma_done_ie cpu_to_le32 (1 << DMA_DONE_INTERRUPT_ENABLE)
  116. /*-------------------------------------------------------------------------*/
  117. static int
  118. net2280_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
  119. {
  120. struct net2280 *dev;
  121. struct net2280_ep *ep;
  122. u32 max, tmp;
  123. unsigned long flags;
  124. ep = container_of (_ep, struct net2280_ep, ep);
  125. if (!_ep || !desc || ep->desc || _ep->name == ep0name
  126. || desc->bDescriptorType != USB_DT_ENDPOINT)
  127. return -EINVAL;
  128. dev = ep->dev;
  129. if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
  130. return -ESHUTDOWN;
  131. /* erratum 0119 workaround ties up an endpoint number */
  132. if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE)
  133. return -EDOM;
  134. /* sanity check ep-e/ep-f since their fifos are small */
  135. max = usb_endpoint_maxp (desc) & 0x1fff;
  136. if (ep->num > 4 && max > 64)
  137. return -ERANGE;
  138. spin_lock_irqsave (&dev->lock, flags);
  139. _ep->maxpacket = max & 0x7ff;
  140. ep->desc = desc;
  141. /* ep_reset() has already been called */
  142. ep->stopped = 0;
  143. ep->wedged = 0;
  144. ep->out_overflow = 0;
  145. /* set speed-dependent max packet; may kick in high bandwidth */
  146. set_idx_reg (dev->regs, REG_EP_MAXPKT (dev, ep->num), max);
  147. /* FIFO lines can't go to different packets. PIO is ok, so
  148. * use it instead of troublesome (non-bulk) multi-packet DMA.
  149. */
  150. if (ep->dma && (max % 4) != 0 && use_dma_chaining) {
  151. DEBUG (ep->dev, "%s, no dma for maxpacket %d\n",
  152. ep->ep.name, ep->ep.maxpacket);
  153. ep->dma = NULL;
  154. }
  155. /* set type, direction, address; reset fifo counters */
  156. writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
  157. tmp = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
  158. if (tmp == USB_ENDPOINT_XFER_INT) {
  159. /* erratum 0105 workaround prevents hs NYET */
  160. if (dev->chiprev == 0100
  161. && dev->gadget.speed == USB_SPEED_HIGH
  162. && !(desc->bEndpointAddress & USB_DIR_IN))
  163. writel ((1 << CLEAR_NAK_OUT_PACKETS_MODE),
  164. &ep->regs->ep_rsp);
  165. } else if (tmp == USB_ENDPOINT_XFER_BULK) {
  166. /* catch some particularly blatant driver bugs */
  167. if ((dev->gadget.speed == USB_SPEED_HIGH
  168. && max != 512)
  169. || (dev->gadget.speed == USB_SPEED_FULL
  170. && max > 64)) {
  171. spin_unlock_irqrestore (&dev->lock, flags);
  172. return -ERANGE;
  173. }
  174. }
  175. ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC) ? 1 : 0;
  176. tmp <<= ENDPOINT_TYPE;
  177. tmp |= desc->bEndpointAddress;
  178. tmp |= (4 << ENDPOINT_BYTE_COUNT); /* default full fifo lines */
  179. tmp |= 1 << ENDPOINT_ENABLE;
  180. wmb ();
  181. /* for OUT transfers, block the rx fifo until a read is posted */
  182. ep->is_in = (tmp & USB_DIR_IN) != 0;
  183. if (!ep->is_in)
  184. writel ((1 << SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
  185. else if (dev->pdev->device != 0x2280) {
  186. /* Added for 2282, Don't use nak packets on an in endpoint,
  187. * this was ignored on 2280
  188. */
  189. writel ((1 << CLEAR_NAK_OUT_PACKETS)
  190. | (1 << CLEAR_NAK_OUT_PACKETS_MODE), &ep->regs->ep_rsp);
  191. }
  192. writel (tmp, &ep->regs->ep_cfg);
  193. /* enable irqs */
  194. if (!ep->dma) { /* pio, per-packet */
  195. tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
  196. writel (tmp, &dev->regs->pciirqenb0);
  197. tmp = (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE)
  198. | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE);
  199. if (dev->pdev->device == 0x2280)
  200. tmp |= readl (&ep->regs->ep_irqenb);
  201. writel (tmp, &ep->regs->ep_irqenb);
  202. } else { /* dma, per-request */
  203. tmp = (1 << (8 + ep->num)); /* completion */
  204. tmp |= readl (&dev->regs->pciirqenb1);
  205. writel (tmp, &dev->regs->pciirqenb1);
  206. /* for short OUT transfers, dma completions can't
  207. * advance the queue; do it pio-style, by hand.
  208. * NOTE erratum 0112 workaround #2
  209. */
  210. if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
  211. tmp = (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
  212. writel (tmp, &ep->regs->ep_irqenb);
  213. tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
  214. writel (tmp, &dev->regs->pciirqenb0);
  215. }
  216. }
  217. tmp = desc->bEndpointAddress;
  218. DEBUG (dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
  219. _ep->name, tmp & 0x0f, DIR_STRING (tmp),
  220. type_string (desc->bmAttributes),
  221. ep->dma ? "dma" : "pio", max);
  222. /* pci writes may still be posted */
  223. spin_unlock_irqrestore (&dev->lock, flags);
  224. return 0;
  225. }
  226. static int handshake (u32 __iomem *ptr, u32 mask, u32 done, int usec)
  227. {
  228. u32 result;
  229. do {
  230. result = readl (ptr);
  231. if (result == ~(u32)0) /* "device unplugged" */
  232. return -ENODEV;
  233. result &= mask;
  234. if (result == done)
  235. return 0;
  236. udelay (1);
  237. usec--;
  238. } while (usec > 0);
  239. return -ETIMEDOUT;
  240. }
  241. static const struct usb_ep_ops net2280_ep_ops;
  242. static void ep_reset (struct net2280_regs __iomem *regs, struct net2280_ep *ep)
  243. {
  244. u32 tmp;
  245. ep->desc = NULL;
  246. INIT_LIST_HEAD (&ep->queue);
  247. ep->ep.maxpacket = ~0;
  248. ep->ep.ops = &net2280_ep_ops;
  249. /* disable the dma, irqs, endpoint... */
  250. if (ep->dma) {
  251. writel (0, &ep->dma->dmactl);
  252. writel ( (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
  253. | (1 << DMA_TRANSACTION_DONE_INTERRUPT)
  254. | (1 << DMA_ABORT)
  255. , &ep->dma->dmastat);
  256. tmp = readl (&regs->pciirqenb0);
  257. tmp &= ~(1 << ep->num);
  258. writel (tmp, &regs->pciirqenb0);
  259. } else {
  260. tmp = readl (&regs->pciirqenb1);
  261. tmp &= ~(1 << (8 + ep->num)); /* completion */
  262. writel (tmp, &regs->pciirqenb1);
  263. }
  264. writel (0, &ep->regs->ep_irqenb);
  265. /* init to our chosen defaults, notably so that we NAK OUT
  266. * packets until the driver queues a read (+note erratum 0112)
  267. */
  268. if (!ep->is_in || ep->dev->pdev->device == 0x2280) {
  269. tmp = (1 << SET_NAK_OUT_PACKETS_MODE)
  270. | (1 << SET_NAK_OUT_PACKETS)
  271. | (1 << CLEAR_EP_HIDE_STATUS_PHASE)
  272. | (1 << CLEAR_INTERRUPT_MODE);
  273. } else {
  274. /* added for 2282 */
  275. tmp = (1 << CLEAR_NAK_OUT_PACKETS_MODE)
  276. | (1 << CLEAR_NAK_OUT_PACKETS)
  277. | (1 << CLEAR_EP_HIDE_STATUS_PHASE)
  278. | (1 << CLEAR_INTERRUPT_MODE);
  279. }
  280. if (ep->num != 0) {
  281. tmp |= (1 << CLEAR_ENDPOINT_TOGGLE)
  282. | (1 << CLEAR_ENDPOINT_HALT);
  283. }
  284. writel (tmp, &ep->regs->ep_rsp);
  285. /* scrub most status bits, and flush any fifo state */
  286. if (ep->dev->pdev->device == 0x2280)
  287. tmp = (1 << FIFO_OVERFLOW)
  288. | (1 << FIFO_UNDERFLOW);
  289. else
  290. tmp = 0;
  291. writel (tmp | (1 << TIMEOUT)
  292. | (1 << USB_STALL_SENT)
  293. | (1 << USB_IN_NAK_SENT)
  294. | (1 << USB_IN_ACK_RCVD)
  295. | (1 << USB_OUT_PING_NAK_SENT)
  296. | (1 << USB_OUT_ACK_SENT)
  297. | (1 << FIFO_FLUSH)
  298. | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
  299. | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
  300. | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
  301. | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
  302. | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
  303. | (1 << DATA_IN_TOKEN_INTERRUPT)
  304. , &ep->regs->ep_stat);
  305. /* fifo size is handled separately */
  306. }
  307. static void nuke (struct net2280_ep *);
  308. static int net2280_disable (struct usb_ep *_ep)
  309. {
  310. struct net2280_ep *ep;
  311. unsigned long flags;
  312. ep = container_of (_ep, struct net2280_ep, ep);
  313. if (!_ep || !ep->desc || _ep->name == ep0name)
  314. return -EINVAL;
  315. spin_lock_irqsave (&ep->dev->lock, flags);
  316. nuke (ep);
  317. ep_reset (ep->dev->regs, ep);
  318. VDEBUG (ep->dev, "disabled %s %s\n",
  319. ep->dma ? "dma" : "pio", _ep->name);
  320. /* synch memory views with the device */
  321. (void) readl (&ep->regs->ep_cfg);
  322. if (use_dma && !ep->dma && ep->num >= 1 && ep->num <= 4)
  323. ep->dma = &ep->dev->dma [ep->num - 1];
  324. spin_unlock_irqrestore (&ep->dev->lock, flags);
  325. return 0;
  326. }
  327. /*-------------------------------------------------------------------------*/
  328. static struct usb_request *
  329. net2280_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
  330. {
  331. struct net2280_ep *ep;
  332. struct net2280_request *req;
  333. if (!_ep)
  334. return NULL;
  335. ep = container_of (_ep, struct net2280_ep, ep);
  336. req = kzalloc(sizeof(*req), gfp_flags);
  337. if (!req)
  338. return NULL;
  339. req->req.dma = DMA_ADDR_INVALID;
  340. INIT_LIST_HEAD (&req->queue);
  341. /* this dma descriptor may be swapped with the previous dummy */
  342. if (ep->dma) {
  343. struct net2280_dma *td;
  344. td = pci_pool_alloc (ep->dev->requests, gfp_flags,
  345. &req->td_dma);
  346. if (!td) {
  347. kfree (req);
  348. return NULL;
  349. }
  350. td->dmacount = 0; /* not VALID */
  351. td->dmaaddr = cpu_to_le32 (DMA_ADDR_INVALID);
  352. td->dmadesc = td->dmaaddr;
  353. req->td = td;
  354. }
  355. return &req->req;
  356. }
  357. static void
  358. net2280_free_request (struct usb_ep *_ep, struct usb_request *_req)
  359. {
  360. struct net2280_ep *ep;
  361. struct net2280_request *req;
  362. ep = container_of (_ep, struct net2280_ep, ep);
  363. if (!_ep || !_req)
  364. return;
  365. req = container_of (_req, struct net2280_request, req);
  366. WARN_ON (!list_empty (&req->queue));
  367. if (req->td)
  368. pci_pool_free (ep->dev->requests, req->td, req->td_dma);
  369. kfree (req);
  370. }
  371. /*-------------------------------------------------------------------------*/
  372. /* load a packet into the fifo we use for usb IN transfers.
  373. * works for all endpoints.
  374. *
  375. * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
  376. * at a time, but this code is simpler because it knows it only writes
  377. * one packet. ep-a..ep-d should use dma instead.
  378. */
  379. static void
  380. write_fifo (struct net2280_ep *ep, struct usb_request *req)
  381. {
  382. struct net2280_ep_regs __iomem *regs = ep->regs;
  383. u8 *buf;
  384. u32 tmp;
  385. unsigned count, total;
  386. /* INVARIANT: fifo is currently empty. (testable) */
  387. if (req) {
  388. buf = req->buf + req->actual;
  389. prefetch (buf);
  390. total = req->length - req->actual;
  391. } else {
  392. total = 0;
  393. buf = NULL;
  394. }
  395. /* write just one packet at a time */
  396. count = ep->ep.maxpacket;
  397. if (count > total) /* min() cannot be used on a bitfield */
  398. count = total;
  399. VDEBUG (ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
  400. ep->ep.name, count,
  401. (count != ep->ep.maxpacket) ? " (short)" : "",
  402. req);
  403. while (count >= 4) {
  404. /* NOTE be careful if you try to align these. fifo lines
  405. * should normally be full (4 bytes) and successive partial
  406. * lines are ok only in certain cases.
  407. */
  408. tmp = get_unaligned ((u32 *)buf);
  409. cpu_to_le32s (&tmp);
  410. writel (tmp, &regs->ep_data);
  411. buf += 4;
  412. count -= 4;
  413. }
  414. /* last fifo entry is "short" unless we wrote a full packet.
  415. * also explicitly validate last word in (periodic) transfers
  416. * when maxpacket is not a multiple of 4 bytes.
  417. */
  418. if (count || total < ep->ep.maxpacket) {
  419. tmp = count ? get_unaligned ((u32 *)buf) : count;
  420. cpu_to_le32s (&tmp);
  421. set_fifo_bytecount (ep, count & 0x03);
  422. writel (tmp, &regs->ep_data);
  423. }
  424. /* pci writes may still be posted */
  425. }
  426. /* work around erratum 0106: PCI and USB race over the OUT fifo.
  427. * caller guarantees chiprev 0100, out endpoint is NAKing, and
  428. * there's no real data in the fifo.
  429. *
  430. * NOTE: also used in cases where that erratum doesn't apply:
  431. * where the host wrote "too much" data to us.
  432. */
  433. static void out_flush (struct net2280_ep *ep)
  434. {
  435. u32 __iomem *statp;
  436. u32 tmp;
  437. ASSERT_OUT_NAKING (ep);
  438. statp = &ep->regs->ep_stat;
  439. writel ( (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
  440. | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
  441. , statp);
  442. writel ((1 << FIFO_FLUSH), statp);
  443. mb ();
  444. tmp = readl (statp);
  445. if (tmp & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
  446. /* high speed did bulk NYET; fifo isn't filling */
  447. && ep->dev->gadget.speed == USB_SPEED_FULL) {
  448. unsigned usec;
  449. usec = 50; /* 64 byte bulk/interrupt */
  450. handshake (statp, (1 << USB_OUT_PING_NAK_SENT),
  451. (1 << USB_OUT_PING_NAK_SENT), usec);
  452. /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
  453. }
  454. }
  455. /* unload packet(s) from the fifo we use for usb OUT transfers.
  456. * returns true iff the request completed, because of short packet
  457. * or the request buffer having filled with full packets.
  458. *
  459. * for ep-a..ep-d this will read multiple packets out when they
  460. * have been accepted.
  461. */
  462. static int
  463. read_fifo (struct net2280_ep *ep, struct net2280_request *req)
  464. {
  465. struct net2280_ep_regs __iomem *regs = ep->regs;
  466. u8 *buf = req->req.buf + req->req.actual;
  467. unsigned count, tmp, is_short;
  468. unsigned cleanup = 0, prevent = 0;
  469. /* erratum 0106 ... packets coming in during fifo reads might
  470. * be incompletely rejected. not all cases have workarounds.
  471. */
  472. if (ep->dev->chiprev == 0x0100
  473. && ep->dev->gadget.speed == USB_SPEED_FULL) {
  474. udelay (1);
  475. tmp = readl (&ep->regs->ep_stat);
  476. if ((tmp & (1 << NAK_OUT_PACKETS)))
  477. cleanup = 1;
  478. else if ((tmp & (1 << FIFO_FULL))) {
  479. start_out_naking (ep);
  480. prevent = 1;
  481. }
  482. /* else: hope we don't see the problem */
  483. }
  484. /* never overflow the rx buffer. the fifo reads packets until
  485. * it sees a short one; we might not be ready for them all.
  486. */
  487. prefetchw (buf);
  488. count = readl (&regs->ep_avail);
  489. if (unlikely (count == 0)) {
  490. udelay (1);
  491. tmp = readl (&ep->regs->ep_stat);
  492. count = readl (&regs->ep_avail);
  493. /* handled that data already? */
  494. if (count == 0 && (tmp & (1 << NAK_OUT_PACKETS)) == 0)
  495. return 0;
  496. }
  497. tmp = req->req.length - req->req.actual;
  498. if (count > tmp) {
  499. /* as with DMA, data overflow gets flushed */
  500. if ((tmp % ep->ep.maxpacket) != 0) {
  501. ERROR (ep->dev,
  502. "%s out fifo %d bytes, expected %d\n",
  503. ep->ep.name, count, tmp);
  504. req->req.status = -EOVERFLOW;
  505. cleanup = 1;
  506. /* NAK_OUT_PACKETS will be set, so flushing is safe;
  507. * the next read will start with the next packet
  508. */
  509. } /* else it's a ZLP, no worries */
  510. count = tmp;
  511. }
  512. req->req.actual += count;
  513. is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
  514. VDEBUG (ep->dev, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
  515. ep->ep.name, count, is_short ? " (short)" : "",
  516. cleanup ? " flush" : "", prevent ? " nak" : "",
  517. req, req->req.actual, req->req.length);
  518. while (count >= 4) {
  519. tmp = readl (&regs->ep_data);
  520. cpu_to_le32s (&tmp);
  521. put_unaligned (tmp, (u32 *)buf);
  522. buf += 4;
  523. count -= 4;
  524. }
  525. if (count) {
  526. tmp = readl (&regs->ep_data);
  527. /* LE conversion is implicit here: */
  528. do {
  529. *buf++ = (u8) tmp;
  530. tmp >>= 8;
  531. } while (--count);
  532. }
  533. if (cleanup)
  534. out_flush (ep);
  535. if (prevent) {
  536. writel ((1 << CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
  537. (void) readl (&ep->regs->ep_rsp);
  538. }
  539. return is_short || ((req->req.actual == req->req.length)
  540. && !req->req.zero);
  541. }
  542. /* fill out dma descriptor to match a given request */
  543. static void
  544. fill_dma_desc (struct net2280_ep *ep, struct net2280_request *req, int valid)
  545. {
  546. struct net2280_dma *td = req->td;
  547. u32 dmacount = req->req.length;
  548. /* don't let DMA continue after a short OUT packet,
  549. * so overruns can't affect the next transfer.
  550. * in case of overruns on max-size packets, we can't
  551. * stop the fifo from filling but we can flush it.
  552. */
  553. if (ep->is_in)
  554. dmacount |= (1 << DMA_DIRECTION);
  555. if ((!ep->is_in && (dmacount % ep->ep.maxpacket) != 0)
  556. || ep->dev->pdev->device != 0x2280)
  557. dmacount |= (1 << END_OF_CHAIN);
  558. req->valid = valid;
  559. if (valid)
  560. dmacount |= (1 << VALID_BIT);
  561. if (likely(!req->req.no_interrupt || !use_dma_chaining))
  562. dmacount |= (1 << DMA_DONE_INTERRUPT_ENABLE);
  563. /* td->dmadesc = previously set by caller */
  564. td->dmaaddr = cpu_to_le32 (req->req.dma);
  565. /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
  566. wmb ();
  567. td->dmacount = cpu_to_le32(dmacount);
  568. }
  569. static const u32 dmactl_default =
  570. (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
  571. | (1 << DMA_CLEAR_COUNT_ENABLE)
  572. /* erratum 0116 workaround part 1 (use POLLING) */
  573. | (POLL_100_USEC << DESCRIPTOR_POLLING_RATE)
  574. | (1 << DMA_VALID_BIT_POLLING_ENABLE)
  575. | (1 << DMA_VALID_BIT_ENABLE)
  576. | (1 << DMA_SCATTER_GATHER_ENABLE)
  577. /* erratum 0116 workaround part 2 (no AUTOSTART) */
  578. | (1 << DMA_ENABLE);
  579. static inline void spin_stop_dma (struct net2280_dma_regs __iomem *dma)
  580. {
  581. handshake (&dma->dmactl, (1 << DMA_ENABLE), 0, 50);
  582. }
  583. static inline void stop_dma (struct net2280_dma_regs __iomem *dma)
  584. {
  585. writel (readl (&dma->dmactl) & ~(1 << DMA_ENABLE), &dma->dmactl);
  586. spin_stop_dma (dma);
  587. }
  588. static void start_queue (struct net2280_ep *ep, u32 dmactl, u32 td_dma)
  589. {
  590. struct net2280_dma_regs __iomem *dma = ep->dma;
  591. unsigned int tmp = (1 << VALID_BIT) | (ep->is_in << DMA_DIRECTION);
  592. if (ep->dev->pdev->device != 0x2280)
  593. tmp |= (1 << END_OF_CHAIN);
  594. writel (tmp, &dma->dmacount);
  595. writel (readl (&dma->dmastat), &dma->dmastat);
  596. writel (td_dma, &dma->dmadesc);
  597. writel (dmactl, &dma->dmactl);
  598. /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
  599. (void) readl (&ep->dev->pci->pcimstctl);
  600. writel ((1 << DMA_START), &dma->dmastat);
  601. if (!ep->is_in)
  602. stop_out_naking (ep);
  603. }
  604. static void start_dma (struct net2280_ep *ep, struct net2280_request *req)
  605. {
  606. u32 tmp;
  607. struct net2280_dma_regs __iomem *dma = ep->dma;
  608. /* FIXME can't use DMA for ZLPs */
  609. /* on this path we "know" there's no dma active (yet) */
  610. WARN_ON (readl (&dma->dmactl) & (1 << DMA_ENABLE));
  611. writel (0, &ep->dma->dmactl);
  612. /* previous OUT packet might have been short */
  613. if (!ep->is_in && ((tmp = readl (&ep->regs->ep_stat))
  614. & (1 << NAK_OUT_PACKETS)) != 0) {
  615. writel ((1 << SHORT_PACKET_TRANSFERRED_INTERRUPT),
  616. &ep->regs->ep_stat);
  617. tmp = readl (&ep->regs->ep_avail);
  618. if (tmp) {
  619. writel (readl (&dma->dmastat), &dma->dmastat);
  620. /* transfer all/some fifo data */
  621. writel (req->req.dma, &dma->dmaaddr);
  622. tmp = min (tmp, req->req.length);
  623. /* dma irq, faking scatterlist status */
  624. req->td->dmacount = cpu_to_le32 (req->req.length - tmp);
  625. writel ((1 << DMA_DONE_INTERRUPT_ENABLE)
  626. | tmp, &dma->dmacount);
  627. req->td->dmadesc = 0;
  628. req->valid = 1;
  629. writel ((1 << DMA_ENABLE), &dma->dmactl);
  630. writel ((1 << DMA_START), &dma->dmastat);
  631. return;
  632. }
  633. }
  634. tmp = dmactl_default;
  635. /* force packet boundaries between dma requests, but prevent the
  636. * controller from automagically writing a last "short" packet
  637. * (zero length) unless the driver explicitly said to do that.
  638. */
  639. if (ep->is_in) {
  640. if (likely ((req->req.length % ep->ep.maxpacket) != 0
  641. || req->req.zero)) {
  642. tmp |= (1 << DMA_FIFO_VALIDATE);
  643. ep->in_fifo_validate = 1;
  644. } else
  645. ep->in_fifo_validate = 0;
  646. }
  647. /* init req->td, pointing to the current dummy */
  648. req->td->dmadesc = cpu_to_le32 (ep->td_dma);
  649. fill_dma_desc (ep, req, 1);
  650. if (!use_dma_chaining)
  651. req->td->dmacount |= cpu_to_le32 (1 << END_OF_CHAIN);
  652. start_queue (ep, tmp, req->td_dma);
  653. }
  654. static inline void
  655. queue_dma (struct net2280_ep *ep, struct net2280_request *req, int valid)
  656. {
  657. struct net2280_dma *end;
  658. dma_addr_t tmp;
  659. /* swap new dummy for old, link; fill and maybe activate */
  660. end = ep->dummy;
  661. ep->dummy = req->td;
  662. req->td = end;
  663. tmp = ep->td_dma;
  664. ep->td_dma = req->td_dma;
  665. req->td_dma = tmp;
  666. end->dmadesc = cpu_to_le32 (ep->td_dma);
  667. fill_dma_desc (ep, req, valid);
  668. }
  669. static void
  670. done (struct net2280_ep *ep, struct net2280_request *req, int status)
  671. {
  672. struct net2280 *dev;
  673. unsigned stopped = ep->stopped;
  674. list_del_init (&req->queue);
  675. if (req->req.status == -EINPROGRESS)
  676. req->req.status = status;
  677. else
  678. status = req->req.status;
  679. dev = ep->dev;
  680. if (ep->dma)
  681. usb_gadget_unmap_request(&dev->gadget, &req->req, ep->is_in);
  682. if (status && status != -ESHUTDOWN)
  683. VDEBUG (dev, "complete %s req %p stat %d len %u/%u\n",
  684. ep->ep.name, &req->req, status,
  685. req->req.actual, req->req.length);
  686. /* don't modify queue heads during completion callback */
  687. ep->stopped = 1;
  688. spin_unlock (&dev->lock);
  689. req->req.complete (&ep->ep, &req->req);
  690. spin_lock (&dev->lock);
  691. ep->stopped = stopped;
  692. }
  693. /*-------------------------------------------------------------------------*/
  694. static int
  695. net2280_queue (struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
  696. {
  697. struct net2280_request *req;
  698. struct net2280_ep *ep;
  699. struct net2280 *dev;
  700. unsigned long flags;
  701. /* we always require a cpu-view buffer, so that we can
  702. * always use pio (as fallback or whatever).
  703. */
  704. req = container_of (_req, struct net2280_request, req);
  705. if (!_req || !_req->complete || !_req->buf
  706. || !list_empty (&req->queue))
  707. return -EINVAL;
  708. if (_req->length > (~0 & DMA_BYTE_COUNT_MASK))
  709. return -EDOM;
  710. ep = container_of (_ep, struct net2280_ep, ep);
  711. if (!_ep || (!ep->desc && ep->num != 0))
  712. return -EINVAL;
  713. dev = ep->dev;
  714. if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
  715. return -ESHUTDOWN;
  716. /* FIXME implement PIO fallback for ZLPs with DMA */
  717. if (ep->dma && _req->length == 0)
  718. return -EOPNOTSUPP;
  719. /* set up dma mapping in case the caller didn't */
  720. if (ep->dma) {
  721. int ret;
  722. ret = usb_gadget_map_request(&dev->gadget, _req,
  723. ep->is_in);
  724. if (ret)
  725. return ret;
  726. }
  727. #if 0
  728. VDEBUG (dev, "%s queue req %p, len %d buf %p\n",
  729. _ep->name, _req, _req->length, _req->buf);
  730. #endif
  731. spin_lock_irqsave (&dev->lock, flags);
  732. _req->status = -EINPROGRESS;
  733. _req->actual = 0;
  734. /* kickstart this i/o queue? */
  735. if (list_empty (&ep->queue) && !ep->stopped) {
  736. /* use DMA if the endpoint supports it, else pio */
  737. if (ep->dma)
  738. start_dma (ep, req);
  739. else {
  740. /* maybe there's no control data, just status ack */
  741. if (ep->num == 0 && _req->length == 0) {
  742. allow_status (ep);
  743. done (ep, req, 0);
  744. VDEBUG (dev, "%s status ack\n", ep->ep.name);
  745. goto done;
  746. }
  747. /* PIO ... stuff the fifo, or unblock it. */
  748. if (ep->is_in)
  749. write_fifo (ep, _req);
  750. else if (list_empty (&ep->queue)) {
  751. u32 s;
  752. /* OUT FIFO might have packet(s) buffered */
  753. s = readl (&ep->regs->ep_stat);
  754. if ((s & (1 << FIFO_EMPTY)) == 0) {
  755. /* note: _req->short_not_ok is
  756. * ignored here since PIO _always_
  757. * stops queue advance here, and
  758. * _req->status doesn't change for
  759. * short reads (only _req->actual)
  760. */
  761. if (read_fifo (ep, req)) {
  762. done (ep, req, 0);
  763. if (ep->num == 0)
  764. allow_status (ep);
  765. /* don't queue it */
  766. req = NULL;
  767. } else
  768. s = readl (&ep->regs->ep_stat);
  769. }
  770. /* don't NAK, let the fifo fill */
  771. if (req && (s & (1 << NAK_OUT_PACKETS)))
  772. writel ((1 << CLEAR_NAK_OUT_PACKETS),
  773. &ep->regs->ep_rsp);
  774. }
  775. }
  776. } else if (ep->dma) {
  777. int valid = 1;
  778. if (ep->is_in) {
  779. int expect;
  780. /* preventing magic zlps is per-engine state, not
  781. * per-transfer; irq logic must recover hiccups.
  782. */
  783. expect = likely (req->req.zero
  784. || (req->req.length % ep->ep.maxpacket) != 0);
  785. if (expect != ep->in_fifo_validate)
  786. valid = 0;
  787. }
  788. queue_dma (ep, req, valid);
  789. } /* else the irq handler advances the queue. */
  790. ep->responded = 1;
  791. if (req)
  792. list_add_tail (&req->queue, &ep->queue);
  793. done:
  794. spin_unlock_irqrestore (&dev->lock, flags);
  795. /* pci writes may still be posted */
  796. return 0;
  797. }
  798. static inline void
  799. dma_done (
  800. struct net2280_ep *ep,
  801. struct net2280_request *req,
  802. u32 dmacount,
  803. int status
  804. )
  805. {
  806. req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
  807. done (ep, req, status);
  808. }
  809. static void restart_dma (struct net2280_ep *ep);
  810. static void scan_dma_completions (struct net2280_ep *ep)
  811. {
  812. /* only look at descriptors that were "naturally" retired,
  813. * so fifo and list head state won't matter
  814. */
  815. while (!list_empty (&ep->queue)) {
  816. struct net2280_request *req;
  817. u32 tmp;
  818. req = list_entry (ep->queue.next,
  819. struct net2280_request, queue);
  820. if (!req->valid)
  821. break;
  822. rmb ();
  823. tmp = le32_to_cpup (&req->td->dmacount);
  824. if ((tmp & (1 << VALID_BIT)) != 0)
  825. break;
  826. /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
  827. * cases where DMA must be aborted; this code handles
  828. * all non-abort DMA completions.
  829. */
  830. if (unlikely (req->td->dmadesc == 0)) {
  831. /* paranoia */
  832. tmp = readl (&ep->dma->dmacount);
  833. if (tmp & DMA_BYTE_COUNT_MASK)
  834. break;
  835. /* single transfer mode */
  836. dma_done (ep, req, tmp, 0);
  837. break;
  838. } else if (!ep->is_in
  839. && (req->req.length % ep->ep.maxpacket) != 0) {
  840. tmp = readl (&ep->regs->ep_stat);
  841. /* AVOID TROUBLE HERE by not issuing short reads from
  842. * your gadget driver. That helps avoids errata 0121,
  843. * 0122, and 0124; not all cases trigger the warning.
  844. */
  845. if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
  846. WARNING (ep->dev, "%s lost packet sync!\n",
  847. ep->ep.name);
  848. req->req.status = -EOVERFLOW;
  849. } else if ((tmp = readl (&ep->regs->ep_avail)) != 0) {
  850. /* fifo gets flushed later */
  851. ep->out_overflow = 1;
  852. DEBUG (ep->dev, "%s dma, discard %d len %d\n",
  853. ep->ep.name, tmp,
  854. req->req.length);
  855. req->req.status = -EOVERFLOW;
  856. }
  857. }
  858. dma_done (ep, req, tmp, 0);
  859. }
  860. }
  861. static void restart_dma (struct net2280_ep *ep)
  862. {
  863. struct net2280_request *req;
  864. u32 dmactl = dmactl_default;
  865. if (ep->stopped)
  866. return;
  867. req = list_entry (ep->queue.next, struct net2280_request, queue);
  868. if (!use_dma_chaining) {
  869. start_dma (ep, req);
  870. return;
  871. }
  872. /* the 2280 will be processing the queue unless queue hiccups after
  873. * the previous transfer:
  874. * IN: wanted automagic zlp, head doesn't (or vice versa)
  875. * DMA_FIFO_VALIDATE doesn't init from dma descriptors.
  876. * OUT: was "usb-short", we must restart.
  877. */
  878. if (ep->is_in && !req->valid) {
  879. struct net2280_request *entry, *prev = NULL;
  880. int reqmode, done = 0;
  881. DEBUG (ep->dev, "%s dma hiccup td %p\n", ep->ep.name, req->td);
  882. ep->in_fifo_validate = likely (req->req.zero
  883. || (req->req.length % ep->ep.maxpacket) != 0);
  884. if (ep->in_fifo_validate)
  885. dmactl |= (1 << DMA_FIFO_VALIDATE);
  886. list_for_each_entry (entry, &ep->queue, queue) {
  887. __le32 dmacount;
  888. if (entry == req)
  889. continue;
  890. dmacount = entry->td->dmacount;
  891. if (!done) {
  892. reqmode = likely (entry->req.zero
  893. || (entry->req.length
  894. % ep->ep.maxpacket) != 0);
  895. if (reqmode == ep->in_fifo_validate) {
  896. entry->valid = 1;
  897. dmacount |= valid_bit;
  898. entry->td->dmacount = dmacount;
  899. prev = entry;
  900. continue;
  901. } else {
  902. /* force a hiccup */
  903. prev->td->dmacount |= dma_done_ie;
  904. done = 1;
  905. }
  906. }
  907. /* walk the rest of the queue so unlinks behave */
  908. entry->valid = 0;
  909. dmacount &= ~valid_bit;
  910. entry->td->dmacount = dmacount;
  911. prev = entry;
  912. }
  913. }
  914. writel (0, &ep->dma->dmactl);
  915. start_queue (ep, dmactl, req->td_dma);
  916. }
  917. static void abort_dma (struct net2280_ep *ep)
  918. {
  919. /* abort the current transfer */
  920. if (likely (!list_empty (&ep->queue))) {
  921. /* FIXME work around errata 0121, 0122, 0124 */
  922. writel ((1 << DMA_ABORT), &ep->dma->dmastat);
  923. spin_stop_dma (ep->dma);
  924. } else
  925. stop_dma (ep->dma);
  926. scan_dma_completions (ep);
  927. }
  928. /* dequeue ALL requests */
  929. static void nuke (struct net2280_ep *ep)
  930. {
  931. struct net2280_request *req;
  932. /* called with spinlock held */
  933. ep->stopped = 1;
  934. if (ep->dma)
  935. abort_dma (ep);
  936. while (!list_empty (&ep->queue)) {
  937. req = list_entry (ep->queue.next,
  938. struct net2280_request,
  939. queue);
  940. done (ep, req, -ESHUTDOWN);
  941. }
  942. }
  943. /* dequeue JUST ONE request */
  944. static int net2280_dequeue (struct usb_ep *_ep, struct usb_request *_req)
  945. {
  946. struct net2280_ep *ep;
  947. struct net2280_request *req;
  948. unsigned long flags;
  949. u32 dmactl;
  950. int stopped;
  951. ep = container_of (_ep, struct net2280_ep, ep);
  952. if (!_ep || (!ep->desc && ep->num != 0) || !_req)
  953. return -EINVAL;
  954. spin_lock_irqsave (&ep->dev->lock, flags);
  955. stopped = ep->stopped;
  956. /* quiesce dma while we patch the queue */
  957. dmactl = 0;
  958. ep->stopped = 1;
  959. if (ep->dma) {
  960. dmactl = readl (&ep->dma->dmactl);
  961. /* WARNING erratum 0127 may kick in ... */
  962. stop_dma (ep->dma);
  963. scan_dma_completions (ep);
  964. }
  965. /* make sure it's still queued on this endpoint */
  966. list_for_each_entry (req, &ep->queue, queue) {
  967. if (&req->req == _req)
  968. break;
  969. }
  970. if (&req->req != _req) {
  971. spin_unlock_irqrestore (&ep->dev->lock, flags);
  972. return -EINVAL;
  973. }
  974. /* queue head may be partially complete. */
  975. if (ep->queue.next == &req->queue) {
  976. if (ep->dma) {
  977. DEBUG (ep->dev, "unlink (%s) dma\n", _ep->name);
  978. _req->status = -ECONNRESET;
  979. abort_dma (ep);
  980. if (likely (ep->queue.next == &req->queue)) {
  981. // NOTE: misreports single-transfer mode
  982. req->td->dmacount = 0; /* invalidate */
  983. dma_done (ep, req,
  984. readl (&ep->dma->dmacount),
  985. -ECONNRESET);
  986. }
  987. } else {
  988. DEBUG (ep->dev, "unlink (%s) pio\n", _ep->name);
  989. done (ep, req, -ECONNRESET);
  990. }
  991. req = NULL;
  992. /* patch up hardware chaining data */
  993. } else if (ep->dma && use_dma_chaining) {
  994. if (req->queue.prev == ep->queue.next) {
  995. writel (le32_to_cpu (req->td->dmadesc),
  996. &ep->dma->dmadesc);
  997. if (req->td->dmacount & dma_done_ie)
  998. writel (readl (&ep->dma->dmacount)
  999. | le32_to_cpu(dma_done_ie),
  1000. &ep->dma->dmacount);
  1001. } else {
  1002. struct net2280_request *prev;
  1003. prev = list_entry (req->queue.prev,
  1004. struct net2280_request, queue);
  1005. prev->td->dmadesc = req->td->dmadesc;
  1006. if (req->td->dmacount & dma_done_ie)
  1007. prev->td->dmacount |= dma_done_ie;
  1008. }
  1009. }
  1010. if (req)
  1011. done (ep, req, -ECONNRESET);
  1012. ep->stopped = stopped;
  1013. if (ep->dma) {
  1014. /* turn off dma on inactive queues */
  1015. if (list_empty (&ep->queue))
  1016. stop_dma (ep->dma);
  1017. else if (!ep->stopped) {
  1018. /* resume current request, or start new one */
  1019. if (req)
  1020. writel (dmactl, &ep->dma->dmactl);
  1021. else
  1022. start_dma (ep, list_entry (ep->queue.next,
  1023. struct net2280_request, queue));
  1024. }
  1025. }
  1026. spin_unlock_irqrestore (&ep->dev->lock, flags);
  1027. return 0;
  1028. }
  1029. /*-------------------------------------------------------------------------*/
  1030. static int net2280_fifo_status (struct usb_ep *_ep);
  1031. static int
  1032. net2280_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
  1033. {
  1034. struct net2280_ep *ep;
  1035. unsigned long flags;
  1036. int retval = 0;
  1037. ep = container_of (_ep, struct net2280_ep, ep);
  1038. if (!_ep || (!ep->desc && ep->num != 0))
  1039. return -EINVAL;
  1040. if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
  1041. return -ESHUTDOWN;
  1042. if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
  1043. == USB_ENDPOINT_XFER_ISOC)
  1044. return -EINVAL;
  1045. spin_lock_irqsave (&ep->dev->lock, flags);
  1046. if (!list_empty (&ep->queue))
  1047. retval = -EAGAIN;
  1048. else if (ep->is_in && value && net2280_fifo_status (_ep) != 0)
  1049. retval = -EAGAIN;
  1050. else {
  1051. VDEBUG (ep->dev, "%s %s %s\n", _ep->name,
  1052. value ? "set" : "clear",
  1053. wedged ? "wedge" : "halt");
  1054. /* set/clear, then synch memory views with the device */
  1055. if (value) {
  1056. if (ep->num == 0)
  1057. ep->dev->protocol_stall = 1;
  1058. else
  1059. set_halt (ep);
  1060. if (wedged)
  1061. ep->wedged = 1;
  1062. } else {
  1063. clear_halt (ep);
  1064. ep->wedged = 0;
  1065. }
  1066. (void) readl (&ep->regs->ep_rsp);
  1067. }
  1068. spin_unlock_irqrestore (&ep->dev->lock, flags);
  1069. return retval;
  1070. }
  1071. static int
  1072. net2280_set_halt(struct usb_ep *_ep, int value)
  1073. {
  1074. return net2280_set_halt_and_wedge(_ep, value, 0);
  1075. }
  1076. static int
  1077. net2280_set_wedge(struct usb_ep *_ep)
  1078. {
  1079. if (!_ep || _ep->name == ep0name)
  1080. return -EINVAL;
  1081. return net2280_set_halt_and_wedge(_ep, 1, 1);
  1082. }
  1083. static int
  1084. net2280_fifo_status (struct usb_ep *_ep)
  1085. {
  1086. struct net2280_ep *ep;
  1087. u32 avail;
  1088. ep = container_of (_ep, struct net2280_ep, ep);
  1089. if (!_ep || (!ep->desc && ep->num != 0))
  1090. return -ENODEV;
  1091. if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
  1092. return -ESHUTDOWN;
  1093. avail = readl (&ep->regs->ep_avail) & ((1 << 12) - 1);
  1094. if (avail > ep->fifo_size)
  1095. return -EOVERFLOW;
  1096. if (ep->is_in)
  1097. avail = ep->fifo_size - avail;
  1098. return avail;
  1099. }
  1100. static void
  1101. net2280_fifo_flush (struct usb_ep *_ep)
  1102. {
  1103. struct net2280_ep *ep;
  1104. ep = container_of (_ep, struct net2280_ep, ep);
  1105. if (!_ep || (!ep->desc && ep->num != 0))
  1106. return;
  1107. if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
  1108. return;
  1109. writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
  1110. (void) readl (&ep->regs->ep_rsp);
  1111. }
  1112. static const struct usb_ep_ops net2280_ep_ops = {
  1113. .enable = net2280_enable,
  1114. .disable = net2280_disable,
  1115. .alloc_request = net2280_alloc_request,
  1116. .free_request = net2280_free_request,
  1117. .queue = net2280_queue,
  1118. .dequeue = net2280_dequeue,
  1119. .set_halt = net2280_set_halt,
  1120. .set_wedge = net2280_set_wedge,
  1121. .fifo_status = net2280_fifo_status,
  1122. .fifo_flush = net2280_fifo_flush,
  1123. };
  1124. /*-------------------------------------------------------------------------*/
  1125. static int net2280_get_frame (struct usb_gadget *_gadget)
  1126. {
  1127. struct net2280 *dev;
  1128. unsigned long flags;
  1129. u16 retval;
  1130. if (!_gadget)
  1131. return -ENODEV;
  1132. dev = container_of (_gadget, struct net2280, gadget);
  1133. spin_lock_irqsave (&dev->lock, flags);
  1134. retval = get_idx_reg (dev->regs, REG_FRAME) & 0x03ff;
  1135. spin_unlock_irqrestore (&dev->lock, flags);
  1136. return retval;
  1137. }
  1138. static int net2280_wakeup (struct usb_gadget *_gadget)
  1139. {
  1140. struct net2280 *dev;
  1141. u32 tmp;
  1142. unsigned long flags;
  1143. if (!_gadget)
  1144. return 0;
  1145. dev = container_of (_gadget, struct net2280, gadget);
  1146. spin_lock_irqsave (&dev->lock, flags);
  1147. tmp = readl (&dev->usb->usbctl);
  1148. if (tmp & (1 << DEVICE_REMOTE_WAKEUP_ENABLE))
  1149. writel (1 << GENERATE_RESUME, &dev->usb->usbstat);
  1150. spin_unlock_irqrestore (&dev->lock, flags);
  1151. /* pci writes may still be posted */
  1152. return 0;
  1153. }
  1154. static int net2280_set_selfpowered (struct usb_gadget *_gadget, int value)
  1155. {
  1156. struct net2280 *dev;
  1157. u32 tmp;
  1158. unsigned long flags;
  1159. if (!_gadget)
  1160. return 0;
  1161. dev = container_of (_gadget, struct net2280, gadget);
  1162. spin_lock_irqsave (&dev->lock, flags);
  1163. tmp = readl (&dev->usb->usbctl);
  1164. if (value)
  1165. tmp |= (1 << SELF_POWERED_STATUS);
  1166. else
  1167. tmp &= ~(1 << SELF_POWERED_STATUS);
  1168. writel (tmp, &dev->usb->usbctl);
  1169. spin_unlock_irqrestore (&dev->lock, flags);
  1170. return 0;
  1171. }
  1172. static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
  1173. {
  1174. struct net2280 *dev;
  1175. u32 tmp;
  1176. unsigned long flags;
  1177. if (!_gadget)
  1178. return -ENODEV;
  1179. dev = container_of (_gadget, struct net2280, gadget);
  1180. spin_lock_irqsave (&dev->lock, flags);
  1181. tmp = readl (&dev->usb->usbctl);
  1182. dev->softconnect = (is_on != 0);
  1183. if (is_on)
  1184. tmp |= (1 << USB_DETECT_ENABLE);
  1185. else
  1186. tmp &= ~(1 << USB_DETECT_ENABLE);
  1187. writel (tmp, &dev->usb->usbctl);
  1188. spin_unlock_irqrestore (&dev->lock, flags);
  1189. return 0;
  1190. }
  1191. static int net2280_start(struct usb_gadget *_gadget,
  1192. struct usb_gadget_driver *driver);
  1193. static int net2280_stop(struct usb_gadget *_gadget,
  1194. struct usb_gadget_driver *driver);
  1195. static const struct usb_gadget_ops net2280_ops = {
  1196. .get_frame = net2280_get_frame,
  1197. .wakeup = net2280_wakeup,
  1198. .set_selfpowered = net2280_set_selfpowered,
  1199. .pullup = net2280_pullup,
  1200. .udc_start = net2280_start,
  1201. .udc_stop = net2280_stop,
  1202. };
  1203. /*-------------------------------------------------------------------------*/
  1204. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  1205. /* FIXME move these into procfs, and use seq_file.
  1206. * Sysfs _still_ doesn't behave for arbitrarily sized files,
  1207. * and also doesn't help products using this with 2.4 kernels.
  1208. */
  1209. /* "function" sysfs attribute */
  1210. static ssize_t
  1211. show_function (struct device *_dev, struct device_attribute *attr, char *buf)
  1212. {
  1213. struct net2280 *dev = dev_get_drvdata (_dev);
  1214. if (!dev->driver
  1215. || !dev->driver->function
  1216. || strlen (dev->driver->function) > PAGE_SIZE)
  1217. return 0;
  1218. return scnprintf (buf, PAGE_SIZE, "%s\n", dev->driver->function);
  1219. }
  1220. static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
  1221. static ssize_t net2280_show_registers(struct device *_dev,
  1222. struct device_attribute *attr, char *buf)
  1223. {
  1224. struct net2280 *dev;
  1225. char *next;
  1226. unsigned size, t;
  1227. unsigned long flags;
  1228. int i;
  1229. u32 t1, t2;
  1230. const char *s;
  1231. dev = dev_get_drvdata (_dev);
  1232. next = buf;
  1233. size = PAGE_SIZE;
  1234. spin_lock_irqsave (&dev->lock, flags);
  1235. if (dev->driver)
  1236. s = dev->driver->driver.name;
  1237. else
  1238. s = "(none)";
  1239. /* Main Control Registers */
  1240. t = scnprintf (next, size, "%s version " DRIVER_VERSION
  1241. ", chiprev %04x, dma %s\n\n"
  1242. "devinit %03x fifoctl %08x gadget '%s'\n"
  1243. "pci irqenb0 %02x irqenb1 %08x "
  1244. "irqstat0 %04x irqstat1 %08x\n",
  1245. driver_name, dev->chiprev,
  1246. use_dma
  1247. ? (use_dma_chaining ? "chaining" : "enabled")
  1248. : "disabled",
  1249. readl (&dev->regs->devinit),
  1250. readl (&dev->regs->fifoctl),
  1251. s,
  1252. readl (&dev->regs->pciirqenb0),
  1253. readl (&dev->regs->pciirqenb1),
  1254. readl (&dev->regs->irqstat0),
  1255. readl (&dev->regs->irqstat1));
  1256. size -= t;
  1257. next += t;
  1258. /* USB Control Registers */
  1259. t1 = readl (&dev->usb->usbctl);
  1260. t2 = readl (&dev->usb->usbstat);
  1261. if (t1 & (1 << VBUS_PIN)) {
  1262. if (t2 & (1 << HIGH_SPEED))
  1263. s = "high speed";
  1264. else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
  1265. s = "powered";
  1266. else
  1267. s = "full speed";
  1268. /* full speed bit (6) not working?? */
  1269. } else
  1270. s = "not attached";
  1271. t = scnprintf (next, size,
  1272. "stdrsp %08x usbctl %08x usbstat %08x "
  1273. "addr 0x%02x (%s)\n",
  1274. readl (&dev->usb->stdrsp), t1, t2,
  1275. readl (&dev->usb->ouraddr), s);
  1276. size -= t;
  1277. next += t;
  1278. /* PCI Master Control Registers */
  1279. /* DMA Control Registers */
  1280. /* Configurable EP Control Registers */
  1281. for (i = 0; i < 7; i++) {
  1282. struct net2280_ep *ep;
  1283. ep = &dev->ep [i];
  1284. if (i && !ep->desc)
  1285. continue;
  1286. t1 = readl (&ep->regs->ep_cfg);
  1287. t2 = readl (&ep->regs->ep_rsp) & 0xff;
  1288. t = scnprintf (next, size,
  1289. "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
  1290. "irqenb %02x\n",
  1291. ep->ep.name, t1, t2,
  1292. (t2 & (1 << CLEAR_NAK_OUT_PACKETS))
  1293. ? "NAK " : "",
  1294. (t2 & (1 << CLEAR_EP_HIDE_STATUS_PHASE))
  1295. ? "hide " : "",
  1296. (t2 & (1 << CLEAR_EP_FORCE_CRC_ERROR))
  1297. ? "CRC " : "",
  1298. (t2 & (1 << CLEAR_INTERRUPT_MODE))
  1299. ? "interrupt " : "",
  1300. (t2 & (1<<CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
  1301. ? "status " : "",
  1302. (t2 & (1 << CLEAR_NAK_OUT_PACKETS_MODE))
  1303. ? "NAKmode " : "",
  1304. (t2 & (1 << CLEAR_ENDPOINT_TOGGLE))
  1305. ? "DATA1 " : "DATA0 ",
  1306. (t2 & (1 << CLEAR_ENDPOINT_HALT))
  1307. ? "HALT " : "",
  1308. readl (&ep->regs->ep_irqenb));
  1309. size -= t;
  1310. next += t;
  1311. t = scnprintf (next, size,
  1312. "\tstat %08x avail %04x "
  1313. "(ep%d%s-%s)%s\n",
  1314. readl (&ep->regs->ep_stat),
  1315. readl (&ep->regs->ep_avail),
  1316. t1 & 0x0f, DIR_STRING (t1),
  1317. type_string (t1 >> 8),
  1318. ep->stopped ? "*" : "");
  1319. size -= t;
  1320. next += t;
  1321. if (!ep->dma)
  1322. continue;
  1323. t = scnprintf (next, size,
  1324. " dma\tctl %08x stat %08x count %08x\n"
  1325. "\taddr %08x desc %08x\n",
  1326. readl (&ep->dma->dmactl),
  1327. readl (&ep->dma->dmastat),
  1328. readl (&ep->dma->dmacount),
  1329. readl (&ep->dma->dmaaddr),
  1330. readl (&ep->dma->dmadesc));
  1331. size -= t;
  1332. next += t;
  1333. }
  1334. /* Indexed Registers */
  1335. // none yet
  1336. /* Statistics */
  1337. t = scnprintf (next, size, "\nirqs: ");
  1338. size -= t;
  1339. next += t;
  1340. for (i = 0; i < 7; i++) {
  1341. struct net2280_ep *ep;
  1342. ep = &dev->ep [i];
  1343. if (i && !ep->irqs)
  1344. continue;
  1345. t = scnprintf (next, size, " %s/%lu", ep->ep.name, ep->irqs);
  1346. size -= t;
  1347. next += t;
  1348. }
  1349. t = scnprintf (next, size, "\n");
  1350. size -= t;
  1351. next += t;
  1352. spin_unlock_irqrestore (&dev->lock, flags);
  1353. return PAGE_SIZE - size;
  1354. }
  1355. static DEVICE_ATTR(registers, S_IRUGO, net2280_show_registers, NULL);
  1356. static ssize_t
  1357. show_queues (struct device *_dev, struct device_attribute *attr, char *buf)
  1358. {
  1359. struct net2280 *dev;
  1360. char *next;
  1361. unsigned size;
  1362. unsigned long flags;
  1363. int i;
  1364. dev = dev_get_drvdata (_dev);
  1365. next = buf;
  1366. size = PAGE_SIZE;
  1367. spin_lock_irqsave (&dev->lock, flags);
  1368. for (i = 0; i < 7; i++) {
  1369. struct net2280_ep *ep = &dev->ep [i];
  1370. struct net2280_request *req;
  1371. int t;
  1372. if (i != 0) {
  1373. const struct usb_endpoint_descriptor *d;
  1374. d = ep->desc;
  1375. if (!d)
  1376. continue;
  1377. t = d->bEndpointAddress;
  1378. t = scnprintf (next, size,
  1379. "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
  1380. ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
  1381. (t & USB_DIR_IN) ? "in" : "out",
  1382. ({ char *val;
  1383. switch (d->bmAttributes & 0x03) {
  1384. case USB_ENDPOINT_XFER_BULK:
  1385. val = "bulk"; break;
  1386. case USB_ENDPOINT_XFER_INT:
  1387. val = "intr"; break;
  1388. default:
  1389. val = "iso"; break;
  1390. }; val; }),
  1391. usb_endpoint_maxp (d) & 0x1fff,
  1392. ep->dma ? "dma" : "pio", ep->fifo_size
  1393. );
  1394. } else /* ep0 should only have one transfer queued */
  1395. t = scnprintf (next, size, "ep0 max 64 pio %s\n",
  1396. ep->is_in ? "in" : "out");
  1397. if (t <= 0 || t > size)
  1398. goto done;
  1399. size -= t;
  1400. next += t;
  1401. if (list_empty (&ep->queue)) {
  1402. t = scnprintf (next, size, "\t(nothing queued)\n");
  1403. if (t <= 0 || t > size)
  1404. goto done;
  1405. size -= t;
  1406. next += t;
  1407. continue;
  1408. }
  1409. list_for_each_entry (req, &ep->queue, queue) {
  1410. if (ep->dma && req->td_dma == readl (&ep->dma->dmadesc))
  1411. t = scnprintf (next, size,
  1412. "\treq %p len %d/%d "
  1413. "buf %p (dmacount %08x)\n",
  1414. &req->req, req->req.actual,
  1415. req->req.length, req->req.buf,
  1416. readl (&ep->dma->dmacount));
  1417. else
  1418. t = scnprintf (next, size,
  1419. "\treq %p len %d/%d buf %p\n",
  1420. &req->req, req->req.actual,
  1421. req->req.length, req->req.buf);
  1422. if (t <= 0 || t > size)
  1423. goto done;
  1424. size -= t;
  1425. next += t;
  1426. if (ep->dma) {
  1427. struct net2280_dma *td;
  1428. td = req->td;
  1429. t = scnprintf (next, size, "\t td %08x "
  1430. " count %08x buf %08x desc %08x\n",
  1431. (u32) req->td_dma,
  1432. le32_to_cpu (td->dmacount),
  1433. le32_to_cpu (td->dmaaddr),
  1434. le32_to_cpu (td->dmadesc));
  1435. if (t <= 0 || t > size)
  1436. goto done;
  1437. size -= t;
  1438. next += t;
  1439. }
  1440. }
  1441. }
  1442. done:
  1443. spin_unlock_irqrestore (&dev->lock, flags);
  1444. return PAGE_SIZE - size;
  1445. }
  1446. static DEVICE_ATTR (queues, S_IRUGO, show_queues, NULL);
  1447. #else
  1448. #define device_create_file(a,b) (0)
  1449. #define device_remove_file(a,b) do { } while (0)
  1450. #endif
  1451. /*-------------------------------------------------------------------------*/
  1452. /* another driver-specific mode might be a request type doing dma
  1453. * to/from another device fifo instead of to/from memory.
  1454. */
  1455. static void set_fifo_mode (struct net2280 *dev, int mode)
  1456. {
  1457. /* keeping high bits preserves BAR2 */
  1458. writel ((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
  1459. /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
  1460. INIT_LIST_HEAD (&dev->gadget.ep_list);
  1461. list_add_tail (&dev->ep [1].ep.ep_list, &dev->gadget.ep_list);
  1462. list_add_tail (&dev->ep [2].ep.ep_list, &dev->gadget.ep_list);
  1463. switch (mode) {
  1464. case 0:
  1465. list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
  1466. list_add_tail (&dev->ep [4].ep.ep_list, &dev->gadget.ep_list);
  1467. dev->ep [1].fifo_size = dev->ep [2].fifo_size = 1024;
  1468. break;
  1469. case 1:
  1470. dev->ep [1].fifo_size = dev->ep [2].fifo_size = 2048;
  1471. break;
  1472. case 2:
  1473. list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
  1474. dev->ep [1].fifo_size = 2048;
  1475. dev->ep [2].fifo_size = 1024;
  1476. break;
  1477. }
  1478. /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
  1479. list_add_tail (&dev->ep [5].ep.ep_list, &dev->gadget.ep_list);
  1480. list_add_tail (&dev->ep [6].ep.ep_list, &dev->gadget.ep_list);
  1481. }
  1482. /* keeping it simple:
  1483. * - one bus driver, initted first;
  1484. * - one function driver, initted second
  1485. *
  1486. * most of the work to support multiple net2280 controllers would
  1487. * be to associate this gadget driver (yes?) with all of them, or
  1488. * perhaps to bind specific drivers to specific devices.
  1489. */
  1490. static void usb_reset (struct net2280 *dev)
  1491. {
  1492. u32 tmp;
  1493. dev->gadget.speed = USB_SPEED_UNKNOWN;
  1494. (void) readl (&dev->usb->usbctl);
  1495. net2280_led_init (dev);
  1496. /* disable automatic responses, and irqs */
  1497. writel (0, &dev->usb->stdrsp);
  1498. writel (0, &dev->regs->pciirqenb0);
  1499. writel (0, &dev->regs->pciirqenb1);
  1500. /* clear old dma and irq state */
  1501. for (tmp = 0; tmp < 4; tmp++) {
  1502. struct net2280_ep *ep = &dev->ep [tmp + 1];
  1503. if (ep->dma)
  1504. abort_dma (ep);
  1505. }
  1506. writel (~0, &dev->regs->irqstat0),
  1507. writel (~(1 << SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
  1508. /* reset, and enable pci */
  1509. tmp = readl (&dev->regs->devinit)
  1510. | (1 << PCI_ENABLE)
  1511. | (1 << FIFO_SOFT_RESET)
  1512. | (1 << USB_SOFT_RESET)
  1513. | (1 << M8051_RESET);
  1514. writel (tmp, &dev->regs->devinit);
  1515. /* standard fifo and endpoint allocations */
  1516. set_fifo_mode (dev, (fifo_mode <= 2) ? fifo_mode : 0);
  1517. }
  1518. static void usb_reinit (struct net2280 *dev)
  1519. {
  1520. u32 tmp;
  1521. int init_dma;
  1522. /* use_dma changes are ignored till next device re-init */
  1523. init_dma = use_dma;
  1524. /* basic endpoint init */
  1525. for (tmp = 0; tmp < 7; tmp++) {
  1526. struct net2280_ep *ep = &dev->ep [tmp];
  1527. ep->ep.name = ep_name [tmp];
  1528. ep->dev = dev;
  1529. ep->num = tmp;
  1530. if (tmp > 0 && tmp <= 4) {
  1531. ep->fifo_size = 1024;
  1532. if (init_dma)
  1533. ep->dma = &dev->dma [tmp - 1];
  1534. } else
  1535. ep->fifo_size = 64;
  1536. ep->regs = &dev->epregs [tmp];
  1537. ep_reset (dev->regs, ep);
  1538. }
  1539. dev->ep [0].ep.maxpacket = 64;
  1540. dev->ep [5].ep.maxpacket = 64;
  1541. dev->ep [6].ep.maxpacket = 64;
  1542. dev->gadget.ep0 = &dev->ep [0].ep;
  1543. dev->ep [0].stopped = 0;
  1544. INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
  1545. /* we want to prevent lowlevel/insecure access from the USB host,
  1546. * but erratum 0119 means this enable bit is ignored
  1547. */
  1548. for (tmp = 0; tmp < 5; tmp++)
  1549. writel (EP_DONTUSE, &dev->dep [tmp].dep_cfg);
  1550. }
  1551. static void ep0_start (struct net2280 *dev)
  1552. {
  1553. writel ( (1 << CLEAR_EP_HIDE_STATUS_PHASE)
  1554. | (1 << CLEAR_NAK_OUT_PACKETS)
  1555. | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
  1556. , &dev->epregs [0].ep_rsp);
  1557. /*
  1558. * hardware optionally handles a bunch of standard requests
  1559. * that the API hides from drivers anyway. have it do so.
  1560. * endpoint status/features are handled in software, to
  1561. * help pass tests for some dubious behavior.
  1562. */
  1563. writel ( (1 << SET_TEST_MODE)
  1564. | (1 << SET_ADDRESS)
  1565. | (1 << DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP)
  1566. | (1 << GET_DEVICE_STATUS)
  1567. | (1 << GET_INTERFACE_STATUS)
  1568. , &dev->usb->stdrsp);
  1569. writel ( (1 << USB_ROOT_PORT_WAKEUP_ENABLE)
  1570. | (1 << SELF_POWERED_USB_DEVICE)
  1571. | (1 << REMOTE_WAKEUP_SUPPORT)
  1572. | (dev->softconnect << USB_DETECT_ENABLE)
  1573. | (1 << SELF_POWERED_STATUS)
  1574. , &dev->usb->usbctl);
  1575. /* enable irqs so we can see ep0 and general operation */
  1576. writel ( (1 << SETUP_PACKET_INTERRUPT_ENABLE)
  1577. | (1 << ENDPOINT_0_INTERRUPT_ENABLE)
  1578. , &dev->regs->pciirqenb0);
  1579. writel ( (1 << PCI_INTERRUPT_ENABLE)
  1580. | (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE)
  1581. | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE)
  1582. | (1 << PCI_RETRY_ABORT_INTERRUPT_ENABLE)
  1583. | (1 << VBUS_INTERRUPT_ENABLE)
  1584. | (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE)
  1585. | (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE)
  1586. , &dev->regs->pciirqenb1);
  1587. /* don't leave any writes posted */
  1588. (void) readl (&dev->usb->usbctl);
  1589. }
  1590. /* when a driver is successfully registered, it will receive
  1591. * control requests including set_configuration(), which enables
  1592. * non-control requests. then usb traffic follows until a
  1593. * disconnect is reported. then a host may connect again, or
  1594. * the driver might get unbound.
  1595. */
  1596. static int net2280_start(struct usb_gadget *_gadget,
  1597. struct usb_gadget_driver *driver)
  1598. {
  1599. struct net2280 *dev;
  1600. int retval;
  1601. unsigned i;
  1602. /* insist on high speed support from the driver, since
  1603. * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
  1604. * "must not be used in normal operation"
  1605. */
  1606. if (!driver || driver->max_speed < USB_SPEED_HIGH
  1607. || !driver->setup)
  1608. return -EINVAL;
  1609. dev = container_of (_gadget, struct net2280, gadget);
  1610. for (i = 0; i < 7; i++)
  1611. dev->ep [i].irqs = 0;
  1612. /* hook up the driver ... */
  1613. dev->softconnect = 1;
  1614. driver->driver.bus = NULL;
  1615. dev->driver = driver;
  1616. dev->gadget.dev.driver = &driver->driver;
  1617. retval = device_create_file (&dev->pdev->dev, &dev_attr_function);
  1618. if (retval) goto err_unbind;
  1619. retval = device_create_file (&dev->pdev->dev, &dev_attr_queues);
  1620. if (retval) goto err_func;
  1621. /* ... then enable host detection and ep0; and we're ready
  1622. * for set_configuration as well as eventual disconnect.
  1623. */
  1624. net2280_led_active (dev, 1);
  1625. ep0_start (dev);
  1626. DEBUG (dev, "%s ready, usbctl %08x stdrsp %08x\n",
  1627. driver->driver.name,
  1628. readl (&dev->usb->usbctl),
  1629. readl (&dev->usb->stdrsp));
  1630. /* pci writes may still be posted */
  1631. return 0;
  1632. err_func:
  1633. device_remove_file (&dev->pdev->dev, &dev_attr_function);
  1634. err_unbind:
  1635. driver->unbind (&dev->gadget);
  1636. dev->gadget.dev.driver = NULL;
  1637. dev->driver = NULL;
  1638. return retval;
  1639. }
  1640. static void
  1641. stop_activity (struct net2280 *dev, struct usb_gadget_driver *driver)
  1642. {
  1643. int i;
  1644. /* don't disconnect if it's not connected */
  1645. if (dev->gadget.speed == USB_SPEED_UNKNOWN)
  1646. driver = NULL;
  1647. /* stop hardware; prevent new request submissions;
  1648. * and kill any outstanding requests.
  1649. */
  1650. usb_reset (dev);
  1651. for (i = 0; i < 7; i++)
  1652. nuke (&dev->ep [i]);
  1653. usb_reinit (dev);
  1654. }
  1655. static int net2280_stop(struct usb_gadget *_gadget,
  1656. struct usb_gadget_driver *driver)
  1657. {
  1658. struct net2280 *dev;
  1659. unsigned long flags;
  1660. dev = container_of (_gadget, struct net2280, gadget);
  1661. spin_lock_irqsave (&dev->lock, flags);
  1662. stop_activity (dev, driver);
  1663. spin_unlock_irqrestore (&dev->lock, flags);
  1664. dev->gadget.dev.driver = NULL;
  1665. dev->driver = NULL;
  1666. net2280_led_active (dev, 0);
  1667. device_remove_file (&dev->pdev->dev, &dev_attr_function);
  1668. device_remove_file (&dev->pdev->dev, &dev_attr_queues);
  1669. DEBUG (dev, "unregistered driver '%s'\n", driver->driver.name);
  1670. return 0;
  1671. }
  1672. /*-------------------------------------------------------------------------*/
  1673. /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
  1674. * also works for dma-capable endpoints, in pio mode or just
  1675. * to manually advance the queue after short OUT transfers.
  1676. */
  1677. static void handle_ep_small (struct net2280_ep *ep)
  1678. {
  1679. struct net2280_request *req;
  1680. u32 t;
  1681. /* 0 error, 1 mid-data, 2 done */
  1682. int mode = 1;
  1683. if (!list_empty (&ep->queue))
  1684. req = list_entry (ep->queue.next,
  1685. struct net2280_request, queue);
  1686. else
  1687. req = NULL;
  1688. /* ack all, and handle what we care about */
  1689. t = readl (&ep->regs->ep_stat);
  1690. ep->irqs++;
  1691. #if 0
  1692. VDEBUG (ep->dev, "%s ack ep_stat %08x, req %p\n",
  1693. ep->ep.name, t, req ? &req->req : 0);
  1694. #endif
  1695. if (!ep->is_in || ep->dev->pdev->device == 0x2280)
  1696. writel (t & ~(1 << NAK_OUT_PACKETS), &ep->regs->ep_stat);
  1697. else
  1698. /* Added for 2282 */
  1699. writel (t, &ep->regs->ep_stat);
  1700. /* for ep0, monitor token irqs to catch data stage length errors
  1701. * and to synchronize on status.
  1702. *
  1703. * also, to defer reporting of protocol stalls ... here's where
  1704. * data or status first appears, handling stalls here should never
  1705. * cause trouble on the host side..
  1706. *
  1707. * control requests could be slightly faster without token synch for
  1708. * status, but status can jam up that way.
  1709. */
  1710. if (unlikely (ep->num == 0)) {
  1711. if (ep->is_in) {
  1712. /* status; stop NAKing */
  1713. if (t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)) {
  1714. if (ep->dev->protocol_stall) {
  1715. ep->stopped = 1;
  1716. set_halt (ep);
  1717. }
  1718. if (!req)
  1719. allow_status (ep);
  1720. mode = 2;
  1721. /* reply to extra IN data tokens with a zlp */
  1722. } else if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
  1723. if (ep->dev->protocol_stall) {
  1724. ep->stopped = 1;
  1725. set_halt (ep);
  1726. mode = 2;
  1727. } else if (ep->responded &&
  1728. !req && !ep->stopped)
  1729. write_fifo (ep, NULL);
  1730. }
  1731. } else {
  1732. /* status; stop NAKing */
  1733. if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
  1734. if (ep->dev->protocol_stall) {
  1735. ep->stopped = 1;
  1736. set_halt (ep);
  1737. }
  1738. mode = 2;
  1739. /* an extra OUT token is an error */
  1740. } else if (((t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT))
  1741. && req
  1742. && req->req.actual == req->req.length)
  1743. || (ep->responded && !req)) {
  1744. ep->dev->protocol_stall = 1;
  1745. set_halt (ep);
  1746. ep->stopped = 1;
  1747. if (req)
  1748. done (ep, req, -EOVERFLOW);
  1749. req = NULL;
  1750. }
  1751. }
  1752. }
  1753. if (unlikely (!req))
  1754. return;
  1755. /* manual DMA queue advance after short OUT */
  1756. if (likely (ep->dma != 0)) {
  1757. if (t & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
  1758. u32 count;
  1759. int stopped = ep->stopped;
  1760. /* TRANSFERRED works around OUT_DONE erratum 0112.
  1761. * we expect (N <= maxpacket) bytes; host wrote M.
  1762. * iff (M < N) we won't ever see a DMA interrupt.
  1763. */
  1764. ep->stopped = 1;
  1765. for (count = 0; ; t = readl (&ep->regs->ep_stat)) {
  1766. /* any preceding dma transfers must finish.
  1767. * dma handles (M >= N), may empty the queue
  1768. */
  1769. scan_dma_completions (ep);
  1770. if (unlikely (list_empty (&ep->queue)
  1771. || ep->out_overflow)) {
  1772. req = NULL;
  1773. break;
  1774. }
  1775. req = list_entry (ep->queue.next,
  1776. struct net2280_request, queue);
  1777. /* here either (M < N), a "real" short rx;
  1778. * or (M == N) and the queue didn't empty
  1779. */
  1780. if (likely (t & (1 << FIFO_EMPTY))) {
  1781. count = readl (&ep->dma->dmacount);
  1782. count &= DMA_BYTE_COUNT_MASK;
  1783. if (readl (&ep->dma->dmadesc)
  1784. != req->td_dma)
  1785. req = NULL;
  1786. break;
  1787. }
  1788. udelay(1);
  1789. }
  1790. /* stop DMA, leave ep NAKing */
  1791. writel ((1 << DMA_ABORT), &ep->dma->dmastat);
  1792. spin_stop_dma (ep->dma);
  1793. if (likely (req)) {
  1794. req->td->dmacount = 0;
  1795. t = readl (&ep->regs->ep_avail);
  1796. dma_done (ep, req, count,
  1797. (ep->out_overflow || t)
  1798. ? -EOVERFLOW : 0);
  1799. }
  1800. /* also flush to prevent erratum 0106 trouble */
  1801. if (unlikely (ep->out_overflow
  1802. || (ep->dev->chiprev == 0x0100
  1803. && ep->dev->gadget.speed
  1804. == USB_SPEED_FULL))) {
  1805. out_flush (ep);
  1806. ep->out_overflow = 0;
  1807. }
  1808. /* (re)start dma if needed, stop NAKing */
  1809. ep->stopped = stopped;
  1810. if (!list_empty (&ep->queue))
  1811. restart_dma (ep);
  1812. } else
  1813. DEBUG (ep->dev, "%s dma ep_stat %08x ??\n",
  1814. ep->ep.name, t);
  1815. return;
  1816. /* data packet(s) received (in the fifo, OUT) */
  1817. } else if (t & (1 << DATA_PACKET_RECEIVED_INTERRUPT)) {
  1818. if (read_fifo (ep, req) && ep->num != 0)
  1819. mode = 2;
  1820. /* data packet(s) transmitted (IN) */
  1821. } else if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)) {
  1822. unsigned len;
  1823. len = req->req.length - req->req.actual;
  1824. if (len > ep->ep.maxpacket)
  1825. len = ep->ep.maxpacket;
  1826. req->req.actual += len;
  1827. /* if we wrote it all, we're usually done */
  1828. if (req->req.actual == req->req.length) {
  1829. if (ep->num == 0) {
  1830. /* send zlps until the status stage */
  1831. } else if (!req->req.zero || len != ep->ep.maxpacket)
  1832. mode = 2;
  1833. }
  1834. /* there was nothing to do ... */
  1835. } else if (mode == 1)
  1836. return;
  1837. /* done */
  1838. if (mode == 2) {
  1839. /* stream endpoints often resubmit/unlink in completion */
  1840. done (ep, req, 0);
  1841. /* maybe advance queue to next request */
  1842. if (ep->num == 0) {
  1843. /* NOTE: net2280 could let gadget driver start the
  1844. * status stage later. since not all controllers let
  1845. * them control that, the api doesn't (yet) allow it.
  1846. */
  1847. if (!ep->stopped)
  1848. allow_status (ep);
  1849. req = NULL;
  1850. } else {
  1851. if (!list_empty (&ep->queue) && !ep->stopped)
  1852. req = list_entry (ep->queue.next,
  1853. struct net2280_request, queue);
  1854. else
  1855. req = NULL;
  1856. if (req && !ep->is_in)
  1857. stop_out_naking (ep);
  1858. }
  1859. }
  1860. /* is there a buffer for the next packet?
  1861. * for best streaming performance, make sure there is one.
  1862. */
  1863. if (req && !ep->stopped) {
  1864. /* load IN fifo with next packet (may be zlp) */
  1865. if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT))
  1866. write_fifo (ep, &req->req);
  1867. }
  1868. }
  1869. static struct net2280_ep *
  1870. get_ep_by_addr (struct net2280 *dev, u16 wIndex)
  1871. {
  1872. struct net2280_ep *ep;
  1873. if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
  1874. return &dev->ep [0];
  1875. list_for_each_entry (ep, &dev->gadget.ep_list, ep.ep_list) {
  1876. u8 bEndpointAddress;
  1877. if (!ep->desc)
  1878. continue;
  1879. bEndpointAddress = ep->desc->bEndpointAddress;
  1880. if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
  1881. continue;
  1882. if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
  1883. return ep;
  1884. }
  1885. return NULL;
  1886. }
  1887. static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
  1888. {
  1889. struct net2280_ep *ep;
  1890. u32 num, scratch;
  1891. /* most of these don't need individual acks */
  1892. stat &= ~(1 << INTA_ASSERTED);
  1893. if (!stat)
  1894. return;
  1895. // DEBUG (dev, "irqstat0 %04x\n", stat);
  1896. /* starting a control request? */
  1897. if (unlikely (stat & (1 << SETUP_PACKET_INTERRUPT))) {
  1898. union {
  1899. u32 raw [2];
  1900. struct usb_ctrlrequest r;
  1901. } u;
  1902. int tmp;
  1903. struct net2280_request *req;
  1904. if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
  1905. if (readl (&dev->usb->usbstat) & (1 << HIGH_SPEED))
  1906. dev->gadget.speed = USB_SPEED_HIGH;
  1907. else
  1908. dev->gadget.speed = USB_SPEED_FULL;
  1909. net2280_led_speed (dev, dev->gadget.speed);
  1910. DEBUG(dev, "%s\n", usb_speed_string(dev->gadget.speed));
  1911. }
  1912. ep = &dev->ep [0];
  1913. ep->irqs++;
  1914. /* make sure any leftover request state is cleared */
  1915. stat &= ~(1 << ENDPOINT_0_INTERRUPT);
  1916. while (!list_empty (&ep->queue)) {
  1917. req = list_entry (ep->queue.next,
  1918. struct net2280_request, queue);
  1919. done (ep, req, (req->req.actual == req->req.length)
  1920. ? 0 : -EPROTO);
  1921. }
  1922. ep->stopped = 0;
  1923. dev->protocol_stall = 0;
  1924. if (ep->dev->pdev->device == 0x2280)
  1925. tmp = (1 << FIFO_OVERFLOW)
  1926. | (1 << FIFO_UNDERFLOW);
  1927. else
  1928. tmp = 0;
  1929. writel (tmp | (1 << TIMEOUT)
  1930. | (1 << USB_STALL_SENT)
  1931. | (1 << USB_IN_NAK_SENT)
  1932. | (1 << USB_IN_ACK_RCVD)
  1933. | (1 << USB_OUT_PING_NAK_SENT)
  1934. | (1 << USB_OUT_ACK_SENT)
  1935. | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
  1936. | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
  1937. | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
  1938. | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
  1939. | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
  1940. | (1 << DATA_IN_TOKEN_INTERRUPT)
  1941. , &ep->regs->ep_stat);
  1942. u.raw [0] = readl (&dev->usb->setup0123);
  1943. u.raw [1] = readl (&dev->usb->setup4567);
  1944. cpu_to_le32s (&u.raw [0]);
  1945. cpu_to_le32s (&u.raw [1]);
  1946. tmp = 0;
  1947. #define w_value le16_to_cpu(u.r.wValue)
  1948. #define w_index le16_to_cpu(u.r.wIndex)
  1949. #define w_length le16_to_cpu(u.r.wLength)
  1950. /* ack the irq */
  1951. writel (1 << SETUP_PACKET_INTERRUPT, &dev->regs->irqstat0);
  1952. stat ^= (1 << SETUP_PACKET_INTERRUPT);
  1953. /* watch control traffic at the token level, and force
  1954. * synchronization before letting the status stage happen.
  1955. * FIXME ignore tokens we'll NAK, until driver responds.
  1956. * that'll mean a lot less irqs for some drivers.
  1957. */
  1958. ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
  1959. if (ep->is_in) {
  1960. scratch = (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
  1961. | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
  1962. | (1 << DATA_IN_TOKEN_INTERRUPT);
  1963. stop_out_naking (ep);
  1964. } else
  1965. scratch = (1 << DATA_PACKET_RECEIVED_INTERRUPT)
  1966. | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
  1967. | (1 << DATA_IN_TOKEN_INTERRUPT);
  1968. writel (scratch, &dev->epregs [0].ep_irqenb);
  1969. /* we made the hardware handle most lowlevel requests;
  1970. * everything else goes uplevel to the gadget code.
  1971. */
  1972. ep->responded = 1;
  1973. switch (u.r.bRequest) {
  1974. case USB_REQ_GET_STATUS: {
  1975. struct net2280_ep *e;
  1976. __le32 status;
  1977. /* hw handles device and interface status */
  1978. if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
  1979. goto delegate;
  1980. if ((e = get_ep_by_addr (dev, w_index)) == 0
  1981. || w_length > 2)
  1982. goto do_stall;
  1983. if (readl (&e->regs->ep_rsp)
  1984. & (1 << SET_ENDPOINT_HALT))
  1985. status = cpu_to_le32 (1);
  1986. else
  1987. status = cpu_to_le32 (0);
  1988. /* don't bother with a request object! */
  1989. writel (0, &dev->epregs [0].ep_irqenb);
  1990. set_fifo_bytecount (ep, w_length);
  1991. writel ((__force u32)status, &dev->epregs [0].ep_data);
  1992. allow_status (ep);
  1993. VDEBUG (dev, "%s stat %02x\n", ep->ep.name, status);
  1994. goto next_endpoints;
  1995. }
  1996. break;
  1997. case USB_REQ_CLEAR_FEATURE: {
  1998. struct net2280_ep *e;
  1999. /* hw handles device features */
  2000. if (u.r.bRequestType != USB_RECIP_ENDPOINT)
  2001. goto delegate;
  2002. if (w_value != USB_ENDPOINT_HALT
  2003. || w_length != 0)
  2004. goto do_stall;
  2005. if ((e = get_ep_by_addr (dev, w_index)) == 0)
  2006. goto do_stall;
  2007. if (e->wedged) {
  2008. VDEBUG(dev, "%s wedged, halt not cleared\n",
  2009. ep->ep.name);
  2010. } else {
  2011. VDEBUG(dev, "%s clear halt\n", ep->ep.name);
  2012. clear_halt(e);
  2013. }
  2014. allow_status (ep);
  2015. goto next_endpoints;
  2016. }
  2017. break;
  2018. case USB_REQ_SET_FEATURE: {
  2019. struct net2280_ep *e;
  2020. /* hw handles device features */
  2021. if (u.r.bRequestType != USB_RECIP_ENDPOINT)
  2022. goto delegate;
  2023. if (w_value != USB_ENDPOINT_HALT
  2024. || w_length != 0)
  2025. goto do_stall;
  2026. if ((e = get_ep_by_addr (dev, w_index)) == 0)
  2027. goto do_stall;
  2028. if (e->ep.name == ep0name)
  2029. goto do_stall;
  2030. set_halt (e);
  2031. allow_status (ep);
  2032. VDEBUG (dev, "%s set halt\n", ep->ep.name);
  2033. goto next_endpoints;
  2034. }
  2035. break;
  2036. default:
  2037. delegate:
  2038. VDEBUG (dev, "setup %02x.%02x v%04x i%04x l%04x "
  2039. "ep_cfg %08x\n",
  2040. u.r.bRequestType, u.r.bRequest,
  2041. w_value, w_index, w_length,
  2042. readl (&ep->regs->ep_cfg));
  2043. ep->responded = 0;
  2044. spin_unlock (&dev->lock);
  2045. tmp = dev->driver->setup (&dev->gadget, &u.r);
  2046. spin_lock (&dev->lock);
  2047. }
  2048. /* stall ep0 on error */
  2049. if (tmp < 0) {
  2050. do_stall:
  2051. VDEBUG (dev, "req %02x.%02x protocol STALL; stat %d\n",
  2052. u.r.bRequestType, u.r.bRequest, tmp);
  2053. dev->protocol_stall = 1;
  2054. }
  2055. /* some in/out token irq should follow; maybe stall then.
  2056. * driver must queue a request (even zlp) or halt ep0
  2057. * before the host times out.
  2058. */
  2059. }
  2060. #undef w_value
  2061. #undef w_index
  2062. #undef w_length
  2063. next_endpoints:
  2064. /* endpoint data irq ? */
  2065. scratch = stat & 0x7f;
  2066. stat &= ~0x7f;
  2067. for (num = 0; scratch; num++) {
  2068. u32 t;
  2069. /* do this endpoint's FIFO and queue need tending? */
  2070. t = 1 << num;
  2071. if ((scratch & t) == 0)
  2072. continue;
  2073. scratch ^= t;
  2074. ep = &dev->ep [num];
  2075. handle_ep_small (ep);
  2076. }
  2077. if (stat)
  2078. DEBUG (dev, "unhandled irqstat0 %08x\n", stat);
  2079. }
  2080. #define DMA_INTERRUPTS ( \
  2081. (1 << DMA_D_INTERRUPT) \
  2082. | (1 << DMA_C_INTERRUPT) \
  2083. | (1 << DMA_B_INTERRUPT) \
  2084. | (1 << DMA_A_INTERRUPT))
  2085. #define PCI_ERROR_INTERRUPTS ( \
  2086. (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT) \
  2087. | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT) \
  2088. | (1 << PCI_RETRY_ABORT_INTERRUPT))
  2089. static void handle_stat1_irqs (struct net2280 *dev, u32 stat)
  2090. {
  2091. struct net2280_ep *ep;
  2092. u32 tmp, num, mask, scratch;
  2093. /* after disconnect there's nothing else to do! */
  2094. tmp = (1 << VBUS_INTERRUPT) | (1 << ROOT_PORT_RESET_INTERRUPT);
  2095. mask = (1 << HIGH_SPEED) | (1 << FULL_SPEED);
  2096. /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
  2097. * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRUPT set and
  2098. * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
  2099. * only indicates a change in the reset state).
  2100. */
  2101. if (stat & tmp) {
  2102. writel (tmp, &dev->regs->irqstat1);
  2103. if ((((stat & (1 << ROOT_PORT_RESET_INTERRUPT))
  2104. && ((readl (&dev->usb->usbstat) & mask)
  2105. == 0))
  2106. || ((readl (&dev->usb->usbctl)
  2107. & (1 << VBUS_PIN)) == 0)
  2108. ) && ( dev->gadget.speed != USB_SPEED_UNKNOWN)) {
  2109. DEBUG (dev, "disconnect %s\n",
  2110. dev->driver->driver.name);
  2111. stop_activity (dev, dev->driver);
  2112. ep0_start (dev);
  2113. return;
  2114. }
  2115. stat &= ~tmp;
  2116. /* vBUS can bounce ... one of many reasons to ignore the
  2117. * notion of hotplug events on bus connect/disconnect!
  2118. */
  2119. if (!stat)
  2120. return;
  2121. }
  2122. /* NOTE: chip stays in PCI D0 state for now, but it could
  2123. * enter D1 to save more power
  2124. */
  2125. tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
  2126. if (stat & tmp) {
  2127. writel (tmp, &dev->regs->irqstat1);
  2128. if (stat & (1 << SUSPEND_REQUEST_INTERRUPT)) {
  2129. if (dev->driver->suspend)
  2130. dev->driver->suspend (&dev->gadget);
  2131. if (!enable_suspend)
  2132. stat &= ~(1 << SUSPEND_REQUEST_INTERRUPT);
  2133. } else {
  2134. if (dev->driver->resume)
  2135. dev->driver->resume (&dev->gadget);
  2136. /* at high speed, note erratum 0133 */
  2137. }
  2138. stat &= ~tmp;
  2139. }
  2140. /* clear any other status/irqs */
  2141. if (stat)
  2142. writel (stat, &dev->regs->irqstat1);
  2143. /* some status we can just ignore */
  2144. if (dev->pdev->device == 0x2280)
  2145. stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
  2146. | (1 << SUSPEND_REQUEST_INTERRUPT)
  2147. | (1 << RESUME_INTERRUPT)
  2148. | (1 << SOF_INTERRUPT));
  2149. else
  2150. stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
  2151. | (1 << RESUME_INTERRUPT)
  2152. | (1 << SOF_DOWN_INTERRUPT)
  2153. | (1 << SOF_INTERRUPT));
  2154. if (!stat)
  2155. return;
  2156. // DEBUG (dev, "irqstat1 %08x\n", stat);
  2157. /* DMA status, for ep-{a,b,c,d} */
  2158. scratch = stat & DMA_INTERRUPTS;
  2159. stat &= ~DMA_INTERRUPTS;
  2160. scratch >>= 9;
  2161. for (num = 0; scratch; num++) {
  2162. struct net2280_dma_regs __iomem *dma;
  2163. tmp = 1 << num;
  2164. if ((tmp & scratch) == 0)
  2165. continue;
  2166. scratch ^= tmp;
  2167. ep = &dev->ep [num + 1];
  2168. dma = ep->dma;
  2169. if (!dma)
  2170. continue;
  2171. /* clear ep's dma status */
  2172. tmp = readl (&dma->dmastat);
  2173. writel (tmp, &dma->dmastat);
  2174. /* chaining should stop on abort, short OUT from fifo,
  2175. * or (stat0 codepath) short OUT transfer.
  2176. */
  2177. if (!use_dma_chaining) {
  2178. if ((tmp & (1 << DMA_TRANSACTION_DONE_INTERRUPT))
  2179. == 0) {
  2180. DEBUG (ep->dev, "%s no xact done? %08x\n",
  2181. ep->ep.name, tmp);
  2182. continue;
  2183. }
  2184. stop_dma (ep->dma);
  2185. }
  2186. /* OUT transfers terminate when the data from the
  2187. * host is in our memory. Process whatever's done.
  2188. * On this path, we know transfer's last packet wasn't
  2189. * less than req->length. NAK_OUT_PACKETS may be set,
  2190. * or the FIFO may already be holding new packets.
  2191. *
  2192. * IN transfers can linger in the FIFO for a very
  2193. * long time ... we ignore that for now, accounting
  2194. * precisely (like PIO does) needs per-packet irqs
  2195. */
  2196. scan_dma_completions (ep);
  2197. /* disable dma on inactive queues; else maybe restart */
  2198. if (list_empty (&ep->queue)) {
  2199. if (use_dma_chaining)
  2200. stop_dma (ep->dma);
  2201. } else {
  2202. tmp = readl (&dma->dmactl);
  2203. if (!use_dma_chaining
  2204. || (tmp & (1 << DMA_ENABLE)) == 0)
  2205. restart_dma (ep);
  2206. else if (ep->is_in && use_dma_chaining) {
  2207. struct net2280_request *req;
  2208. __le32 dmacount;
  2209. /* the descriptor at the head of the chain
  2210. * may still have VALID_BIT clear; that's
  2211. * used to trigger changing DMA_FIFO_VALIDATE
  2212. * (affects automagic zlp writes).
  2213. */
  2214. req = list_entry (ep->queue.next,
  2215. struct net2280_request, queue);
  2216. dmacount = req->td->dmacount;
  2217. dmacount &= cpu_to_le32 (
  2218. (1 << VALID_BIT)
  2219. | DMA_BYTE_COUNT_MASK);
  2220. if (dmacount && (dmacount & valid_bit) == 0)
  2221. restart_dma (ep);
  2222. }
  2223. }
  2224. ep->irqs++;
  2225. }
  2226. /* NOTE: there are other PCI errors we might usefully notice.
  2227. * if they appear very often, here's where to try recovering.
  2228. */
  2229. if (stat & PCI_ERROR_INTERRUPTS) {
  2230. ERROR (dev, "pci dma error; stat %08x\n", stat);
  2231. stat &= ~PCI_ERROR_INTERRUPTS;
  2232. /* these are fatal errors, but "maybe" they won't
  2233. * happen again ...
  2234. */
  2235. stop_activity (dev, dev->driver);
  2236. ep0_start (dev);
  2237. stat = 0;
  2238. }
  2239. if (stat)
  2240. DEBUG (dev, "unhandled irqstat1 %08x\n", stat);
  2241. }
  2242. static irqreturn_t net2280_irq (int irq, void *_dev)
  2243. {
  2244. struct net2280 *dev = _dev;
  2245. /* shared interrupt, not ours */
  2246. if (!(readl(&dev->regs->irqstat0) & (1 << INTA_ASSERTED)))
  2247. return IRQ_NONE;
  2248. spin_lock (&dev->lock);
  2249. /* handle disconnect, dma, and more */
  2250. handle_stat1_irqs (dev, readl (&dev->regs->irqstat1));
  2251. /* control requests and PIO */
  2252. handle_stat0_irqs (dev, readl (&dev->regs->irqstat0));
  2253. spin_unlock (&dev->lock);
  2254. return IRQ_HANDLED;
  2255. }
  2256. /*-------------------------------------------------------------------------*/
  2257. static void gadget_release (struct device *_dev)
  2258. {
  2259. struct net2280 *dev = dev_get_drvdata (_dev);
  2260. kfree (dev);
  2261. }
  2262. /* tear down the binding between this driver and the pci device */
  2263. static void net2280_remove (struct pci_dev *pdev)
  2264. {
  2265. struct net2280 *dev = pci_get_drvdata (pdev);
  2266. usb_del_gadget_udc(&dev->gadget);
  2267. BUG_ON(dev->driver);
  2268. /* then clean up the resources we allocated during probe() */
  2269. net2280_led_shutdown (dev);
  2270. if (dev->requests) {
  2271. int i;
  2272. for (i = 1; i < 5; i++) {
  2273. if (!dev->ep [i].dummy)
  2274. continue;
  2275. pci_pool_free (dev->requests, dev->ep [i].dummy,
  2276. dev->ep [i].td_dma);
  2277. }
  2278. pci_pool_destroy (dev->requests);
  2279. }
  2280. if (dev->got_irq)
  2281. free_irq (pdev->irq, dev);
  2282. if (dev->regs)
  2283. iounmap (dev->regs);
  2284. if (dev->region)
  2285. release_mem_region (pci_resource_start (pdev, 0),
  2286. pci_resource_len (pdev, 0));
  2287. if (dev->enabled)
  2288. pci_disable_device (pdev);
  2289. device_unregister (&dev->gadget.dev);
  2290. device_remove_file (&pdev->dev, &dev_attr_registers);
  2291. pci_set_drvdata (pdev, NULL);
  2292. INFO (dev, "unbind\n");
  2293. }
  2294. /* wrap this driver around the specified device, but
  2295. * don't respond over USB until a gadget driver binds to us.
  2296. */
  2297. static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
  2298. {
  2299. struct net2280 *dev;
  2300. unsigned long resource, len;
  2301. void __iomem *base = NULL;
  2302. int retval, i;
  2303. /* alloc, and start init */
  2304. dev = kzalloc (sizeof *dev, GFP_KERNEL);
  2305. if (dev == NULL){
  2306. retval = -ENOMEM;
  2307. goto done;
  2308. }
  2309. pci_set_drvdata (pdev, dev);
  2310. spin_lock_init (&dev->lock);
  2311. dev->pdev = pdev;
  2312. dev->gadget.ops = &net2280_ops;
  2313. dev->gadget.max_speed = USB_SPEED_HIGH;
  2314. /* the "gadget" abstracts/virtualizes the controller */
  2315. dev_set_name(&dev->gadget.dev, "gadget");
  2316. dev->gadget.dev.parent = &pdev->dev;
  2317. dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
  2318. dev->gadget.dev.release = gadget_release;
  2319. dev->gadget.name = driver_name;
  2320. /* now all the pci goodies ... */
  2321. if (pci_enable_device (pdev) < 0) {
  2322. retval = -ENODEV;
  2323. goto done;
  2324. }
  2325. dev->enabled = 1;
  2326. /* BAR 0 holds all the registers
  2327. * BAR 1 is 8051 memory; unused here (note erratum 0103)
  2328. * BAR 2 is fifo memory; unused here
  2329. */
  2330. resource = pci_resource_start (pdev, 0);
  2331. len = pci_resource_len (pdev, 0);
  2332. if (!request_mem_region (resource, len, driver_name)) {
  2333. DEBUG (dev, "controller already in use\n");
  2334. retval = -EBUSY;
  2335. goto done;
  2336. }
  2337. dev->region = 1;
  2338. /* FIXME provide firmware download interface to put
  2339. * 8051 code into the chip, e.g. to turn on PCI PM.
  2340. */
  2341. base = ioremap_nocache (resource, len);
  2342. if (base == NULL) {
  2343. DEBUG (dev, "can't map memory\n");
  2344. retval = -EFAULT;
  2345. goto done;
  2346. }
  2347. dev->regs = (struct net2280_regs __iomem *) base;
  2348. dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
  2349. dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
  2350. dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
  2351. dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
  2352. dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
  2353. /* put into initial config, link up all endpoints */
  2354. writel (0, &dev->usb->usbctl);
  2355. usb_reset (dev);
  2356. usb_reinit (dev);
  2357. /* irq setup after old hardware is cleaned up */
  2358. if (!pdev->irq) {
  2359. ERROR (dev, "No IRQ. Check PCI setup!\n");
  2360. retval = -ENODEV;
  2361. goto done;
  2362. }
  2363. if (request_irq (pdev->irq, net2280_irq, IRQF_SHARED, driver_name, dev)
  2364. != 0) {
  2365. ERROR (dev, "request interrupt %d failed\n", pdev->irq);
  2366. retval = -EBUSY;
  2367. goto done;
  2368. }
  2369. dev->got_irq = 1;
  2370. /* DMA setup */
  2371. /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
  2372. dev->requests = pci_pool_create ("requests", pdev,
  2373. sizeof (struct net2280_dma),
  2374. 0 /* no alignment requirements */,
  2375. 0 /* or page-crossing issues */);
  2376. if (!dev->requests) {
  2377. DEBUG (dev, "can't get request pool\n");
  2378. retval = -ENOMEM;
  2379. goto done;
  2380. }
  2381. for (i = 1; i < 5; i++) {
  2382. struct net2280_dma *td;
  2383. td = pci_pool_alloc (dev->requests, GFP_KERNEL,
  2384. &dev->ep [i].td_dma);
  2385. if (!td) {
  2386. DEBUG (dev, "can't get dummy %d\n", i);
  2387. retval = -ENOMEM;
  2388. goto done;
  2389. }
  2390. td->dmacount = 0; /* not VALID */
  2391. td->dmaaddr = cpu_to_le32 (DMA_ADDR_INVALID);
  2392. td->dmadesc = td->dmaaddr;
  2393. dev->ep [i].dummy = td;
  2394. }
  2395. /* enable lower-overhead pci memory bursts during DMA */
  2396. writel ( (1 << DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE)
  2397. // 256 write retries may not be enough...
  2398. // | (1 << PCI_RETRY_ABORT_ENABLE)
  2399. | (1 << DMA_READ_MULTIPLE_ENABLE)
  2400. | (1 << DMA_READ_LINE_ENABLE)
  2401. , &dev->pci->pcimstctl);
  2402. /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
  2403. pci_set_master (pdev);
  2404. pci_try_set_mwi (pdev);
  2405. /* ... also flushes any posted pci writes */
  2406. dev->chiprev = get_idx_reg (dev->regs, REG_CHIPREV) & 0xffff;
  2407. /* done */
  2408. INFO (dev, "%s\n", driver_desc);
  2409. INFO (dev, "irq %d, pci mem %p, chip rev %04x\n",
  2410. pdev->irq, base, dev->chiprev);
  2411. INFO (dev, "version: " DRIVER_VERSION "; dma %s\n",
  2412. use_dma
  2413. ? (use_dma_chaining ? "chaining" : "enabled")
  2414. : "disabled");
  2415. retval = device_register (&dev->gadget.dev);
  2416. if (retval) goto done;
  2417. retval = device_create_file (&pdev->dev, &dev_attr_registers);
  2418. if (retval) goto done;
  2419. retval = usb_add_gadget_udc(&pdev->dev, &dev->gadget);
  2420. if (retval)
  2421. goto done;
  2422. return 0;
  2423. done:
  2424. if (dev)
  2425. net2280_remove (pdev);
  2426. return retval;
  2427. }
  2428. /* make sure the board is quiescent; otherwise it will continue
  2429. * generating IRQs across the upcoming reboot.
  2430. */
  2431. static void net2280_shutdown (struct pci_dev *pdev)
  2432. {
  2433. struct net2280 *dev = pci_get_drvdata (pdev);
  2434. /* disable IRQs */
  2435. writel (0, &dev->regs->pciirqenb0);
  2436. writel (0, &dev->regs->pciirqenb1);
  2437. /* disable the pullup so the host will think we're gone */
  2438. writel (0, &dev->usb->usbctl);
  2439. }
  2440. /*-------------------------------------------------------------------------*/
  2441. static const struct pci_device_id pci_ids [] = { {
  2442. .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
  2443. .class_mask = ~0,
  2444. .vendor = 0x17cc,
  2445. .device = 0x2280,
  2446. .subvendor = PCI_ANY_ID,
  2447. .subdevice = PCI_ANY_ID,
  2448. }, {
  2449. .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
  2450. .class_mask = ~0,
  2451. .vendor = 0x17cc,
  2452. .device = 0x2282,
  2453. .subvendor = PCI_ANY_ID,
  2454. .subdevice = PCI_ANY_ID,
  2455. }, { /* end: all zeroes */ }
  2456. };
  2457. MODULE_DEVICE_TABLE (pci, pci_ids);
  2458. /* pci driver glue; this is a "new style" PCI driver module */
  2459. static struct pci_driver net2280_pci_driver = {
  2460. .name = (char *) driver_name,
  2461. .id_table = pci_ids,
  2462. .probe = net2280_probe,
  2463. .remove = net2280_remove,
  2464. .shutdown = net2280_shutdown,
  2465. /* FIXME add power management support */
  2466. };
  2467. MODULE_DESCRIPTION (DRIVER_DESC);
  2468. MODULE_AUTHOR ("David Brownell");
  2469. MODULE_LICENSE ("GPL");
  2470. static int __init init (void)
  2471. {
  2472. if (!use_dma)
  2473. use_dma_chaining = 0;
  2474. return pci_register_driver (&net2280_pci_driver);
  2475. }
  2476. module_init (init);
  2477. static void __exit cleanup (void)
  2478. {
  2479. pci_unregister_driver (&net2280_pci_driver);
  2480. }
  2481. module_exit (cleanup);