net2280.c 76 KB

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