net2280.c 75 KB

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