net2280.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645
  1. /*
  2. * Driver for the PLX NET2280 USB device controller.
  3. * Specs and errata are available from <http://www.plxtech.com>.
  4. *
  5. * PLX Technology Inc. (formerly NetChip Technology) supported the
  6. * development of this driver.
  7. *
  8. *
  9. * CODE STATUS HIGHLIGHTS
  10. *
  11. * This driver should work well with most "gadget" drivers, including
  12. * the Mass Storage, Serial, and Ethernet/RNDIS gadget drivers
  13. * as well as Gadget Zero and Gadgetfs.
  14. *
  15. * DMA is enabled by default.
  16. *
  17. * MSI is enabled by default. The legacy IRQ is used if MSI couldn't
  18. * be enabled.
  19. *
  20. * Note that almost all the errata workarounds here are only needed for
  21. * rev1 chips. Rev1a silicon (0110) fixes almost all of them.
  22. */
  23. /*
  24. * Copyright (C) 2003 David Brownell
  25. * Copyright (C) 2003-2005 PLX Technology, Inc.
  26. * Copyright (C) 2014 Ricardo Ribalda - Qtechnology/AS
  27. *
  28. * Modified Seth Levy 2005 PLX Technology, Inc. to provide compatibility
  29. * with 2282 chip
  30. *
  31. * Modified Ricardo Ribalda Qtechnology AS to provide compatibility
  32. * with usb 338x chip. Based on PLX driver
  33. *
  34. * This program is free software; you can redistribute it and/or modify
  35. * it under the terms of the GNU General Public License as published by
  36. * the Free Software Foundation; either version 2 of the License, or
  37. * (at your option) any later version.
  38. */
  39. #include <linux/module.h>
  40. #include <linux/pci.h>
  41. #include <linux/dma-mapping.h>
  42. #include <linux/kernel.h>
  43. #include <linux/delay.h>
  44. #include <linux/ioport.h>
  45. #include <linux/slab.h>
  46. #include <linux/errno.h>
  47. #include <linux/init.h>
  48. #include <linux/timer.h>
  49. #include <linux/list.h>
  50. #include <linux/interrupt.h>
  51. #include <linux/moduleparam.h>
  52. #include <linux/device.h>
  53. #include <linux/usb/ch9.h>
  54. #include <linux/usb/gadget.h>
  55. #include <linux/prefetch.h>
  56. #include <linux/io.h>
  57. #include <asm/byteorder.h>
  58. #include <asm/irq.h>
  59. #include <asm/unaligned.h>
  60. #define DRIVER_DESC "PLX NET228x/USB338x USB Peripheral Controller"
  61. #define DRIVER_VERSION "2005 Sept 27/v3.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 u32 ep_bit[9] = { 0, 17, 2, 19, 4, 1, 18, 3, 20 };
  67. static const char ep0name[] = "ep0";
  68. static const char *const ep_name[] = {
  69. ep0name,
  70. "ep-a", "ep-b", "ep-c", "ep-d",
  71. "ep-e", "ep-f", "ep-g", "ep-h",
  72. };
  73. /* Endpoint names for usb3380 advance mode */
  74. static const char *const ep_name_adv[] = {
  75. ep0name,
  76. "ep1in", "ep2out", "ep3in", "ep4out",
  77. "ep1out", "ep2in", "ep3out", "ep4in",
  78. };
  79. /* mode 0 == ep-{a,b,c,d} 1K fifo each
  80. * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
  81. * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
  82. */
  83. static ushort fifo_mode;
  84. /* "modprobe net2280 fifo_mode=1" etc */
  85. module_param(fifo_mode, ushort, 0644);
  86. /* enable_suspend -- When enabled, the driver will respond to
  87. * USB suspend requests by powering down the NET2280. Otherwise,
  88. * USB suspend requests will be ignored. This is acceptable for
  89. * self-powered devices
  90. */
  91. static bool enable_suspend;
  92. /* "modprobe net2280 enable_suspend=1" etc */
  93. module_param(enable_suspend, bool, 0444);
  94. #define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
  95. static char *type_string(u8 bmAttributes)
  96. {
  97. switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
  98. case USB_ENDPOINT_XFER_BULK: return "bulk";
  99. case USB_ENDPOINT_XFER_ISOC: return "iso";
  100. case USB_ENDPOINT_XFER_INT: return "intr";
  101. }
  102. return "control";
  103. }
  104. #include "net2280.h"
  105. #define valid_bit cpu_to_le32(BIT(VALID_BIT))
  106. #define dma_done_ie cpu_to_le32(BIT(DMA_DONE_INTERRUPT_ENABLE))
  107. /*-------------------------------------------------------------------------*/
  108. static inline void enable_pciirqenb(struct net2280_ep *ep)
  109. {
  110. u32 tmp = readl(&ep->dev->regs->pciirqenb0);
  111. if (ep->dev->quirks & PLX_LEGACY)
  112. tmp |= BIT(ep->num);
  113. else
  114. tmp |= BIT(ep_bit[ep->num]);
  115. writel(tmp, &ep->dev->regs->pciirqenb0);
  116. return;
  117. }
  118. static int
  119. net2280_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
  120. {
  121. struct net2280 *dev;
  122. struct net2280_ep *ep;
  123. u32 max, tmp;
  124. unsigned long flags;
  125. static const u32 ep_key[9] = { 1, 0, 1, 0, 1, 1, 0, 1, 0 };
  126. int ret = 0;
  127. ep = container_of(_ep, struct net2280_ep, ep);
  128. if (!_ep || !desc || ep->desc || _ep->name == ep0name ||
  129. desc->bDescriptorType != USB_DT_ENDPOINT) {
  130. pr_err("%s: failed at line=%d\n", __func__, __LINE__);
  131. return -EINVAL;
  132. }
  133. dev = ep->dev;
  134. if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
  135. ret = -ESHUTDOWN;
  136. goto print_err;
  137. }
  138. /* erratum 0119 workaround ties up an endpoint number */
  139. if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE) {
  140. ret = -EDOM;
  141. goto print_err;
  142. }
  143. if (dev->quirks & PLX_SUPERSPEED) {
  144. if ((desc->bEndpointAddress & 0x0f) >= 0x0c) {
  145. ret = -EDOM;
  146. goto print_err;
  147. }
  148. ep->is_in = !!usb_endpoint_dir_in(desc);
  149. if (dev->enhanced_mode && ep->is_in && ep_key[ep->num]) {
  150. ret = -EINVAL;
  151. goto print_err;
  152. }
  153. }
  154. /* sanity check ep-e/ep-f since their fifos are small */
  155. max = usb_endpoint_maxp(desc) & 0x1fff;
  156. if (ep->num > 4 && max > 64 && (dev->quirks & PLX_LEGACY)) {
  157. ret = -ERANGE;
  158. goto print_err;
  159. }
  160. spin_lock_irqsave(&dev->lock, flags);
  161. _ep->maxpacket = max & 0x7ff;
  162. ep->desc = desc;
  163. /* ep_reset() has already been called */
  164. ep->stopped = 0;
  165. ep->wedged = 0;
  166. ep->out_overflow = 0;
  167. /* set speed-dependent max packet; may kick in high bandwidth */
  168. set_max_speed(ep, max);
  169. /* set type, direction, address; reset fifo counters */
  170. writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat);
  171. tmp = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
  172. if (tmp == USB_ENDPOINT_XFER_INT) {
  173. /* erratum 0105 workaround prevents hs NYET */
  174. if (dev->chiprev == 0100 &&
  175. dev->gadget.speed == USB_SPEED_HIGH &&
  176. !(desc->bEndpointAddress & USB_DIR_IN))
  177. writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE),
  178. &ep->regs->ep_rsp);
  179. } else if (tmp == USB_ENDPOINT_XFER_BULK) {
  180. /* catch some particularly blatant driver bugs */
  181. if ((dev->gadget.speed == USB_SPEED_SUPER && max != 1024) ||
  182. (dev->gadget.speed == USB_SPEED_HIGH && max != 512) ||
  183. (dev->gadget.speed == USB_SPEED_FULL && max > 64)) {
  184. spin_unlock_irqrestore(&dev->lock, flags);
  185. ret = -ERANGE;
  186. goto print_err;
  187. }
  188. }
  189. ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC);
  190. /* Enable this endpoint */
  191. if (dev->quirks & PLX_LEGACY) {
  192. tmp <<= ENDPOINT_TYPE;
  193. tmp |= desc->bEndpointAddress;
  194. /* default full fifo lines */
  195. tmp |= (4 << ENDPOINT_BYTE_COUNT);
  196. tmp |= BIT(ENDPOINT_ENABLE);
  197. ep->is_in = (tmp & USB_DIR_IN) != 0;
  198. } else {
  199. /* In Legacy mode, only OUT endpoints are used */
  200. if (dev->enhanced_mode && ep->is_in) {
  201. tmp <<= IN_ENDPOINT_TYPE;
  202. tmp |= BIT(IN_ENDPOINT_ENABLE);
  203. /* Not applicable to Legacy */
  204. tmp |= BIT(ENDPOINT_DIRECTION);
  205. } else {
  206. tmp <<= OUT_ENDPOINT_TYPE;
  207. tmp |= BIT(OUT_ENDPOINT_ENABLE);
  208. tmp |= (ep->is_in << ENDPOINT_DIRECTION);
  209. }
  210. tmp |= usb_endpoint_num(desc);
  211. tmp |= (ep->ep.maxburst << MAX_BURST_SIZE);
  212. }
  213. /* Make sure all the registers are written before ep_rsp*/
  214. wmb();
  215. /* for OUT transfers, block the rx fifo until a read is posted */
  216. if (!ep->is_in)
  217. writel(BIT(SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
  218. else if (!(dev->quirks & PLX_2280)) {
  219. /* Added for 2282, Don't use nak packets on an in endpoint,
  220. * this was ignored on 2280
  221. */
  222. writel(BIT(CLEAR_NAK_OUT_PACKETS) |
  223. BIT(CLEAR_NAK_OUT_PACKETS_MODE), &ep->regs->ep_rsp);
  224. }
  225. writel(tmp, &ep->cfg->ep_cfg);
  226. /* enable irqs */
  227. if (!ep->dma) { /* pio, per-packet */
  228. enable_pciirqenb(ep);
  229. tmp = BIT(DATA_PACKET_RECEIVED_INTERRUPT_ENABLE) |
  230. BIT(DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE);
  231. if (dev->quirks & PLX_2280)
  232. tmp |= readl(&ep->regs->ep_irqenb);
  233. writel(tmp, &ep->regs->ep_irqenb);
  234. } else { /* dma, per-request */
  235. tmp = BIT((8 + ep->num)); /* completion */
  236. tmp |= readl(&dev->regs->pciirqenb1);
  237. writel(tmp, &dev->regs->pciirqenb1);
  238. /* for short OUT transfers, dma completions can't
  239. * advance the queue; do it pio-style, by hand.
  240. * NOTE erratum 0112 workaround #2
  241. */
  242. if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
  243. tmp = BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
  244. writel(tmp, &ep->regs->ep_irqenb);
  245. enable_pciirqenb(ep);
  246. }
  247. }
  248. tmp = desc->bEndpointAddress;
  249. ep_dbg(dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
  250. _ep->name, tmp & 0x0f, DIR_STRING(tmp),
  251. type_string(desc->bmAttributes),
  252. ep->dma ? "dma" : "pio", max);
  253. /* pci writes may still be posted */
  254. spin_unlock_irqrestore(&dev->lock, flags);
  255. return ret;
  256. print_err:
  257. dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, ret);
  258. return ret;
  259. }
  260. static int handshake(u32 __iomem *ptr, u32 mask, u32 done, int usec)
  261. {
  262. u32 result;
  263. do {
  264. result = readl(ptr);
  265. if (result == ~(u32)0) /* "device unplugged" */
  266. return -ENODEV;
  267. result &= mask;
  268. if (result == done)
  269. return 0;
  270. udelay(1);
  271. usec--;
  272. } while (usec > 0);
  273. return -ETIMEDOUT;
  274. }
  275. static const struct usb_ep_ops net2280_ep_ops;
  276. static void ep_reset_228x(struct net2280_regs __iomem *regs,
  277. struct net2280_ep *ep)
  278. {
  279. u32 tmp;
  280. ep->desc = NULL;
  281. INIT_LIST_HEAD(&ep->queue);
  282. usb_ep_set_maxpacket_limit(&ep->ep, ~0);
  283. ep->ep.ops = &net2280_ep_ops;
  284. /* disable the dma, irqs, endpoint... */
  285. if (ep->dma) {
  286. writel(0, &ep->dma->dmactl);
  287. writel(BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
  288. BIT(DMA_TRANSACTION_DONE_INTERRUPT) |
  289. BIT(DMA_ABORT),
  290. &ep->dma->dmastat);
  291. tmp = readl(&regs->pciirqenb0);
  292. tmp &= ~BIT(ep->num);
  293. writel(tmp, &regs->pciirqenb0);
  294. } else {
  295. tmp = readl(&regs->pciirqenb1);
  296. tmp &= ~BIT((8 + ep->num)); /* completion */
  297. writel(tmp, &regs->pciirqenb1);
  298. }
  299. writel(0, &ep->regs->ep_irqenb);
  300. /* init to our chosen defaults, notably so that we NAK OUT
  301. * packets until the driver queues a read (+note erratum 0112)
  302. */
  303. if (!ep->is_in || (ep->dev->quirks & PLX_2280)) {
  304. tmp = BIT(SET_NAK_OUT_PACKETS_MODE) |
  305. BIT(SET_NAK_OUT_PACKETS) |
  306. BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
  307. BIT(CLEAR_INTERRUPT_MODE);
  308. } else {
  309. /* added for 2282 */
  310. tmp = BIT(CLEAR_NAK_OUT_PACKETS_MODE) |
  311. BIT(CLEAR_NAK_OUT_PACKETS) |
  312. BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
  313. BIT(CLEAR_INTERRUPT_MODE);
  314. }
  315. if (ep->num != 0) {
  316. tmp |= BIT(CLEAR_ENDPOINT_TOGGLE) |
  317. BIT(CLEAR_ENDPOINT_HALT);
  318. }
  319. writel(tmp, &ep->regs->ep_rsp);
  320. /* scrub most status bits, and flush any fifo state */
  321. if (ep->dev->quirks & PLX_2280)
  322. tmp = BIT(FIFO_OVERFLOW) |
  323. BIT(FIFO_UNDERFLOW);
  324. else
  325. tmp = 0;
  326. writel(tmp | BIT(TIMEOUT) |
  327. BIT(USB_STALL_SENT) |
  328. BIT(USB_IN_NAK_SENT) |
  329. BIT(USB_IN_ACK_RCVD) |
  330. BIT(USB_OUT_PING_NAK_SENT) |
  331. BIT(USB_OUT_ACK_SENT) |
  332. BIT(FIFO_FLUSH) |
  333. BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
  334. BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
  335. BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
  336. BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
  337. BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
  338. BIT(DATA_IN_TOKEN_INTERRUPT),
  339. &ep->regs->ep_stat);
  340. /* fifo size is handled separately */
  341. }
  342. static void ep_reset_338x(struct net2280_regs __iomem *regs,
  343. struct net2280_ep *ep)
  344. {
  345. u32 tmp, dmastat;
  346. ep->desc = NULL;
  347. INIT_LIST_HEAD(&ep->queue);
  348. usb_ep_set_maxpacket_limit(&ep->ep, ~0);
  349. ep->ep.ops = &net2280_ep_ops;
  350. /* disable the dma, irqs, endpoint... */
  351. if (ep->dma) {
  352. writel(0, &ep->dma->dmactl);
  353. writel(BIT(DMA_ABORT_DONE_INTERRUPT) |
  354. BIT(DMA_PAUSE_DONE_INTERRUPT) |
  355. BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
  356. BIT(DMA_TRANSACTION_DONE_INTERRUPT),
  357. /* | BIT(DMA_ABORT), */
  358. &ep->dma->dmastat);
  359. dmastat = readl(&ep->dma->dmastat);
  360. if (dmastat == 0x5002) {
  361. ep_warn(ep->dev, "The dmastat return = %x!!\n",
  362. dmastat);
  363. writel(0x5a, &ep->dma->dmastat);
  364. }
  365. tmp = readl(&regs->pciirqenb0);
  366. tmp &= ~BIT(ep_bit[ep->num]);
  367. writel(tmp, &regs->pciirqenb0);
  368. } else {
  369. if (ep->num < 5) {
  370. tmp = readl(&regs->pciirqenb1);
  371. tmp &= ~BIT((8 + ep->num)); /* completion */
  372. writel(tmp, &regs->pciirqenb1);
  373. }
  374. }
  375. writel(0, &ep->regs->ep_irqenb);
  376. writel(BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
  377. BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
  378. BIT(FIFO_OVERFLOW) |
  379. BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
  380. BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
  381. BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
  382. BIT(DATA_IN_TOKEN_INTERRUPT), &ep->regs->ep_stat);
  383. }
  384. static void nuke(struct net2280_ep *);
  385. static int net2280_disable(struct usb_ep *_ep)
  386. {
  387. struct net2280_ep *ep;
  388. unsigned long flags;
  389. ep = container_of(_ep, struct net2280_ep, ep);
  390. if (!_ep || !ep->desc || _ep->name == ep0name) {
  391. pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
  392. return -EINVAL;
  393. }
  394. spin_lock_irqsave(&ep->dev->lock, flags);
  395. nuke(ep);
  396. if (ep->dev->quirks & PLX_SUPERSPEED)
  397. ep_reset_338x(ep->dev->regs, ep);
  398. else
  399. ep_reset_228x(ep->dev->regs, ep);
  400. ep_vdbg(ep->dev, "disabled %s %s\n",
  401. ep->dma ? "dma" : "pio", _ep->name);
  402. /* synch memory views with the device */
  403. (void)readl(&ep->cfg->ep_cfg);
  404. if (!ep->dma && ep->num >= 1 && ep->num <= 4)
  405. ep->dma = &ep->dev->dma[ep->num - 1];
  406. spin_unlock_irqrestore(&ep->dev->lock, flags);
  407. return 0;
  408. }
  409. /*-------------------------------------------------------------------------*/
  410. static struct usb_request
  411. *net2280_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
  412. {
  413. struct net2280_ep *ep;
  414. struct net2280_request *req;
  415. if (!_ep) {
  416. pr_err("%s: Invalid ep\n", __func__);
  417. return NULL;
  418. }
  419. ep = container_of(_ep, struct net2280_ep, ep);
  420. req = kzalloc(sizeof(*req), gfp_flags);
  421. if (!req)
  422. return NULL;
  423. INIT_LIST_HEAD(&req->queue);
  424. /* this dma descriptor may be swapped with the previous dummy */
  425. if (ep->dma) {
  426. struct net2280_dma *td;
  427. td = pci_pool_alloc(ep->dev->requests, gfp_flags,
  428. &req->td_dma);
  429. if (!td) {
  430. kfree(req);
  431. return NULL;
  432. }
  433. td->dmacount = 0; /* not VALID */
  434. td->dmadesc = td->dmaaddr;
  435. req->td = td;
  436. }
  437. return &req->req;
  438. }
  439. static void net2280_free_request(struct usb_ep *_ep, struct usb_request *_req)
  440. {
  441. struct net2280_ep *ep;
  442. struct net2280_request *req;
  443. ep = container_of(_ep, struct net2280_ep, ep);
  444. if (!_ep || !_req) {
  445. dev_err(&ep->dev->pdev->dev, "%s: Inavlid ep=%p or req=%p\n",
  446. __func__, _ep, _req);
  447. return;
  448. }
  449. req = container_of(_req, struct net2280_request, req);
  450. WARN_ON(!list_empty(&req->queue));
  451. if (req->td)
  452. pci_pool_free(ep->dev->requests, req->td, req->td_dma);
  453. kfree(req);
  454. }
  455. /*-------------------------------------------------------------------------*/
  456. /* load a packet into the fifo we use for usb IN transfers.
  457. * works for all endpoints.
  458. *
  459. * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
  460. * at a time, but this code is simpler because it knows it only writes
  461. * one packet. ep-a..ep-d should use dma instead.
  462. */
  463. static void write_fifo(struct net2280_ep *ep, struct usb_request *req)
  464. {
  465. struct net2280_ep_regs __iomem *regs = ep->regs;
  466. u8 *buf;
  467. u32 tmp;
  468. unsigned count, total;
  469. /* INVARIANT: fifo is currently empty. (testable) */
  470. if (req) {
  471. buf = req->buf + req->actual;
  472. prefetch(buf);
  473. total = req->length - req->actual;
  474. } else {
  475. total = 0;
  476. buf = NULL;
  477. }
  478. /* write just one packet at a time */
  479. count = ep->ep.maxpacket;
  480. if (count > total) /* min() cannot be used on a bitfield */
  481. count = total;
  482. ep_vdbg(ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
  483. ep->ep.name, count,
  484. (count != ep->ep.maxpacket) ? " (short)" : "",
  485. req);
  486. while (count >= 4) {
  487. /* NOTE be careful if you try to align these. fifo lines
  488. * should normally be full (4 bytes) and successive partial
  489. * lines are ok only in certain cases.
  490. */
  491. tmp = get_unaligned((u32 *)buf);
  492. cpu_to_le32s(&tmp);
  493. writel(tmp, &regs->ep_data);
  494. buf += 4;
  495. count -= 4;
  496. }
  497. /* last fifo entry is "short" unless we wrote a full packet.
  498. * also explicitly validate last word in (periodic) transfers
  499. * when maxpacket is not a multiple of 4 bytes.
  500. */
  501. if (count || total < ep->ep.maxpacket) {
  502. tmp = count ? get_unaligned((u32 *)buf) : count;
  503. cpu_to_le32s(&tmp);
  504. set_fifo_bytecount(ep, count & 0x03);
  505. writel(tmp, &regs->ep_data);
  506. }
  507. /* pci writes may still be posted */
  508. }
  509. /* work around erratum 0106: PCI and USB race over the OUT fifo.
  510. * caller guarantees chiprev 0100, out endpoint is NAKing, and
  511. * there's no real data in the fifo.
  512. *
  513. * NOTE: also used in cases where that erratum doesn't apply:
  514. * where the host wrote "too much" data to us.
  515. */
  516. static void out_flush(struct net2280_ep *ep)
  517. {
  518. u32 __iomem *statp;
  519. u32 tmp;
  520. statp = &ep->regs->ep_stat;
  521. tmp = readl(statp);
  522. if (tmp & BIT(NAK_OUT_PACKETS)) {
  523. ep_dbg(ep->dev, "%s %s %08x !NAK\n",
  524. ep->ep.name, __func__, tmp);
  525. writel(BIT(SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
  526. }
  527. writel(BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
  528. BIT(DATA_PACKET_RECEIVED_INTERRUPT),
  529. statp);
  530. writel(BIT(FIFO_FLUSH), statp);
  531. /* Make sure that stap is written */
  532. mb();
  533. tmp = readl(statp);
  534. if (tmp & BIT(DATA_OUT_PING_TOKEN_INTERRUPT) &&
  535. /* high speed did bulk NYET; fifo isn't filling */
  536. ep->dev->gadget.speed == USB_SPEED_FULL) {
  537. unsigned usec;
  538. usec = 50; /* 64 byte bulk/interrupt */
  539. handshake(statp, BIT(USB_OUT_PING_NAK_SENT),
  540. BIT(USB_OUT_PING_NAK_SENT), usec);
  541. /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
  542. }
  543. }
  544. /* unload packet(s) from the fifo we use for usb OUT transfers.
  545. * returns true iff the request completed, because of short packet
  546. * or the request buffer having filled with full packets.
  547. *
  548. * for ep-a..ep-d this will read multiple packets out when they
  549. * have been accepted.
  550. */
  551. static int read_fifo(struct net2280_ep *ep, struct net2280_request *req)
  552. {
  553. struct net2280_ep_regs __iomem *regs = ep->regs;
  554. u8 *buf = req->req.buf + req->req.actual;
  555. unsigned count, tmp, is_short;
  556. unsigned cleanup = 0, prevent = 0;
  557. /* erratum 0106 ... packets coming in during fifo reads might
  558. * be incompletely rejected. not all cases have workarounds.
  559. */
  560. if (ep->dev->chiprev == 0x0100 &&
  561. ep->dev->gadget.speed == USB_SPEED_FULL) {
  562. udelay(1);
  563. tmp = readl(&ep->regs->ep_stat);
  564. if ((tmp & BIT(NAK_OUT_PACKETS)))
  565. cleanup = 1;
  566. else if ((tmp & BIT(FIFO_FULL))) {
  567. start_out_naking(ep);
  568. prevent = 1;
  569. }
  570. /* else: hope we don't see the problem */
  571. }
  572. /* never overflow the rx buffer. the fifo reads packets until
  573. * it sees a short one; we might not be ready for them all.
  574. */
  575. prefetchw(buf);
  576. count = readl(&regs->ep_avail);
  577. if (unlikely(count == 0)) {
  578. udelay(1);
  579. tmp = readl(&ep->regs->ep_stat);
  580. count = readl(&regs->ep_avail);
  581. /* handled that data already? */
  582. if (count == 0 && (tmp & BIT(NAK_OUT_PACKETS)) == 0)
  583. return 0;
  584. }
  585. tmp = req->req.length - req->req.actual;
  586. if (count > tmp) {
  587. /* as with DMA, data overflow gets flushed */
  588. if ((tmp % ep->ep.maxpacket) != 0) {
  589. ep_err(ep->dev,
  590. "%s out fifo %d bytes, expected %d\n",
  591. ep->ep.name, count, tmp);
  592. req->req.status = -EOVERFLOW;
  593. cleanup = 1;
  594. /* NAK_OUT_PACKETS will be set, so flushing is safe;
  595. * the next read will start with the next packet
  596. */
  597. } /* else it's a ZLP, no worries */
  598. count = tmp;
  599. }
  600. req->req.actual += count;
  601. is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
  602. ep_vdbg(ep->dev, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
  603. ep->ep.name, count, is_short ? " (short)" : "",
  604. cleanup ? " flush" : "", prevent ? " nak" : "",
  605. req, req->req.actual, req->req.length);
  606. while (count >= 4) {
  607. tmp = readl(&regs->ep_data);
  608. cpu_to_le32s(&tmp);
  609. put_unaligned(tmp, (u32 *)buf);
  610. buf += 4;
  611. count -= 4;
  612. }
  613. if (count) {
  614. tmp = readl(&regs->ep_data);
  615. /* LE conversion is implicit here: */
  616. do {
  617. *buf++ = (u8) tmp;
  618. tmp >>= 8;
  619. } while (--count);
  620. }
  621. if (cleanup)
  622. out_flush(ep);
  623. if (prevent) {
  624. writel(BIT(CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
  625. (void) readl(&ep->regs->ep_rsp);
  626. }
  627. return is_short || ((req->req.actual == req->req.length) &&
  628. !req->req.zero);
  629. }
  630. /* fill out dma descriptor to match a given request */
  631. static void fill_dma_desc(struct net2280_ep *ep,
  632. struct net2280_request *req, int valid)
  633. {
  634. struct net2280_dma *td = req->td;
  635. u32 dmacount = req->req.length;
  636. /* don't let DMA continue after a short OUT packet,
  637. * so overruns can't affect the next transfer.
  638. * in case of overruns on max-size packets, we can't
  639. * stop the fifo from filling but we can flush it.
  640. */
  641. if (ep->is_in)
  642. dmacount |= BIT(DMA_DIRECTION);
  643. if ((!ep->is_in && (dmacount % ep->ep.maxpacket) != 0) ||
  644. !(ep->dev->quirks & PLX_2280))
  645. dmacount |= BIT(END_OF_CHAIN);
  646. req->valid = valid;
  647. if (valid)
  648. dmacount |= BIT(VALID_BIT);
  649. dmacount |= BIT(DMA_DONE_INTERRUPT_ENABLE);
  650. /* td->dmadesc = previously set by caller */
  651. td->dmaaddr = cpu_to_le32 (req->req.dma);
  652. /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
  653. wmb();
  654. td->dmacount = cpu_to_le32(dmacount);
  655. }
  656. static const u32 dmactl_default =
  657. BIT(DMA_SCATTER_GATHER_DONE_INTERRUPT) |
  658. BIT(DMA_CLEAR_COUNT_ENABLE) |
  659. /* erratum 0116 workaround part 1 (use POLLING) */
  660. (POLL_100_USEC << DESCRIPTOR_POLLING_RATE) |
  661. BIT(DMA_VALID_BIT_POLLING_ENABLE) |
  662. BIT(DMA_VALID_BIT_ENABLE) |
  663. BIT(DMA_SCATTER_GATHER_ENABLE) |
  664. /* erratum 0116 workaround part 2 (no AUTOSTART) */
  665. BIT(DMA_ENABLE);
  666. static inline void spin_stop_dma(struct net2280_dma_regs __iomem *dma)
  667. {
  668. handshake(&dma->dmactl, BIT(DMA_ENABLE), 0, 50);
  669. }
  670. static inline void stop_dma(struct net2280_dma_regs __iomem *dma)
  671. {
  672. writel(readl(&dma->dmactl) & ~BIT(DMA_ENABLE), &dma->dmactl);
  673. spin_stop_dma(dma);
  674. }
  675. static void start_queue(struct net2280_ep *ep, u32 dmactl, u32 td_dma)
  676. {
  677. struct net2280_dma_regs __iomem *dma = ep->dma;
  678. unsigned int tmp = BIT(VALID_BIT) | (ep->is_in << DMA_DIRECTION);
  679. if (!(ep->dev->quirks & PLX_2280))
  680. tmp |= BIT(END_OF_CHAIN);
  681. writel(tmp, &dma->dmacount);
  682. writel(readl(&dma->dmastat), &dma->dmastat);
  683. writel(td_dma, &dma->dmadesc);
  684. if (ep->dev->quirks & PLX_SUPERSPEED)
  685. dmactl |= BIT(DMA_REQUEST_OUTSTANDING);
  686. writel(dmactl, &dma->dmactl);
  687. /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
  688. (void) readl(&ep->dev->pci->pcimstctl);
  689. writel(BIT(DMA_START), &dma->dmastat);
  690. if (!ep->is_in)
  691. stop_out_naking(ep);
  692. }
  693. static void start_dma(struct net2280_ep *ep, struct net2280_request *req)
  694. {
  695. u32 tmp;
  696. struct net2280_dma_regs __iomem *dma = ep->dma;
  697. /* FIXME can't use DMA for ZLPs */
  698. /* on this path we "know" there's no dma active (yet) */
  699. WARN_ON(readl(&dma->dmactl) & BIT(DMA_ENABLE));
  700. writel(0, &ep->dma->dmactl);
  701. /* previous OUT packet might have been short */
  702. if (!ep->is_in && (readl(&ep->regs->ep_stat) &
  703. BIT(NAK_OUT_PACKETS))) {
  704. writel(BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT),
  705. &ep->regs->ep_stat);
  706. tmp = readl(&ep->regs->ep_avail);
  707. if (tmp) {
  708. writel(readl(&dma->dmastat), &dma->dmastat);
  709. /* transfer all/some fifo data */
  710. writel(req->req.dma, &dma->dmaaddr);
  711. tmp = min(tmp, req->req.length);
  712. /* dma irq, faking scatterlist status */
  713. req->td->dmacount = cpu_to_le32(req->req.length - tmp);
  714. writel(BIT(DMA_DONE_INTERRUPT_ENABLE) | tmp,
  715. &dma->dmacount);
  716. req->td->dmadesc = 0;
  717. req->valid = 1;
  718. writel(BIT(DMA_ENABLE), &dma->dmactl);
  719. writel(BIT(DMA_START), &dma->dmastat);
  720. return;
  721. }
  722. }
  723. tmp = dmactl_default;
  724. /* force packet boundaries between dma requests, but prevent the
  725. * controller from automagically writing a last "short" packet
  726. * (zero length) unless the driver explicitly said to do that.
  727. */
  728. if (ep->is_in) {
  729. if (likely((req->req.length % ep->ep.maxpacket) ||
  730. req->req.zero)){
  731. tmp |= BIT(DMA_FIFO_VALIDATE);
  732. ep->in_fifo_validate = 1;
  733. } else
  734. ep->in_fifo_validate = 0;
  735. }
  736. /* init req->td, pointing to the current dummy */
  737. req->td->dmadesc = cpu_to_le32 (ep->td_dma);
  738. fill_dma_desc(ep, req, 1);
  739. req->td->dmacount |= cpu_to_le32(BIT(END_OF_CHAIN));
  740. start_queue(ep, tmp, req->td_dma);
  741. }
  742. static inline void
  743. queue_dma(struct net2280_ep *ep, struct net2280_request *req, int valid)
  744. {
  745. struct net2280_dma *end;
  746. dma_addr_t tmp;
  747. /* swap new dummy for old, link; fill and maybe activate */
  748. end = ep->dummy;
  749. ep->dummy = req->td;
  750. req->td = end;
  751. tmp = ep->td_dma;
  752. ep->td_dma = req->td_dma;
  753. req->td_dma = tmp;
  754. end->dmadesc = cpu_to_le32 (ep->td_dma);
  755. fill_dma_desc(ep, req, valid);
  756. }
  757. static void
  758. done(struct net2280_ep *ep, struct net2280_request *req, int status)
  759. {
  760. struct net2280 *dev;
  761. unsigned stopped = ep->stopped;
  762. list_del_init(&req->queue);
  763. if (req->req.status == -EINPROGRESS)
  764. req->req.status = status;
  765. else
  766. status = req->req.status;
  767. dev = ep->dev;
  768. if (ep->dma)
  769. usb_gadget_unmap_request(&dev->gadget, &req->req, ep->is_in);
  770. if (status && status != -ESHUTDOWN)
  771. ep_vdbg(dev, "complete %s req %p stat %d len %u/%u\n",
  772. ep->ep.name, &req->req, status,
  773. req->req.actual, req->req.length);
  774. /* don't modify queue heads during completion callback */
  775. ep->stopped = 1;
  776. spin_unlock(&dev->lock);
  777. usb_gadget_giveback_request(&ep->ep, &req->req);
  778. spin_lock(&dev->lock);
  779. ep->stopped = stopped;
  780. }
  781. /*-------------------------------------------------------------------------*/
  782. static int
  783. net2280_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
  784. {
  785. struct net2280_request *req;
  786. struct net2280_ep *ep;
  787. struct net2280 *dev;
  788. unsigned long flags;
  789. int ret = 0;
  790. /* we always require a cpu-view buffer, so that we can
  791. * always use pio (as fallback or whatever).
  792. */
  793. ep = container_of(_ep, struct net2280_ep, ep);
  794. if (!_ep || (!ep->desc && ep->num != 0)) {
  795. pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
  796. return -EINVAL;
  797. }
  798. req = container_of(_req, struct net2280_request, req);
  799. if (!_req || !_req->complete || !_req->buf ||
  800. !list_empty(&req->queue)) {
  801. ret = -EINVAL;
  802. goto print_err;
  803. }
  804. if (_req->length > (~0 & DMA_BYTE_COUNT_MASK)) {
  805. ret = -EDOM;
  806. goto print_err;
  807. }
  808. dev = ep->dev;
  809. if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
  810. ret = -ESHUTDOWN;
  811. goto print_err;
  812. }
  813. /* FIXME implement PIO fallback for ZLPs with DMA */
  814. if (ep->dma && _req->length == 0) {
  815. ret = -EOPNOTSUPP;
  816. goto print_err;
  817. }
  818. /* set up dma mapping in case the caller didn't */
  819. if (ep->dma) {
  820. ret = usb_gadget_map_request(&dev->gadget, _req,
  821. ep->is_in);
  822. if (ret)
  823. goto print_err;
  824. }
  825. ep_vdbg(dev, "%s queue req %p, len %d buf %p\n",
  826. _ep->name, _req, _req->length, _req->buf);
  827. spin_lock_irqsave(&dev->lock, flags);
  828. _req->status = -EINPROGRESS;
  829. _req->actual = 0;
  830. /* kickstart this i/o queue? */
  831. if (list_empty(&ep->queue) && !ep->stopped &&
  832. !((dev->quirks & PLX_SUPERSPEED) && ep->dma &&
  833. (readl(&ep->regs->ep_rsp) & BIT(CLEAR_ENDPOINT_HALT)))) {
  834. /* use DMA if the endpoint supports it, else pio */
  835. if (ep->dma)
  836. start_dma(ep, req);
  837. else {
  838. /* maybe there's no control data, just status ack */
  839. if (ep->num == 0 && _req->length == 0) {
  840. allow_status(ep);
  841. done(ep, req, 0);
  842. ep_vdbg(dev, "%s status ack\n", ep->ep.name);
  843. goto done;
  844. }
  845. /* PIO ... stuff the fifo, or unblock it. */
  846. if (ep->is_in)
  847. write_fifo(ep, _req);
  848. else if (list_empty(&ep->queue)) {
  849. u32 s;
  850. /* OUT FIFO might have packet(s) buffered */
  851. s = readl(&ep->regs->ep_stat);
  852. if ((s & BIT(FIFO_EMPTY)) == 0) {
  853. /* note: _req->short_not_ok is
  854. * ignored here since PIO _always_
  855. * stops queue advance here, and
  856. * _req->status doesn't change for
  857. * short reads (only _req->actual)
  858. */
  859. if (read_fifo(ep, req) &&
  860. ep->num == 0) {
  861. done(ep, req, 0);
  862. allow_status(ep);
  863. /* don't queue it */
  864. req = NULL;
  865. } else if (read_fifo(ep, req) &&
  866. ep->num != 0) {
  867. done(ep, req, 0);
  868. req = NULL;
  869. } else
  870. s = readl(&ep->regs->ep_stat);
  871. }
  872. /* don't NAK, let the fifo fill */
  873. if (req && (s & BIT(NAK_OUT_PACKETS)))
  874. writel(BIT(CLEAR_NAK_OUT_PACKETS),
  875. &ep->regs->ep_rsp);
  876. }
  877. }
  878. } else if (ep->dma) {
  879. int valid = 1;
  880. if (ep->is_in) {
  881. int expect;
  882. /* preventing magic zlps is per-engine state, not
  883. * per-transfer; irq logic must recover hiccups.
  884. */
  885. expect = likely(req->req.zero ||
  886. (req->req.length % ep->ep.maxpacket));
  887. if (expect != ep->in_fifo_validate)
  888. valid = 0;
  889. }
  890. queue_dma(ep, req, valid);
  891. } /* else the irq handler advances the queue. */
  892. ep->responded = 1;
  893. if (req)
  894. list_add_tail(&req->queue, &ep->queue);
  895. done:
  896. spin_unlock_irqrestore(&dev->lock, flags);
  897. /* pci writes may still be posted */
  898. return ret;
  899. print_err:
  900. dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, ret);
  901. return ret;
  902. }
  903. static inline void
  904. dma_done(struct net2280_ep *ep, struct net2280_request *req, u32 dmacount,
  905. int status)
  906. {
  907. req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
  908. done(ep, req, status);
  909. }
  910. static void scan_dma_completions(struct net2280_ep *ep)
  911. {
  912. /* only look at descriptors that were "naturally" retired,
  913. * so fifo and list head state won't matter
  914. */
  915. while (!list_empty(&ep->queue)) {
  916. struct net2280_request *req;
  917. u32 tmp;
  918. req = list_entry(ep->queue.next,
  919. struct net2280_request, queue);
  920. if (!req->valid)
  921. break;
  922. rmb();
  923. tmp = le32_to_cpup(&req->td->dmacount);
  924. if ((tmp & BIT(VALID_BIT)) != 0)
  925. break;
  926. /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
  927. * cases where DMA must be aborted; this code handles
  928. * all non-abort DMA completions.
  929. */
  930. if (unlikely(req->td->dmadesc == 0)) {
  931. /* paranoia */
  932. tmp = readl(&ep->dma->dmacount);
  933. if (tmp & DMA_BYTE_COUNT_MASK)
  934. break;
  935. /* single transfer mode */
  936. dma_done(ep, req, tmp, 0);
  937. break;
  938. } else if (!ep->is_in &&
  939. (req->req.length % ep->ep.maxpacket) &&
  940. !(ep->dev->quirks & PLX_SUPERSPEED)) {
  941. tmp = readl(&ep->regs->ep_stat);
  942. /* AVOID TROUBLE HERE by not issuing short reads from
  943. * your gadget driver. That helps avoids errata 0121,
  944. * 0122, and 0124; not all cases trigger the warning.
  945. */
  946. if ((tmp & BIT(NAK_OUT_PACKETS)) == 0) {
  947. ep_warn(ep->dev, "%s lost packet sync!\n",
  948. ep->ep.name);
  949. req->req.status = -EOVERFLOW;
  950. } else {
  951. tmp = readl(&ep->regs->ep_avail);
  952. if (tmp) {
  953. /* fifo gets flushed later */
  954. ep->out_overflow = 1;
  955. ep_dbg(ep->dev,
  956. "%s dma, discard %d len %d\n",
  957. ep->ep.name, tmp,
  958. req->req.length);
  959. req->req.status = -EOVERFLOW;
  960. }
  961. }
  962. }
  963. dma_done(ep, req, tmp, 0);
  964. }
  965. }
  966. static void restart_dma(struct net2280_ep *ep)
  967. {
  968. struct net2280_request *req;
  969. if (ep->stopped)
  970. return;
  971. req = list_entry(ep->queue.next, struct net2280_request, queue);
  972. start_dma(ep, req);
  973. }
  974. static void abort_dma(struct net2280_ep *ep)
  975. {
  976. /* abort the current transfer */
  977. if (likely(!list_empty(&ep->queue))) {
  978. /* FIXME work around errata 0121, 0122, 0124 */
  979. writel(BIT(DMA_ABORT), &ep->dma->dmastat);
  980. spin_stop_dma(ep->dma);
  981. } else
  982. stop_dma(ep->dma);
  983. scan_dma_completions(ep);
  984. }
  985. /* dequeue ALL requests */
  986. static void nuke(struct net2280_ep *ep)
  987. {
  988. struct net2280_request *req;
  989. /* called with spinlock held */
  990. ep->stopped = 1;
  991. if (ep->dma)
  992. abort_dma(ep);
  993. while (!list_empty(&ep->queue)) {
  994. req = list_entry(ep->queue.next,
  995. struct net2280_request,
  996. queue);
  997. done(ep, req, -ESHUTDOWN);
  998. }
  999. }
  1000. /* dequeue JUST ONE request */
  1001. static int net2280_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  1002. {
  1003. struct net2280_ep *ep;
  1004. struct net2280_request *req;
  1005. unsigned long flags;
  1006. u32 dmactl;
  1007. int stopped;
  1008. ep = container_of(_ep, struct net2280_ep, ep);
  1009. if (!_ep || (!ep->desc && ep->num != 0) || !_req) {
  1010. pr_err("%s: Invalid ep=%p or ep->desc or req=%p\n",
  1011. __func__, _ep, _req);
  1012. return -EINVAL;
  1013. }
  1014. spin_lock_irqsave(&ep->dev->lock, flags);
  1015. stopped = ep->stopped;
  1016. /* quiesce dma while we patch the queue */
  1017. dmactl = 0;
  1018. ep->stopped = 1;
  1019. if (ep->dma) {
  1020. dmactl = readl(&ep->dma->dmactl);
  1021. /* WARNING erratum 0127 may kick in ... */
  1022. stop_dma(ep->dma);
  1023. scan_dma_completions(ep);
  1024. }
  1025. /* make sure it's still queued on this endpoint */
  1026. list_for_each_entry(req, &ep->queue, queue) {
  1027. if (&req->req == _req)
  1028. break;
  1029. }
  1030. if (&req->req != _req) {
  1031. spin_unlock_irqrestore(&ep->dev->lock, flags);
  1032. dev_err(&ep->dev->pdev->dev, "%s: Request mismatch\n",
  1033. __func__);
  1034. return -EINVAL;
  1035. }
  1036. /* queue head may be partially complete. */
  1037. if (ep->queue.next == &req->queue) {
  1038. if (ep->dma) {
  1039. ep_dbg(ep->dev, "unlink (%s) dma\n", _ep->name);
  1040. _req->status = -ECONNRESET;
  1041. abort_dma(ep);
  1042. if (likely(ep->queue.next == &req->queue)) {
  1043. /* NOTE: misreports single-transfer mode*/
  1044. req->td->dmacount = 0; /* invalidate */
  1045. dma_done(ep, req,
  1046. readl(&ep->dma->dmacount),
  1047. -ECONNRESET);
  1048. }
  1049. } else {
  1050. ep_dbg(ep->dev, "unlink (%s) pio\n", _ep->name);
  1051. done(ep, req, -ECONNRESET);
  1052. }
  1053. req = NULL;
  1054. }
  1055. if (req)
  1056. done(ep, req, -ECONNRESET);
  1057. ep->stopped = stopped;
  1058. if (ep->dma) {
  1059. /* turn off dma on inactive queues */
  1060. if (list_empty(&ep->queue))
  1061. stop_dma(ep->dma);
  1062. else if (!ep->stopped) {
  1063. /* resume current request, or start new one */
  1064. if (req)
  1065. writel(dmactl, &ep->dma->dmactl);
  1066. else
  1067. start_dma(ep, list_entry(ep->queue.next,
  1068. struct net2280_request, queue));
  1069. }
  1070. }
  1071. spin_unlock_irqrestore(&ep->dev->lock, flags);
  1072. return 0;
  1073. }
  1074. /*-------------------------------------------------------------------------*/
  1075. static int net2280_fifo_status(struct usb_ep *_ep);
  1076. static int
  1077. net2280_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedged)
  1078. {
  1079. struct net2280_ep *ep;
  1080. unsigned long flags;
  1081. int retval = 0;
  1082. ep = container_of(_ep, struct net2280_ep, ep);
  1083. if (!_ep || (!ep->desc && ep->num != 0)) {
  1084. pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
  1085. return -EINVAL;
  1086. }
  1087. if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
  1088. retval = -ESHUTDOWN;
  1089. goto print_err;
  1090. }
  1091. if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
  1092. == USB_ENDPOINT_XFER_ISOC) {
  1093. retval = -EINVAL;
  1094. goto print_err;
  1095. }
  1096. spin_lock_irqsave(&ep->dev->lock, flags);
  1097. if (!list_empty(&ep->queue)) {
  1098. retval = -EAGAIN;
  1099. goto print_unlock;
  1100. } else if (ep->is_in && value && net2280_fifo_status(_ep) != 0) {
  1101. retval = -EAGAIN;
  1102. goto print_unlock;
  1103. } else {
  1104. ep_vdbg(ep->dev, "%s %s %s\n", _ep->name,
  1105. value ? "set" : "clear",
  1106. wedged ? "wedge" : "halt");
  1107. /* set/clear, then synch memory views with the device */
  1108. if (value) {
  1109. if (ep->num == 0)
  1110. ep->dev->protocol_stall = 1;
  1111. else
  1112. set_halt(ep);
  1113. if (wedged)
  1114. ep->wedged = 1;
  1115. } else {
  1116. clear_halt(ep);
  1117. if (ep->dev->quirks & PLX_SUPERSPEED &&
  1118. !list_empty(&ep->queue) && ep->td_dma)
  1119. restart_dma(ep);
  1120. ep->wedged = 0;
  1121. }
  1122. (void) readl(&ep->regs->ep_rsp);
  1123. }
  1124. spin_unlock_irqrestore(&ep->dev->lock, flags);
  1125. return retval;
  1126. print_unlock:
  1127. spin_unlock_irqrestore(&ep->dev->lock, flags);
  1128. print_err:
  1129. dev_err(&ep->dev->pdev->dev, "%s: error=%d\n", __func__, retval);
  1130. return retval;
  1131. }
  1132. static int net2280_set_halt(struct usb_ep *_ep, int value)
  1133. {
  1134. return net2280_set_halt_and_wedge(_ep, value, 0);
  1135. }
  1136. static int net2280_set_wedge(struct usb_ep *_ep)
  1137. {
  1138. if (!_ep || _ep->name == ep0name) {
  1139. pr_err("%s: Invalid ep=%p or ep0\n", __func__, _ep);
  1140. return -EINVAL;
  1141. }
  1142. return net2280_set_halt_and_wedge(_ep, 1, 1);
  1143. }
  1144. static int net2280_fifo_status(struct usb_ep *_ep)
  1145. {
  1146. struct net2280_ep *ep;
  1147. u32 avail;
  1148. ep = container_of(_ep, struct net2280_ep, ep);
  1149. if (!_ep || (!ep->desc && ep->num != 0)) {
  1150. pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
  1151. return -ENODEV;
  1152. }
  1153. if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
  1154. dev_err(&ep->dev->pdev->dev,
  1155. "%s: Invalid driver=%p or speed=%d\n",
  1156. __func__, ep->dev->driver, ep->dev->gadget.speed);
  1157. return -ESHUTDOWN;
  1158. }
  1159. avail = readl(&ep->regs->ep_avail) & (BIT(12) - 1);
  1160. if (avail > ep->fifo_size) {
  1161. dev_err(&ep->dev->pdev->dev, "%s: Fifo overflow\n", __func__);
  1162. return -EOVERFLOW;
  1163. }
  1164. if (ep->is_in)
  1165. avail = ep->fifo_size - avail;
  1166. return avail;
  1167. }
  1168. static void net2280_fifo_flush(struct usb_ep *_ep)
  1169. {
  1170. struct net2280_ep *ep;
  1171. ep = container_of(_ep, struct net2280_ep, ep);
  1172. if (!_ep || (!ep->desc && ep->num != 0)) {
  1173. pr_err("%s: Invalid ep=%p or ep->desc\n", __func__, _ep);
  1174. return;
  1175. }
  1176. if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN) {
  1177. dev_err(&ep->dev->pdev->dev,
  1178. "%s: Invalid driver=%p or speed=%d\n",
  1179. __func__, ep->dev->driver, ep->dev->gadget.speed);
  1180. return;
  1181. }
  1182. writel(BIT(FIFO_FLUSH), &ep->regs->ep_stat);
  1183. (void) readl(&ep->regs->ep_rsp);
  1184. }
  1185. static const struct usb_ep_ops net2280_ep_ops = {
  1186. .enable = net2280_enable,
  1187. .disable = net2280_disable,
  1188. .alloc_request = net2280_alloc_request,
  1189. .free_request = net2280_free_request,
  1190. .queue = net2280_queue,
  1191. .dequeue = net2280_dequeue,
  1192. .set_halt = net2280_set_halt,
  1193. .set_wedge = net2280_set_wedge,
  1194. .fifo_status = net2280_fifo_status,
  1195. .fifo_flush = net2280_fifo_flush,
  1196. };
  1197. /*-------------------------------------------------------------------------*/
  1198. static int net2280_get_frame(struct usb_gadget *_gadget)
  1199. {
  1200. struct net2280 *dev;
  1201. unsigned long flags;
  1202. u16 retval;
  1203. if (!_gadget)
  1204. return -ENODEV;
  1205. dev = container_of(_gadget, struct net2280, gadget);
  1206. spin_lock_irqsave(&dev->lock, flags);
  1207. retval = get_idx_reg(dev->regs, REG_FRAME) & 0x03ff;
  1208. spin_unlock_irqrestore(&dev->lock, flags);
  1209. return retval;
  1210. }
  1211. static int net2280_wakeup(struct usb_gadget *_gadget)
  1212. {
  1213. struct net2280 *dev;
  1214. u32 tmp;
  1215. unsigned long flags;
  1216. if (!_gadget)
  1217. return 0;
  1218. dev = container_of(_gadget, struct net2280, gadget);
  1219. spin_lock_irqsave(&dev->lock, flags);
  1220. tmp = readl(&dev->usb->usbctl);
  1221. if (tmp & BIT(DEVICE_REMOTE_WAKEUP_ENABLE))
  1222. writel(BIT(GENERATE_RESUME), &dev->usb->usbstat);
  1223. spin_unlock_irqrestore(&dev->lock, flags);
  1224. /* pci writes may still be posted */
  1225. return 0;
  1226. }
  1227. static int net2280_set_selfpowered(struct usb_gadget *_gadget, int value)
  1228. {
  1229. struct net2280 *dev;
  1230. u32 tmp;
  1231. unsigned long flags;
  1232. if (!_gadget)
  1233. return 0;
  1234. dev = container_of(_gadget, struct net2280, gadget);
  1235. spin_lock_irqsave(&dev->lock, flags);
  1236. tmp = readl(&dev->usb->usbctl);
  1237. if (value) {
  1238. tmp |= BIT(SELF_POWERED_STATUS);
  1239. _gadget->is_selfpowered = 1;
  1240. } else {
  1241. tmp &= ~BIT(SELF_POWERED_STATUS);
  1242. _gadget->is_selfpowered = 0;
  1243. }
  1244. writel(tmp, &dev->usb->usbctl);
  1245. spin_unlock_irqrestore(&dev->lock, flags);
  1246. return 0;
  1247. }
  1248. static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
  1249. {
  1250. struct net2280 *dev;
  1251. u32 tmp;
  1252. unsigned long flags;
  1253. if (!_gadget)
  1254. return -ENODEV;
  1255. dev = container_of(_gadget, struct net2280, gadget);
  1256. spin_lock_irqsave(&dev->lock, flags);
  1257. tmp = readl(&dev->usb->usbctl);
  1258. dev->softconnect = (is_on != 0);
  1259. if (is_on)
  1260. tmp |= BIT(USB_DETECT_ENABLE);
  1261. else
  1262. tmp &= ~BIT(USB_DETECT_ENABLE);
  1263. writel(tmp, &dev->usb->usbctl);
  1264. spin_unlock_irqrestore(&dev->lock, flags);
  1265. return 0;
  1266. }
  1267. static int net2280_start(struct usb_gadget *_gadget,
  1268. struct usb_gadget_driver *driver);
  1269. static int net2280_stop(struct usb_gadget *_gadget);
  1270. static const struct usb_gadget_ops net2280_ops = {
  1271. .get_frame = net2280_get_frame,
  1272. .wakeup = net2280_wakeup,
  1273. .set_selfpowered = net2280_set_selfpowered,
  1274. .pullup = net2280_pullup,
  1275. .udc_start = net2280_start,
  1276. .udc_stop = net2280_stop,
  1277. };
  1278. /*-------------------------------------------------------------------------*/
  1279. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  1280. /* FIXME move these into procfs, and use seq_file.
  1281. * Sysfs _still_ doesn't behave for arbitrarily sized files,
  1282. * and also doesn't help products using this with 2.4 kernels.
  1283. */
  1284. /* "function" sysfs attribute */
  1285. static ssize_t function_show(struct device *_dev, struct device_attribute *attr,
  1286. char *buf)
  1287. {
  1288. struct net2280 *dev = dev_get_drvdata(_dev);
  1289. if (!dev->driver || !dev->driver->function ||
  1290. strlen(dev->driver->function) > PAGE_SIZE)
  1291. return 0;
  1292. return scnprintf(buf, PAGE_SIZE, "%s\n", dev->driver->function);
  1293. }
  1294. static DEVICE_ATTR_RO(function);
  1295. static ssize_t registers_show(struct device *_dev,
  1296. struct device_attribute *attr, char *buf)
  1297. {
  1298. struct net2280 *dev;
  1299. char *next;
  1300. unsigned size, t;
  1301. unsigned long flags;
  1302. int i;
  1303. u32 t1, t2;
  1304. const char *s;
  1305. dev = dev_get_drvdata(_dev);
  1306. next = buf;
  1307. size = PAGE_SIZE;
  1308. spin_lock_irqsave(&dev->lock, flags);
  1309. if (dev->driver)
  1310. s = dev->driver->driver.name;
  1311. else
  1312. s = "(none)";
  1313. /* Main Control Registers */
  1314. t = scnprintf(next, size, "%s version " DRIVER_VERSION
  1315. ", chiprev %04x\n\n"
  1316. "devinit %03x fifoctl %08x gadget '%s'\n"
  1317. "pci irqenb0 %02x irqenb1 %08x "
  1318. "irqstat0 %04x irqstat1 %08x\n",
  1319. driver_name, dev->chiprev,
  1320. readl(&dev->regs->devinit),
  1321. readl(&dev->regs->fifoctl),
  1322. s,
  1323. readl(&dev->regs->pciirqenb0),
  1324. readl(&dev->regs->pciirqenb1),
  1325. readl(&dev->regs->irqstat0),
  1326. readl(&dev->regs->irqstat1));
  1327. size -= t;
  1328. next += t;
  1329. /* USB Control Registers */
  1330. t1 = readl(&dev->usb->usbctl);
  1331. t2 = readl(&dev->usb->usbstat);
  1332. if (t1 & BIT(VBUS_PIN)) {
  1333. if (t2 & BIT(HIGH_SPEED))
  1334. s = "high speed";
  1335. else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
  1336. s = "powered";
  1337. else
  1338. s = "full speed";
  1339. /* full speed bit (6) not working?? */
  1340. } else
  1341. s = "not attached";
  1342. t = scnprintf(next, size,
  1343. "stdrsp %08x usbctl %08x usbstat %08x "
  1344. "addr 0x%02x (%s)\n",
  1345. readl(&dev->usb->stdrsp), t1, t2,
  1346. readl(&dev->usb->ouraddr), s);
  1347. size -= t;
  1348. next += t;
  1349. /* PCI Master Control Registers */
  1350. /* DMA Control Registers */
  1351. /* Configurable EP Control Registers */
  1352. for (i = 0; i < dev->n_ep; i++) {
  1353. struct net2280_ep *ep;
  1354. ep = &dev->ep[i];
  1355. if (i && !ep->desc)
  1356. continue;
  1357. t1 = readl(&ep->cfg->ep_cfg);
  1358. t2 = readl(&ep->regs->ep_rsp) & 0xff;
  1359. t = scnprintf(next, size,
  1360. "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
  1361. "irqenb %02x\n",
  1362. ep->ep.name, t1, t2,
  1363. (t2 & BIT(CLEAR_NAK_OUT_PACKETS))
  1364. ? "NAK " : "",
  1365. (t2 & BIT(CLEAR_EP_HIDE_STATUS_PHASE))
  1366. ? "hide " : "",
  1367. (t2 & BIT(CLEAR_EP_FORCE_CRC_ERROR))
  1368. ? "CRC " : "",
  1369. (t2 & BIT(CLEAR_INTERRUPT_MODE))
  1370. ? "interrupt " : "",
  1371. (t2 & BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
  1372. ? "status " : "",
  1373. (t2 & BIT(CLEAR_NAK_OUT_PACKETS_MODE))
  1374. ? "NAKmode " : "",
  1375. (t2 & BIT(CLEAR_ENDPOINT_TOGGLE))
  1376. ? "DATA1 " : "DATA0 ",
  1377. (t2 & BIT(CLEAR_ENDPOINT_HALT))
  1378. ? "HALT " : "",
  1379. readl(&ep->regs->ep_irqenb));
  1380. size -= t;
  1381. next += t;
  1382. t = scnprintf(next, size,
  1383. "\tstat %08x avail %04x "
  1384. "(ep%d%s-%s)%s\n",
  1385. readl(&ep->regs->ep_stat),
  1386. readl(&ep->regs->ep_avail),
  1387. t1 & 0x0f, DIR_STRING(t1),
  1388. type_string(t1 >> 8),
  1389. ep->stopped ? "*" : "");
  1390. size -= t;
  1391. next += t;
  1392. if (!ep->dma)
  1393. continue;
  1394. t = scnprintf(next, size,
  1395. " dma\tctl %08x stat %08x count %08x\n"
  1396. "\taddr %08x desc %08x\n",
  1397. readl(&ep->dma->dmactl),
  1398. readl(&ep->dma->dmastat),
  1399. readl(&ep->dma->dmacount),
  1400. readl(&ep->dma->dmaaddr),
  1401. readl(&ep->dma->dmadesc));
  1402. size -= t;
  1403. next += t;
  1404. }
  1405. /* Indexed Registers (none yet) */
  1406. /* Statistics */
  1407. t = scnprintf(next, size, "\nirqs: ");
  1408. size -= t;
  1409. next += t;
  1410. for (i = 0; i < dev->n_ep; i++) {
  1411. struct net2280_ep *ep;
  1412. ep = &dev->ep[i];
  1413. if (i && !ep->irqs)
  1414. continue;
  1415. t = scnprintf(next, size, " %s/%lu", ep->ep.name, ep->irqs);
  1416. size -= t;
  1417. next += t;
  1418. }
  1419. t = scnprintf(next, size, "\n");
  1420. size -= t;
  1421. next += t;
  1422. spin_unlock_irqrestore(&dev->lock, flags);
  1423. return PAGE_SIZE - size;
  1424. }
  1425. static DEVICE_ATTR_RO(registers);
  1426. static ssize_t queues_show(struct device *_dev, struct device_attribute *attr,
  1427. char *buf)
  1428. {
  1429. struct net2280 *dev;
  1430. char *next;
  1431. unsigned size;
  1432. unsigned long flags;
  1433. int i;
  1434. dev = dev_get_drvdata(_dev);
  1435. next = buf;
  1436. size = PAGE_SIZE;
  1437. spin_lock_irqsave(&dev->lock, flags);
  1438. for (i = 0; i < dev->n_ep; i++) {
  1439. struct net2280_ep *ep = &dev->ep[i];
  1440. struct net2280_request *req;
  1441. int t;
  1442. if (i != 0) {
  1443. const struct usb_endpoint_descriptor *d;
  1444. d = ep->desc;
  1445. if (!d)
  1446. continue;
  1447. t = d->bEndpointAddress;
  1448. t = scnprintf(next, size,
  1449. "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
  1450. ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
  1451. (t & USB_DIR_IN) ? "in" : "out",
  1452. type_string(d->bmAttributes),
  1453. usb_endpoint_maxp(d) & 0x1fff,
  1454. ep->dma ? "dma" : "pio", ep->fifo_size
  1455. );
  1456. } else /* ep0 should only have one transfer queued */
  1457. t = scnprintf(next, size, "ep0 max 64 pio %s\n",
  1458. ep->is_in ? "in" : "out");
  1459. if (t <= 0 || t > size)
  1460. goto done;
  1461. size -= t;
  1462. next += t;
  1463. if (list_empty(&ep->queue)) {
  1464. t = scnprintf(next, size, "\t(nothing queued)\n");
  1465. if (t <= 0 || t > size)
  1466. goto done;
  1467. size -= t;
  1468. next += t;
  1469. continue;
  1470. }
  1471. list_for_each_entry(req, &ep->queue, queue) {
  1472. if (ep->dma && req->td_dma == readl(&ep->dma->dmadesc))
  1473. t = scnprintf(next, size,
  1474. "\treq %p len %d/%d "
  1475. "buf %p (dmacount %08x)\n",
  1476. &req->req, req->req.actual,
  1477. req->req.length, req->req.buf,
  1478. readl(&ep->dma->dmacount));
  1479. else
  1480. t = scnprintf(next, size,
  1481. "\treq %p len %d/%d buf %p\n",
  1482. &req->req, req->req.actual,
  1483. req->req.length, req->req.buf);
  1484. if (t <= 0 || t > size)
  1485. goto done;
  1486. size -= t;
  1487. next += t;
  1488. if (ep->dma) {
  1489. struct net2280_dma *td;
  1490. td = req->td;
  1491. t = scnprintf(next, size, "\t td %08x "
  1492. " count %08x buf %08x desc %08x\n",
  1493. (u32) req->td_dma,
  1494. le32_to_cpu(td->dmacount),
  1495. le32_to_cpu(td->dmaaddr),
  1496. le32_to_cpu(td->dmadesc));
  1497. if (t <= 0 || t > size)
  1498. goto done;
  1499. size -= t;
  1500. next += t;
  1501. }
  1502. }
  1503. }
  1504. done:
  1505. spin_unlock_irqrestore(&dev->lock, flags);
  1506. return PAGE_SIZE - size;
  1507. }
  1508. static DEVICE_ATTR_RO(queues);
  1509. #else
  1510. #define device_create_file(a, b) (0)
  1511. #define device_remove_file(a, b) do { } while (0)
  1512. #endif
  1513. /*-------------------------------------------------------------------------*/
  1514. /* another driver-specific mode might be a request type doing dma
  1515. * to/from another device fifo instead of to/from memory.
  1516. */
  1517. static void set_fifo_mode(struct net2280 *dev, int mode)
  1518. {
  1519. /* keeping high bits preserves BAR2 */
  1520. writel((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
  1521. /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
  1522. INIT_LIST_HEAD(&dev->gadget.ep_list);
  1523. list_add_tail(&dev->ep[1].ep.ep_list, &dev->gadget.ep_list);
  1524. list_add_tail(&dev->ep[2].ep.ep_list, &dev->gadget.ep_list);
  1525. switch (mode) {
  1526. case 0:
  1527. list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
  1528. list_add_tail(&dev->ep[4].ep.ep_list, &dev->gadget.ep_list);
  1529. dev->ep[1].fifo_size = dev->ep[2].fifo_size = 1024;
  1530. break;
  1531. case 1:
  1532. dev->ep[1].fifo_size = dev->ep[2].fifo_size = 2048;
  1533. break;
  1534. case 2:
  1535. list_add_tail(&dev->ep[3].ep.ep_list, &dev->gadget.ep_list);
  1536. dev->ep[1].fifo_size = 2048;
  1537. dev->ep[2].fifo_size = 1024;
  1538. break;
  1539. }
  1540. /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
  1541. list_add_tail(&dev->ep[5].ep.ep_list, &dev->gadget.ep_list);
  1542. list_add_tail(&dev->ep[6].ep.ep_list, &dev->gadget.ep_list);
  1543. }
  1544. static void defect7374_disable_data_eps(struct net2280 *dev)
  1545. {
  1546. /*
  1547. * For Defect 7374, disable data EPs (and more):
  1548. * - This phase undoes the earlier phase of the Defect 7374 workaround,
  1549. * returing ep regs back to normal.
  1550. */
  1551. struct net2280_ep *ep;
  1552. int i;
  1553. unsigned char ep_sel;
  1554. u32 tmp_reg;
  1555. for (i = 1; i < 5; i++) {
  1556. ep = &dev->ep[i];
  1557. writel(0, &ep->cfg->ep_cfg);
  1558. }
  1559. /* CSROUT, CSRIN, PCIOUT, PCIIN, STATIN, RCIN */
  1560. for (i = 0; i < 6; i++)
  1561. writel(0, &dev->dep[i].dep_cfg);
  1562. for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
  1563. /* Select an endpoint for subsequent operations: */
  1564. tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
  1565. writel(((tmp_reg & ~0x1f) | ep_sel), &dev->plregs->pl_ep_ctrl);
  1566. if (ep_sel < 2 || (ep_sel > 9 && ep_sel < 14) ||
  1567. ep_sel == 18 || ep_sel == 20)
  1568. continue;
  1569. /* Change settings on some selected endpoints */
  1570. tmp_reg = readl(&dev->plregs->pl_ep_cfg_4);
  1571. tmp_reg &= ~BIT(NON_CTRL_IN_TOLERATE_BAD_DIR);
  1572. writel(tmp_reg, &dev->plregs->pl_ep_cfg_4);
  1573. tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
  1574. tmp_reg |= BIT(EP_INITIALIZED);
  1575. writel(tmp_reg, &dev->plregs->pl_ep_ctrl);
  1576. }
  1577. }
  1578. static void defect7374_enable_data_eps_zero(struct net2280 *dev)
  1579. {
  1580. u32 tmp = 0, tmp_reg;
  1581. u32 scratch;
  1582. int i;
  1583. unsigned char ep_sel;
  1584. scratch = get_idx_reg(dev->regs, SCRATCH);
  1585. WARN_ON((scratch & (0xf << DEFECT7374_FSM_FIELD))
  1586. == DEFECT7374_FSM_SS_CONTROL_READ);
  1587. scratch &= ~(0xf << DEFECT7374_FSM_FIELD);
  1588. ep_warn(dev, "Operate Defect 7374 workaround soft this time");
  1589. ep_warn(dev, "It will operate on cold-reboot and SS connect");
  1590. /*GPEPs:*/
  1591. tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_DIRECTION) |
  1592. (2 << OUT_ENDPOINT_TYPE) | (2 << IN_ENDPOINT_TYPE) |
  1593. ((dev->enhanced_mode) ?
  1594. BIT(OUT_ENDPOINT_ENABLE) : BIT(ENDPOINT_ENABLE)) |
  1595. BIT(IN_ENDPOINT_ENABLE));
  1596. for (i = 1; i < 5; i++)
  1597. writel(tmp, &dev->ep[i].cfg->ep_cfg);
  1598. /* CSRIN, PCIIN, STATIN, RCIN*/
  1599. tmp = ((0 << ENDPOINT_NUMBER) | BIT(ENDPOINT_ENABLE));
  1600. writel(tmp, &dev->dep[1].dep_cfg);
  1601. writel(tmp, &dev->dep[3].dep_cfg);
  1602. writel(tmp, &dev->dep[4].dep_cfg);
  1603. writel(tmp, &dev->dep[5].dep_cfg);
  1604. /*Implemented for development and debug.
  1605. * Can be refined/tuned later.*/
  1606. for (ep_sel = 0; ep_sel <= 21; ep_sel++) {
  1607. /* Select an endpoint for subsequent operations: */
  1608. tmp_reg = readl(&dev->plregs->pl_ep_ctrl);
  1609. writel(((tmp_reg & ~0x1f) | ep_sel),
  1610. &dev->plregs->pl_ep_ctrl);
  1611. if (ep_sel == 1) {
  1612. tmp =
  1613. (readl(&dev->plregs->pl_ep_ctrl) |
  1614. BIT(CLEAR_ACK_ERROR_CODE) | 0);
  1615. writel(tmp, &dev->plregs->pl_ep_ctrl);
  1616. continue;
  1617. }
  1618. if (ep_sel == 0 || (ep_sel > 9 && ep_sel < 14) ||
  1619. ep_sel == 18 || ep_sel == 20)
  1620. continue;
  1621. tmp = (readl(&dev->plregs->pl_ep_cfg_4) |
  1622. BIT(NON_CTRL_IN_TOLERATE_BAD_DIR) | 0);
  1623. writel(tmp, &dev->plregs->pl_ep_cfg_4);
  1624. tmp = readl(&dev->plregs->pl_ep_ctrl) &
  1625. ~BIT(EP_INITIALIZED);
  1626. writel(tmp, &dev->plregs->pl_ep_ctrl);
  1627. }
  1628. /* Set FSM to focus on the first Control Read:
  1629. * - Tip: Connection speed is known upon the first
  1630. * setup request.*/
  1631. scratch |= DEFECT7374_FSM_WAITING_FOR_CONTROL_READ;
  1632. set_idx_reg(dev->regs, SCRATCH, scratch);
  1633. }
  1634. /* keeping it simple:
  1635. * - one bus driver, initted first;
  1636. * - one function driver, initted second
  1637. *
  1638. * most of the work to support multiple net2280 controllers would
  1639. * be to associate this gadget driver (yes?) with all of them, or
  1640. * perhaps to bind specific drivers to specific devices.
  1641. */
  1642. static void usb_reset_228x(struct net2280 *dev)
  1643. {
  1644. u32 tmp;
  1645. dev->gadget.speed = USB_SPEED_UNKNOWN;
  1646. (void) readl(&dev->usb->usbctl);
  1647. net2280_led_init(dev);
  1648. /* disable automatic responses, and irqs */
  1649. writel(0, &dev->usb->stdrsp);
  1650. writel(0, &dev->regs->pciirqenb0);
  1651. writel(0, &dev->regs->pciirqenb1);
  1652. /* clear old dma and irq state */
  1653. for (tmp = 0; tmp < 4; tmp++) {
  1654. struct net2280_ep *ep = &dev->ep[tmp + 1];
  1655. if (ep->dma)
  1656. abort_dma(ep);
  1657. }
  1658. writel(~0, &dev->regs->irqstat0),
  1659. writel(~(u32)BIT(SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
  1660. /* reset, and enable pci */
  1661. tmp = readl(&dev->regs->devinit) |
  1662. BIT(PCI_ENABLE) |
  1663. BIT(FIFO_SOFT_RESET) |
  1664. BIT(USB_SOFT_RESET) |
  1665. BIT(M8051_RESET);
  1666. writel(tmp, &dev->regs->devinit);
  1667. /* standard fifo and endpoint allocations */
  1668. set_fifo_mode(dev, (fifo_mode <= 2) ? fifo_mode : 0);
  1669. }
  1670. static void usb_reset_338x(struct net2280 *dev)
  1671. {
  1672. u32 tmp;
  1673. dev->gadget.speed = USB_SPEED_UNKNOWN;
  1674. (void)readl(&dev->usb->usbctl);
  1675. net2280_led_init(dev);
  1676. if (dev->bug7734_patched) {
  1677. /* disable automatic responses, and irqs */
  1678. writel(0, &dev->usb->stdrsp);
  1679. writel(0, &dev->regs->pciirqenb0);
  1680. writel(0, &dev->regs->pciirqenb1);
  1681. }
  1682. /* clear old dma and irq state */
  1683. for (tmp = 0; tmp < 4; tmp++) {
  1684. struct net2280_ep *ep = &dev->ep[tmp + 1];
  1685. if (ep->dma)
  1686. abort_dma(ep);
  1687. }
  1688. writel(~0, &dev->regs->irqstat0), writel(~0, &dev->regs->irqstat1);
  1689. if (dev->bug7734_patched) {
  1690. /* reset, and enable pci */
  1691. tmp = readl(&dev->regs->devinit) |
  1692. BIT(PCI_ENABLE) |
  1693. BIT(FIFO_SOFT_RESET) |
  1694. BIT(USB_SOFT_RESET) |
  1695. BIT(M8051_RESET);
  1696. writel(tmp, &dev->regs->devinit);
  1697. }
  1698. /* always ep-{1,2,3,4} ... maybe not ep-3 or ep-4 */
  1699. INIT_LIST_HEAD(&dev->gadget.ep_list);
  1700. for (tmp = 1; tmp < dev->n_ep; tmp++)
  1701. list_add_tail(&dev->ep[tmp].ep.ep_list, &dev->gadget.ep_list);
  1702. }
  1703. static void usb_reset(struct net2280 *dev)
  1704. {
  1705. if (dev->quirks & PLX_LEGACY)
  1706. return usb_reset_228x(dev);
  1707. return usb_reset_338x(dev);
  1708. }
  1709. static void usb_reinit_228x(struct net2280 *dev)
  1710. {
  1711. u32 tmp;
  1712. /* basic endpoint init */
  1713. for (tmp = 0; tmp < 7; tmp++) {
  1714. struct net2280_ep *ep = &dev->ep[tmp];
  1715. ep->ep.name = ep_name[tmp];
  1716. ep->dev = dev;
  1717. ep->num = tmp;
  1718. if (tmp > 0 && tmp <= 4) {
  1719. ep->fifo_size = 1024;
  1720. ep->dma = &dev->dma[tmp - 1];
  1721. } else
  1722. ep->fifo_size = 64;
  1723. ep->regs = &dev->epregs[tmp];
  1724. ep->cfg = &dev->epregs[tmp];
  1725. ep_reset_228x(dev->regs, ep);
  1726. }
  1727. usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 64);
  1728. usb_ep_set_maxpacket_limit(&dev->ep[5].ep, 64);
  1729. usb_ep_set_maxpacket_limit(&dev->ep[6].ep, 64);
  1730. dev->gadget.ep0 = &dev->ep[0].ep;
  1731. dev->ep[0].stopped = 0;
  1732. INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
  1733. /* we want to prevent lowlevel/insecure access from the USB host,
  1734. * but erratum 0119 means this enable bit is ignored
  1735. */
  1736. for (tmp = 0; tmp < 5; tmp++)
  1737. writel(EP_DONTUSE, &dev->dep[tmp].dep_cfg);
  1738. }
  1739. static void usb_reinit_338x(struct net2280 *dev)
  1740. {
  1741. int i;
  1742. u32 tmp, val;
  1743. static const u32 ne[9] = { 0, 1, 2, 3, 4, 1, 2, 3, 4 };
  1744. static const u32 ep_reg_addr[9] = { 0x00, 0xC0, 0x00, 0xC0, 0x00,
  1745. 0x00, 0xC0, 0x00, 0xC0 };
  1746. /* basic endpoint init */
  1747. for (i = 0; i < dev->n_ep; i++) {
  1748. struct net2280_ep *ep = &dev->ep[i];
  1749. ep->ep.name = dev->enhanced_mode ? ep_name_adv[i] : ep_name[i];
  1750. ep->dev = dev;
  1751. ep->num = i;
  1752. if (i > 0 && i <= 4)
  1753. ep->dma = &dev->dma[i - 1];
  1754. if (dev->enhanced_mode) {
  1755. ep->cfg = &dev->epregs[ne[i]];
  1756. ep->regs = (struct net2280_ep_regs __iomem *)
  1757. (((void __iomem *)&dev->epregs[ne[i]]) +
  1758. ep_reg_addr[i]);
  1759. } else {
  1760. ep->cfg = &dev->epregs[i];
  1761. ep->regs = &dev->epregs[i];
  1762. }
  1763. ep->fifo_size = (i != 0) ? 2048 : 512;
  1764. ep_reset_338x(dev->regs, ep);
  1765. }
  1766. usb_ep_set_maxpacket_limit(&dev->ep[0].ep, 512);
  1767. dev->gadget.ep0 = &dev->ep[0].ep;
  1768. dev->ep[0].stopped = 0;
  1769. /* Link layer set up */
  1770. if (dev->bug7734_patched) {
  1771. tmp = readl(&dev->usb_ext->usbctl2) &
  1772. ~(BIT(U1_ENABLE) | BIT(U2_ENABLE) | BIT(LTM_ENABLE));
  1773. writel(tmp, &dev->usb_ext->usbctl2);
  1774. }
  1775. /* Hardware Defect and Workaround */
  1776. val = readl(&dev->ll_lfps_regs->ll_lfps_5);
  1777. val &= ~(0xf << TIMER_LFPS_6US);
  1778. val |= 0x5 << TIMER_LFPS_6US;
  1779. writel(val, &dev->ll_lfps_regs->ll_lfps_5);
  1780. val = readl(&dev->ll_lfps_regs->ll_lfps_6);
  1781. val &= ~(0xffff << TIMER_LFPS_80US);
  1782. val |= 0x0100 << TIMER_LFPS_80US;
  1783. writel(val, &dev->ll_lfps_regs->ll_lfps_6);
  1784. /*
  1785. * AA_AB Errata. Issue 4. Workaround for SuperSpeed USB
  1786. * Hot Reset Exit Handshake may Fail in Specific Case using
  1787. * Default Register Settings. Workaround for Enumeration test.
  1788. */
  1789. val = readl(&dev->ll_tsn_regs->ll_tsn_counters_2);
  1790. val &= ~(0x1f << HOT_TX_NORESET_TS2);
  1791. val |= 0x10 << HOT_TX_NORESET_TS2;
  1792. writel(val, &dev->ll_tsn_regs->ll_tsn_counters_2);
  1793. val = readl(&dev->ll_tsn_regs->ll_tsn_counters_3);
  1794. val &= ~(0x1f << HOT_RX_RESET_TS2);
  1795. val |= 0x3 << HOT_RX_RESET_TS2;
  1796. writel(val, &dev->ll_tsn_regs->ll_tsn_counters_3);
  1797. /*
  1798. * Set Recovery Idle to Recover bit:
  1799. * - On SS connections, setting Recovery Idle to Recover Fmw improves
  1800. * link robustness with various hosts and hubs.
  1801. * - It is safe to set for all connection speeds; all chip revisions.
  1802. * - R-M-W to leave other bits undisturbed.
  1803. * - Reference PLX TT-7372
  1804. */
  1805. val = readl(&dev->ll_chicken_reg->ll_tsn_chicken_bit);
  1806. val |= BIT(RECOVERY_IDLE_TO_RECOVER_FMW);
  1807. writel(val, &dev->ll_chicken_reg->ll_tsn_chicken_bit);
  1808. INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
  1809. /* disable dedicated endpoints */
  1810. writel(0x0D, &dev->dep[0].dep_cfg);
  1811. writel(0x0D, &dev->dep[1].dep_cfg);
  1812. writel(0x0E, &dev->dep[2].dep_cfg);
  1813. writel(0x0E, &dev->dep[3].dep_cfg);
  1814. writel(0x0F, &dev->dep[4].dep_cfg);
  1815. writel(0x0C, &dev->dep[5].dep_cfg);
  1816. }
  1817. static void usb_reinit(struct net2280 *dev)
  1818. {
  1819. if (dev->quirks & PLX_LEGACY)
  1820. return usb_reinit_228x(dev);
  1821. return usb_reinit_338x(dev);
  1822. }
  1823. static void ep0_start_228x(struct net2280 *dev)
  1824. {
  1825. writel(BIT(CLEAR_EP_HIDE_STATUS_PHASE) |
  1826. BIT(CLEAR_NAK_OUT_PACKETS) |
  1827. BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE),
  1828. &dev->epregs[0].ep_rsp);
  1829. /*
  1830. * hardware optionally handles a bunch of standard requests
  1831. * that the API hides from drivers anyway. have it do so.
  1832. * endpoint status/features are handled in software, to
  1833. * help pass tests for some dubious behavior.
  1834. */
  1835. writel(BIT(SET_TEST_MODE) |
  1836. BIT(SET_ADDRESS) |
  1837. BIT(DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP) |
  1838. BIT(GET_DEVICE_STATUS) |
  1839. BIT(GET_INTERFACE_STATUS),
  1840. &dev->usb->stdrsp);
  1841. writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
  1842. BIT(SELF_POWERED_USB_DEVICE) |
  1843. BIT(REMOTE_WAKEUP_SUPPORT) |
  1844. (dev->softconnect << USB_DETECT_ENABLE) |
  1845. BIT(SELF_POWERED_STATUS),
  1846. &dev->usb->usbctl);
  1847. /* enable irqs so we can see ep0 and general operation */
  1848. writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
  1849. BIT(ENDPOINT_0_INTERRUPT_ENABLE),
  1850. &dev->regs->pciirqenb0);
  1851. writel(BIT(PCI_INTERRUPT_ENABLE) |
  1852. BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE) |
  1853. BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE) |
  1854. BIT(PCI_RETRY_ABORT_INTERRUPT_ENABLE) |
  1855. BIT(VBUS_INTERRUPT_ENABLE) |
  1856. BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
  1857. BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE),
  1858. &dev->regs->pciirqenb1);
  1859. /* don't leave any writes posted */
  1860. (void) readl(&dev->usb->usbctl);
  1861. }
  1862. static void ep0_start_338x(struct net2280 *dev)
  1863. {
  1864. if (dev->bug7734_patched)
  1865. writel(BIT(CLEAR_NAK_OUT_PACKETS_MODE) |
  1866. BIT(SET_EP_HIDE_STATUS_PHASE),
  1867. &dev->epregs[0].ep_rsp);
  1868. /*
  1869. * hardware optionally handles a bunch of standard requests
  1870. * that the API hides from drivers anyway. have it do so.
  1871. * endpoint status/features are handled in software, to
  1872. * help pass tests for some dubious behavior.
  1873. */
  1874. writel(BIT(SET_ISOCHRONOUS_DELAY) |
  1875. BIT(SET_SEL) |
  1876. BIT(SET_TEST_MODE) |
  1877. BIT(SET_ADDRESS) |
  1878. BIT(GET_INTERFACE_STATUS) |
  1879. BIT(GET_DEVICE_STATUS),
  1880. &dev->usb->stdrsp);
  1881. dev->wakeup_enable = 1;
  1882. writel(BIT(USB_ROOT_PORT_WAKEUP_ENABLE) |
  1883. (dev->softconnect << USB_DETECT_ENABLE) |
  1884. BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
  1885. &dev->usb->usbctl);
  1886. /* enable irqs so we can see ep0 and general operation */
  1887. writel(BIT(SETUP_PACKET_INTERRUPT_ENABLE) |
  1888. BIT(ENDPOINT_0_INTERRUPT_ENABLE),
  1889. &dev->regs->pciirqenb0);
  1890. writel(BIT(PCI_INTERRUPT_ENABLE) |
  1891. BIT(ROOT_PORT_RESET_INTERRUPT_ENABLE) |
  1892. BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE) |
  1893. BIT(VBUS_INTERRUPT_ENABLE),
  1894. &dev->regs->pciirqenb1);
  1895. /* don't leave any writes posted */
  1896. (void)readl(&dev->usb->usbctl);
  1897. }
  1898. static void ep0_start(struct net2280 *dev)
  1899. {
  1900. if (dev->quirks & PLX_LEGACY)
  1901. return ep0_start_228x(dev);
  1902. return ep0_start_338x(dev);
  1903. }
  1904. /* when a driver is successfully registered, it will receive
  1905. * control requests including set_configuration(), which enables
  1906. * non-control requests. then usb traffic follows until a
  1907. * disconnect is reported. then a host may connect again, or
  1908. * the driver might get unbound.
  1909. */
  1910. static int net2280_start(struct usb_gadget *_gadget,
  1911. struct usb_gadget_driver *driver)
  1912. {
  1913. struct net2280 *dev;
  1914. int retval;
  1915. unsigned i;
  1916. /* insist on high speed support from the driver, since
  1917. * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
  1918. * "must not be used in normal operation"
  1919. */
  1920. if (!driver || driver->max_speed < USB_SPEED_HIGH ||
  1921. !driver->setup)
  1922. return -EINVAL;
  1923. dev = container_of(_gadget, struct net2280, gadget);
  1924. for (i = 0; i < dev->n_ep; i++)
  1925. dev->ep[i].irqs = 0;
  1926. /* hook up the driver ... */
  1927. driver->driver.bus = NULL;
  1928. dev->driver = driver;
  1929. retval = device_create_file(&dev->pdev->dev, &dev_attr_function);
  1930. if (retval)
  1931. goto err_unbind;
  1932. retval = device_create_file(&dev->pdev->dev, &dev_attr_queues);
  1933. if (retval)
  1934. goto err_func;
  1935. /* enable host detection and ep0; and we're ready
  1936. * for set_configuration as well as eventual disconnect.
  1937. */
  1938. net2280_led_active(dev, 1);
  1939. if ((dev->quirks & PLX_SUPERSPEED) && !dev->bug7734_patched)
  1940. defect7374_enable_data_eps_zero(dev);
  1941. ep0_start(dev);
  1942. /* pci writes may still be posted */
  1943. return 0;
  1944. err_func:
  1945. device_remove_file(&dev->pdev->dev, &dev_attr_function);
  1946. err_unbind:
  1947. dev->driver = NULL;
  1948. return retval;
  1949. }
  1950. static void stop_activity(struct net2280 *dev, struct usb_gadget_driver *driver)
  1951. {
  1952. int i;
  1953. /* don't disconnect if it's not connected */
  1954. if (dev->gadget.speed == USB_SPEED_UNKNOWN)
  1955. driver = NULL;
  1956. /* stop hardware; prevent new request submissions;
  1957. * and kill any outstanding requests.
  1958. */
  1959. usb_reset(dev);
  1960. for (i = 0; i < dev->n_ep; i++)
  1961. nuke(&dev->ep[i]);
  1962. /* report disconnect; the driver is already quiesced */
  1963. if (driver) {
  1964. spin_unlock(&dev->lock);
  1965. driver->disconnect(&dev->gadget);
  1966. spin_lock(&dev->lock);
  1967. }
  1968. usb_reinit(dev);
  1969. }
  1970. static int net2280_stop(struct usb_gadget *_gadget)
  1971. {
  1972. struct net2280 *dev;
  1973. unsigned long flags;
  1974. dev = container_of(_gadget, struct net2280, gadget);
  1975. spin_lock_irqsave(&dev->lock, flags);
  1976. stop_activity(dev, NULL);
  1977. spin_unlock_irqrestore(&dev->lock, flags);
  1978. net2280_led_active(dev, 0);
  1979. device_remove_file(&dev->pdev->dev, &dev_attr_function);
  1980. device_remove_file(&dev->pdev->dev, &dev_attr_queues);
  1981. dev->driver = NULL;
  1982. return 0;
  1983. }
  1984. /*-------------------------------------------------------------------------*/
  1985. /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
  1986. * also works for dma-capable endpoints, in pio mode or just
  1987. * to manually advance the queue after short OUT transfers.
  1988. */
  1989. static void handle_ep_small(struct net2280_ep *ep)
  1990. {
  1991. struct net2280_request *req;
  1992. u32 t;
  1993. /* 0 error, 1 mid-data, 2 done */
  1994. int mode = 1;
  1995. if (!list_empty(&ep->queue))
  1996. req = list_entry(ep->queue.next,
  1997. struct net2280_request, queue);
  1998. else
  1999. req = NULL;
  2000. /* ack all, and handle what we care about */
  2001. t = readl(&ep->regs->ep_stat);
  2002. ep->irqs++;
  2003. ep_vdbg(ep->dev, "%s ack ep_stat %08x, req %p\n",
  2004. ep->ep.name, t, req ? &req->req : NULL);
  2005. if (!ep->is_in || (ep->dev->quirks & PLX_2280))
  2006. writel(t & ~BIT(NAK_OUT_PACKETS), &ep->regs->ep_stat);
  2007. else
  2008. /* Added for 2282 */
  2009. writel(t, &ep->regs->ep_stat);
  2010. /* for ep0, monitor token irqs to catch data stage length errors
  2011. * and to synchronize on status.
  2012. *
  2013. * also, to defer reporting of protocol stalls ... here's where
  2014. * data or status first appears, handling stalls here should never
  2015. * cause trouble on the host side..
  2016. *
  2017. * control requests could be slightly faster without token synch for
  2018. * status, but status can jam up that way.
  2019. */
  2020. if (unlikely(ep->num == 0)) {
  2021. if (ep->is_in) {
  2022. /* status; stop NAKing */
  2023. if (t & BIT(DATA_OUT_PING_TOKEN_INTERRUPT)) {
  2024. if (ep->dev->protocol_stall) {
  2025. ep->stopped = 1;
  2026. set_halt(ep);
  2027. }
  2028. if (!req)
  2029. allow_status(ep);
  2030. mode = 2;
  2031. /* reply to extra IN data tokens with a zlp */
  2032. } else if (t & BIT(DATA_IN_TOKEN_INTERRUPT)) {
  2033. if (ep->dev->protocol_stall) {
  2034. ep->stopped = 1;
  2035. set_halt(ep);
  2036. mode = 2;
  2037. } else if (ep->responded &&
  2038. !req && !ep->stopped)
  2039. write_fifo(ep, NULL);
  2040. }
  2041. } else {
  2042. /* status; stop NAKing */
  2043. if (t & BIT(DATA_IN_TOKEN_INTERRUPT)) {
  2044. if (ep->dev->protocol_stall) {
  2045. ep->stopped = 1;
  2046. set_halt(ep);
  2047. }
  2048. mode = 2;
  2049. /* an extra OUT token is an error */
  2050. } else if (((t & BIT(DATA_OUT_PING_TOKEN_INTERRUPT)) &&
  2051. req &&
  2052. req->req.actual == req->req.length) ||
  2053. (ep->responded && !req)) {
  2054. ep->dev->protocol_stall = 1;
  2055. set_halt(ep);
  2056. ep->stopped = 1;
  2057. if (req)
  2058. done(ep, req, -EOVERFLOW);
  2059. req = NULL;
  2060. }
  2061. }
  2062. }
  2063. if (unlikely(!req))
  2064. return;
  2065. /* manual DMA queue advance after short OUT */
  2066. if (likely(ep->dma)) {
  2067. if (t & BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
  2068. u32 count;
  2069. int stopped = ep->stopped;
  2070. /* TRANSFERRED works around OUT_DONE erratum 0112.
  2071. * we expect (N <= maxpacket) bytes; host wrote M.
  2072. * iff (M < N) we won't ever see a DMA interrupt.
  2073. */
  2074. ep->stopped = 1;
  2075. for (count = 0; ; t = readl(&ep->regs->ep_stat)) {
  2076. /* any preceding dma transfers must finish.
  2077. * dma handles (M >= N), may empty the queue
  2078. */
  2079. scan_dma_completions(ep);
  2080. if (unlikely(list_empty(&ep->queue) ||
  2081. ep->out_overflow)) {
  2082. req = NULL;
  2083. break;
  2084. }
  2085. req = list_entry(ep->queue.next,
  2086. struct net2280_request, queue);
  2087. /* here either (M < N), a "real" short rx;
  2088. * or (M == N) and the queue didn't empty
  2089. */
  2090. if (likely(t & BIT(FIFO_EMPTY))) {
  2091. count = readl(&ep->dma->dmacount);
  2092. count &= DMA_BYTE_COUNT_MASK;
  2093. if (readl(&ep->dma->dmadesc)
  2094. != req->td_dma)
  2095. req = NULL;
  2096. break;
  2097. }
  2098. udelay(1);
  2099. }
  2100. /* stop DMA, leave ep NAKing */
  2101. writel(BIT(DMA_ABORT), &ep->dma->dmastat);
  2102. spin_stop_dma(ep->dma);
  2103. if (likely(req)) {
  2104. req->td->dmacount = 0;
  2105. t = readl(&ep->regs->ep_avail);
  2106. dma_done(ep, req, count,
  2107. (ep->out_overflow || t)
  2108. ? -EOVERFLOW : 0);
  2109. }
  2110. /* also flush to prevent erratum 0106 trouble */
  2111. if (unlikely(ep->out_overflow ||
  2112. (ep->dev->chiprev == 0x0100 &&
  2113. ep->dev->gadget.speed
  2114. == USB_SPEED_FULL))) {
  2115. out_flush(ep);
  2116. ep->out_overflow = 0;
  2117. }
  2118. /* (re)start dma if needed, stop NAKing */
  2119. ep->stopped = stopped;
  2120. if (!list_empty(&ep->queue))
  2121. restart_dma(ep);
  2122. } else
  2123. ep_dbg(ep->dev, "%s dma ep_stat %08x ??\n",
  2124. ep->ep.name, t);
  2125. return;
  2126. /* data packet(s) received (in the fifo, OUT) */
  2127. } else if (t & BIT(DATA_PACKET_RECEIVED_INTERRUPT)) {
  2128. if (read_fifo(ep, req) && ep->num != 0)
  2129. mode = 2;
  2130. /* data packet(s) transmitted (IN) */
  2131. } else if (t & BIT(DATA_PACKET_TRANSMITTED_INTERRUPT)) {
  2132. unsigned len;
  2133. len = req->req.length - req->req.actual;
  2134. if (len > ep->ep.maxpacket)
  2135. len = ep->ep.maxpacket;
  2136. req->req.actual += len;
  2137. /* if we wrote it all, we're usually done */
  2138. /* send zlps until the status stage */
  2139. if ((req->req.actual == req->req.length) &&
  2140. (!req->req.zero || len != ep->ep.maxpacket) && ep->num)
  2141. mode = 2;
  2142. /* there was nothing to do ... */
  2143. } else if (mode == 1)
  2144. return;
  2145. /* done */
  2146. if (mode == 2) {
  2147. /* stream endpoints often resubmit/unlink in completion */
  2148. done(ep, req, 0);
  2149. /* maybe advance queue to next request */
  2150. if (ep->num == 0) {
  2151. /* NOTE: net2280 could let gadget driver start the
  2152. * status stage later. since not all controllers let
  2153. * them control that, the api doesn't (yet) allow it.
  2154. */
  2155. if (!ep->stopped)
  2156. allow_status(ep);
  2157. req = NULL;
  2158. } else {
  2159. if (!list_empty(&ep->queue) && !ep->stopped)
  2160. req = list_entry(ep->queue.next,
  2161. struct net2280_request, queue);
  2162. else
  2163. req = NULL;
  2164. if (req && !ep->is_in)
  2165. stop_out_naking(ep);
  2166. }
  2167. }
  2168. /* is there a buffer for the next packet?
  2169. * for best streaming performance, make sure there is one.
  2170. */
  2171. if (req && !ep->stopped) {
  2172. /* load IN fifo with next packet (may be zlp) */
  2173. if (t & BIT(DATA_PACKET_TRANSMITTED_INTERRUPT))
  2174. write_fifo(ep, &req->req);
  2175. }
  2176. }
  2177. static struct net2280_ep *get_ep_by_addr(struct net2280 *dev, u16 wIndex)
  2178. {
  2179. struct net2280_ep *ep;
  2180. if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
  2181. return &dev->ep[0];
  2182. list_for_each_entry(ep, &dev->gadget.ep_list, ep.ep_list) {
  2183. u8 bEndpointAddress;
  2184. if (!ep->desc)
  2185. continue;
  2186. bEndpointAddress = ep->desc->bEndpointAddress;
  2187. if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
  2188. continue;
  2189. if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
  2190. return ep;
  2191. }
  2192. return NULL;
  2193. }
  2194. static void defect7374_workaround(struct net2280 *dev, struct usb_ctrlrequest r)
  2195. {
  2196. u32 scratch, fsmvalue;
  2197. u32 ack_wait_timeout, state;
  2198. /* Workaround for Defect 7374 (U1/U2 erroneously rejected): */
  2199. scratch = get_idx_reg(dev->regs, SCRATCH);
  2200. fsmvalue = scratch & (0xf << DEFECT7374_FSM_FIELD);
  2201. scratch &= ~(0xf << DEFECT7374_FSM_FIELD);
  2202. if (!((fsmvalue == DEFECT7374_FSM_WAITING_FOR_CONTROL_READ) &&
  2203. (r.bRequestType & USB_DIR_IN)))
  2204. return;
  2205. /* This is the first Control Read for this connection: */
  2206. if (!(readl(&dev->usb->usbstat) & BIT(SUPER_SPEED_MODE))) {
  2207. /*
  2208. * Connection is NOT SS:
  2209. * - Connection must be FS or HS.
  2210. * - This FSM state should allow workaround software to
  2211. * run after the next USB connection.
  2212. */
  2213. scratch |= DEFECT7374_FSM_NON_SS_CONTROL_READ;
  2214. dev->bug7734_patched = 1;
  2215. goto restore_data_eps;
  2216. }
  2217. /* Connection is SS: */
  2218. for (ack_wait_timeout = 0;
  2219. ack_wait_timeout < DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS;
  2220. ack_wait_timeout++) {
  2221. state = readl(&dev->plregs->pl_ep_status_1)
  2222. & (0xff << STATE);
  2223. if ((state >= (ACK_GOOD_NORMAL << STATE)) &&
  2224. (state <= (ACK_GOOD_MORE_ACKS_TO_COME << STATE))) {
  2225. scratch |= DEFECT7374_FSM_SS_CONTROL_READ;
  2226. dev->bug7734_patched = 1;
  2227. break;
  2228. }
  2229. /*
  2230. * We have not yet received host's Data Phase ACK
  2231. * - Wait and try again.
  2232. */
  2233. udelay(DEFECT_7374_PROCESSOR_WAIT_TIME);
  2234. continue;
  2235. }
  2236. if (ack_wait_timeout >= DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS) {
  2237. ep_err(dev, "FAIL: Defect 7374 workaround waited but failed "
  2238. "to detect SS host's data phase ACK.");
  2239. ep_err(dev, "PL_EP_STATUS_1(23:16):.Expected from 0x11 to 0x16"
  2240. "got 0x%2.2x.\n", state >> STATE);
  2241. } else {
  2242. ep_warn(dev, "INFO: Defect 7374 workaround waited about\n"
  2243. "%duSec for Control Read Data Phase ACK\n",
  2244. DEFECT_7374_PROCESSOR_WAIT_TIME * ack_wait_timeout);
  2245. }
  2246. restore_data_eps:
  2247. /*
  2248. * Restore data EPs to their pre-workaround settings (disabled,
  2249. * initialized, and other details).
  2250. */
  2251. defect7374_disable_data_eps(dev);
  2252. set_idx_reg(dev->regs, SCRATCH, scratch);
  2253. return;
  2254. }
  2255. static void ep_clear_seqnum(struct net2280_ep *ep)
  2256. {
  2257. struct net2280 *dev = ep->dev;
  2258. u32 val;
  2259. static const u32 ep_pl[9] = { 0, 3, 4, 7, 8, 2, 5, 6, 9 };
  2260. val = readl(&dev->plregs->pl_ep_ctrl) & ~0x1f;
  2261. val |= ep_pl[ep->num];
  2262. writel(val, &dev->plregs->pl_ep_ctrl);
  2263. val |= BIT(SEQUENCE_NUMBER_RESET);
  2264. writel(val, &dev->plregs->pl_ep_ctrl);
  2265. return;
  2266. }
  2267. static void handle_stat0_irqs_superspeed(struct net2280 *dev,
  2268. struct net2280_ep *ep, struct usb_ctrlrequest r)
  2269. {
  2270. int tmp = 0;
  2271. #define w_value le16_to_cpu(r.wValue)
  2272. #define w_index le16_to_cpu(r.wIndex)
  2273. #define w_length le16_to_cpu(r.wLength)
  2274. switch (r.bRequest) {
  2275. struct net2280_ep *e;
  2276. u16 status;
  2277. case USB_REQ_SET_CONFIGURATION:
  2278. dev->addressed_state = !w_value;
  2279. goto usb3_delegate;
  2280. case USB_REQ_GET_STATUS:
  2281. switch (r.bRequestType) {
  2282. case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
  2283. status = dev->wakeup_enable ? 0x02 : 0x00;
  2284. if (dev->gadget.is_selfpowered)
  2285. status |= BIT(0);
  2286. status |= (dev->u1_enable << 2 | dev->u2_enable << 3 |
  2287. dev->ltm_enable << 4);
  2288. writel(0, &dev->epregs[0].ep_irqenb);
  2289. set_fifo_bytecount(ep, sizeof(status));
  2290. writel((__force u32) status, &dev->epregs[0].ep_data);
  2291. allow_status_338x(ep);
  2292. break;
  2293. case (USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
  2294. e = get_ep_by_addr(dev, w_index);
  2295. if (!e)
  2296. goto do_stall3;
  2297. status = readl(&e->regs->ep_rsp) &
  2298. BIT(CLEAR_ENDPOINT_HALT);
  2299. writel(0, &dev->epregs[0].ep_irqenb);
  2300. set_fifo_bytecount(ep, sizeof(status));
  2301. writel((__force u32) status, &dev->epregs[0].ep_data);
  2302. allow_status_338x(ep);
  2303. break;
  2304. default:
  2305. goto usb3_delegate;
  2306. }
  2307. break;
  2308. case USB_REQ_CLEAR_FEATURE:
  2309. switch (r.bRequestType) {
  2310. case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
  2311. if (!dev->addressed_state) {
  2312. switch (w_value) {
  2313. case USB_DEVICE_U1_ENABLE:
  2314. dev->u1_enable = 0;
  2315. writel(readl(&dev->usb_ext->usbctl2) &
  2316. ~BIT(U1_ENABLE),
  2317. &dev->usb_ext->usbctl2);
  2318. allow_status_338x(ep);
  2319. goto next_endpoints3;
  2320. case USB_DEVICE_U2_ENABLE:
  2321. dev->u2_enable = 0;
  2322. writel(readl(&dev->usb_ext->usbctl2) &
  2323. ~BIT(U2_ENABLE),
  2324. &dev->usb_ext->usbctl2);
  2325. allow_status_338x(ep);
  2326. goto next_endpoints3;
  2327. case USB_DEVICE_LTM_ENABLE:
  2328. dev->ltm_enable = 0;
  2329. writel(readl(&dev->usb_ext->usbctl2) &
  2330. ~BIT(LTM_ENABLE),
  2331. &dev->usb_ext->usbctl2);
  2332. allow_status_338x(ep);
  2333. goto next_endpoints3;
  2334. default:
  2335. break;
  2336. }
  2337. }
  2338. if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
  2339. dev->wakeup_enable = 0;
  2340. writel(readl(&dev->usb->usbctl) &
  2341. ~BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
  2342. &dev->usb->usbctl);
  2343. allow_status_338x(ep);
  2344. break;
  2345. }
  2346. goto usb3_delegate;
  2347. case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
  2348. e = get_ep_by_addr(dev, w_index);
  2349. if (!e)
  2350. goto do_stall3;
  2351. if (w_value != USB_ENDPOINT_HALT)
  2352. goto do_stall3;
  2353. ep_vdbg(dev, "%s clear halt\n", e->ep.name);
  2354. /*
  2355. * Workaround for SS SeqNum not cleared via
  2356. * Endpoint Halt (Clear) bit. select endpoint
  2357. */
  2358. ep_clear_seqnum(e);
  2359. clear_halt(e);
  2360. if (!list_empty(&e->queue) && e->td_dma)
  2361. restart_dma(e);
  2362. allow_status(ep);
  2363. ep->stopped = 1;
  2364. break;
  2365. default:
  2366. goto usb3_delegate;
  2367. }
  2368. break;
  2369. case USB_REQ_SET_FEATURE:
  2370. switch (r.bRequestType) {
  2371. case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE):
  2372. if (!dev->addressed_state) {
  2373. switch (w_value) {
  2374. case USB_DEVICE_U1_ENABLE:
  2375. dev->u1_enable = 1;
  2376. writel(readl(&dev->usb_ext->usbctl2) |
  2377. BIT(U1_ENABLE),
  2378. &dev->usb_ext->usbctl2);
  2379. allow_status_338x(ep);
  2380. goto next_endpoints3;
  2381. case USB_DEVICE_U2_ENABLE:
  2382. dev->u2_enable = 1;
  2383. writel(readl(&dev->usb_ext->usbctl2) |
  2384. BIT(U2_ENABLE),
  2385. &dev->usb_ext->usbctl2);
  2386. allow_status_338x(ep);
  2387. goto next_endpoints3;
  2388. case USB_DEVICE_LTM_ENABLE:
  2389. dev->ltm_enable = 1;
  2390. writel(readl(&dev->usb_ext->usbctl2) |
  2391. BIT(LTM_ENABLE),
  2392. &dev->usb_ext->usbctl2);
  2393. allow_status_338x(ep);
  2394. goto next_endpoints3;
  2395. default:
  2396. break;
  2397. }
  2398. }
  2399. if (w_value == USB_DEVICE_REMOTE_WAKEUP) {
  2400. dev->wakeup_enable = 1;
  2401. writel(readl(&dev->usb->usbctl) |
  2402. BIT(DEVICE_REMOTE_WAKEUP_ENABLE),
  2403. &dev->usb->usbctl);
  2404. allow_status_338x(ep);
  2405. break;
  2406. }
  2407. goto usb3_delegate;
  2408. case (USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
  2409. e = get_ep_by_addr(dev, w_index);
  2410. if (!e || (w_value != USB_ENDPOINT_HALT))
  2411. goto do_stall3;
  2412. ep->stopped = 1;
  2413. if (ep->num == 0)
  2414. ep->dev->protocol_stall = 1;
  2415. else {
  2416. if (ep->dma)
  2417. abort_dma(ep);
  2418. set_halt(ep);
  2419. }
  2420. allow_status_338x(ep);
  2421. break;
  2422. default:
  2423. goto usb3_delegate;
  2424. }
  2425. break;
  2426. default:
  2427. usb3_delegate:
  2428. ep_vdbg(dev, "setup %02x.%02x v%04x i%04x l%04x ep_cfg %08x\n",
  2429. r.bRequestType, r.bRequest,
  2430. w_value, w_index, w_length,
  2431. readl(&ep->cfg->ep_cfg));
  2432. ep->responded = 0;
  2433. spin_unlock(&dev->lock);
  2434. tmp = dev->driver->setup(&dev->gadget, &r);
  2435. spin_lock(&dev->lock);
  2436. }
  2437. do_stall3:
  2438. if (tmp < 0) {
  2439. ep_vdbg(dev, "req %02x.%02x protocol STALL; stat %d\n",
  2440. r.bRequestType, r.bRequest, tmp);
  2441. dev->protocol_stall = 1;
  2442. /* TD 9.9 Halt Endpoint test. TD 9.22 Set feature test */
  2443. set_halt(ep);
  2444. }
  2445. next_endpoints3:
  2446. #undef w_value
  2447. #undef w_index
  2448. #undef w_length
  2449. return;
  2450. }
  2451. static void handle_stat0_irqs(struct net2280 *dev, u32 stat)
  2452. {
  2453. struct net2280_ep *ep;
  2454. u32 num, scratch;
  2455. /* most of these don't need individual acks */
  2456. stat &= ~BIT(INTA_ASSERTED);
  2457. if (!stat)
  2458. return;
  2459. /* ep_dbg(dev, "irqstat0 %04x\n", stat); */
  2460. /* starting a control request? */
  2461. if (unlikely(stat & BIT(SETUP_PACKET_INTERRUPT))) {
  2462. union {
  2463. u32 raw[2];
  2464. struct usb_ctrlrequest r;
  2465. } u;
  2466. int tmp;
  2467. struct net2280_request *req;
  2468. if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
  2469. u32 val = readl(&dev->usb->usbstat);
  2470. if (val & BIT(SUPER_SPEED)) {
  2471. dev->gadget.speed = USB_SPEED_SUPER;
  2472. usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
  2473. EP0_SS_MAX_PACKET_SIZE);
  2474. } else if (val & BIT(HIGH_SPEED)) {
  2475. dev->gadget.speed = USB_SPEED_HIGH;
  2476. usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
  2477. EP0_HS_MAX_PACKET_SIZE);
  2478. } else {
  2479. dev->gadget.speed = USB_SPEED_FULL;
  2480. usb_ep_set_maxpacket_limit(&dev->ep[0].ep,
  2481. EP0_HS_MAX_PACKET_SIZE);
  2482. }
  2483. net2280_led_speed(dev, dev->gadget.speed);
  2484. ep_dbg(dev, "%s\n",
  2485. usb_speed_string(dev->gadget.speed));
  2486. }
  2487. ep = &dev->ep[0];
  2488. ep->irqs++;
  2489. /* make sure any leftover request state is cleared */
  2490. stat &= ~BIT(ENDPOINT_0_INTERRUPT);
  2491. while (!list_empty(&ep->queue)) {
  2492. req = list_entry(ep->queue.next,
  2493. struct net2280_request, queue);
  2494. done(ep, req, (req->req.actual == req->req.length)
  2495. ? 0 : -EPROTO);
  2496. }
  2497. ep->stopped = 0;
  2498. dev->protocol_stall = 0;
  2499. if (!(dev->quirks & PLX_SUPERSPEED)) {
  2500. if (ep->dev->quirks & PLX_2280)
  2501. tmp = BIT(FIFO_OVERFLOW) |
  2502. BIT(FIFO_UNDERFLOW);
  2503. else
  2504. tmp = 0;
  2505. writel(tmp | BIT(TIMEOUT) |
  2506. BIT(USB_STALL_SENT) |
  2507. BIT(USB_IN_NAK_SENT) |
  2508. BIT(USB_IN_ACK_RCVD) |
  2509. BIT(USB_OUT_PING_NAK_SENT) |
  2510. BIT(USB_OUT_ACK_SENT) |
  2511. BIT(SHORT_PACKET_OUT_DONE_INTERRUPT) |
  2512. BIT(SHORT_PACKET_TRANSFERRED_INTERRUPT) |
  2513. BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
  2514. BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
  2515. BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
  2516. BIT(DATA_IN_TOKEN_INTERRUPT),
  2517. &ep->regs->ep_stat);
  2518. }
  2519. u.raw[0] = readl(&dev->usb->setup0123);
  2520. u.raw[1] = readl(&dev->usb->setup4567);
  2521. cpu_to_le32s(&u.raw[0]);
  2522. cpu_to_le32s(&u.raw[1]);
  2523. if ((dev->quirks & PLX_SUPERSPEED) && !dev->bug7734_patched)
  2524. defect7374_workaround(dev, u.r);
  2525. tmp = 0;
  2526. #define w_value le16_to_cpu(u.r.wValue)
  2527. #define w_index le16_to_cpu(u.r.wIndex)
  2528. #define w_length le16_to_cpu(u.r.wLength)
  2529. /* ack the irq */
  2530. writel(BIT(SETUP_PACKET_INTERRUPT), &dev->regs->irqstat0);
  2531. stat ^= BIT(SETUP_PACKET_INTERRUPT);
  2532. /* watch control traffic at the token level, and force
  2533. * synchronization before letting the status stage happen.
  2534. * FIXME ignore tokens we'll NAK, until driver responds.
  2535. * that'll mean a lot less irqs for some drivers.
  2536. */
  2537. ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
  2538. if (ep->is_in) {
  2539. scratch = BIT(DATA_PACKET_TRANSMITTED_INTERRUPT) |
  2540. BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
  2541. BIT(DATA_IN_TOKEN_INTERRUPT);
  2542. stop_out_naking(ep);
  2543. } else
  2544. scratch = BIT(DATA_PACKET_RECEIVED_INTERRUPT) |
  2545. BIT(DATA_OUT_PING_TOKEN_INTERRUPT) |
  2546. BIT(DATA_IN_TOKEN_INTERRUPT);
  2547. writel(scratch, &dev->epregs[0].ep_irqenb);
  2548. /* we made the hardware handle most lowlevel requests;
  2549. * everything else goes uplevel to the gadget code.
  2550. */
  2551. ep->responded = 1;
  2552. if (dev->gadget.speed == USB_SPEED_SUPER) {
  2553. handle_stat0_irqs_superspeed(dev, ep, u.r);
  2554. goto next_endpoints;
  2555. }
  2556. switch (u.r.bRequest) {
  2557. case USB_REQ_GET_STATUS: {
  2558. struct net2280_ep *e;
  2559. __le32 status;
  2560. /* hw handles device and interface status */
  2561. if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
  2562. goto delegate;
  2563. e = get_ep_by_addr(dev, w_index);
  2564. if (!e || w_length > 2)
  2565. goto do_stall;
  2566. if (readl(&e->regs->ep_rsp) & BIT(SET_ENDPOINT_HALT))
  2567. status = cpu_to_le32(1);
  2568. else
  2569. status = cpu_to_le32(0);
  2570. /* don't bother with a request object! */
  2571. writel(0, &dev->epregs[0].ep_irqenb);
  2572. set_fifo_bytecount(ep, w_length);
  2573. writel((__force u32)status, &dev->epregs[0].ep_data);
  2574. allow_status(ep);
  2575. ep_vdbg(dev, "%s stat %02x\n", ep->ep.name, status);
  2576. goto next_endpoints;
  2577. }
  2578. break;
  2579. case USB_REQ_CLEAR_FEATURE: {
  2580. struct net2280_ep *e;
  2581. /* hw handles device features */
  2582. if (u.r.bRequestType != USB_RECIP_ENDPOINT)
  2583. goto delegate;
  2584. if (w_value != USB_ENDPOINT_HALT || w_length != 0)
  2585. goto do_stall;
  2586. e = get_ep_by_addr(dev, w_index);
  2587. if (!e)
  2588. goto do_stall;
  2589. if (e->wedged) {
  2590. ep_vdbg(dev, "%s wedged, halt not cleared\n",
  2591. ep->ep.name);
  2592. } else {
  2593. ep_vdbg(dev, "%s clear halt\n", e->ep.name);
  2594. clear_halt(e);
  2595. if ((ep->dev->quirks & PLX_SUPERSPEED) &&
  2596. !list_empty(&e->queue) && e->td_dma)
  2597. restart_dma(e);
  2598. }
  2599. allow_status(ep);
  2600. goto next_endpoints;
  2601. }
  2602. break;
  2603. case USB_REQ_SET_FEATURE: {
  2604. struct net2280_ep *e;
  2605. /* hw handles device features */
  2606. if (u.r.bRequestType != USB_RECIP_ENDPOINT)
  2607. goto delegate;
  2608. if (w_value != USB_ENDPOINT_HALT || w_length != 0)
  2609. goto do_stall;
  2610. e = get_ep_by_addr(dev, w_index);
  2611. if (!e)
  2612. goto do_stall;
  2613. if (e->ep.name == ep0name)
  2614. goto do_stall;
  2615. set_halt(e);
  2616. if ((dev->quirks & PLX_SUPERSPEED) && e->dma)
  2617. abort_dma(e);
  2618. allow_status(ep);
  2619. ep_vdbg(dev, "%s set halt\n", ep->ep.name);
  2620. goto next_endpoints;
  2621. }
  2622. break;
  2623. default:
  2624. delegate:
  2625. ep_vdbg(dev, "setup %02x.%02x v%04x i%04x l%04x "
  2626. "ep_cfg %08x\n",
  2627. u.r.bRequestType, u.r.bRequest,
  2628. w_value, w_index, w_length,
  2629. readl(&ep->cfg->ep_cfg));
  2630. ep->responded = 0;
  2631. spin_unlock(&dev->lock);
  2632. tmp = dev->driver->setup(&dev->gadget, &u.r);
  2633. spin_lock(&dev->lock);
  2634. }
  2635. /* stall ep0 on error */
  2636. if (tmp < 0) {
  2637. do_stall:
  2638. ep_vdbg(dev, "req %02x.%02x protocol STALL; stat %d\n",
  2639. u.r.bRequestType, u.r.bRequest, tmp);
  2640. dev->protocol_stall = 1;
  2641. }
  2642. /* some in/out token irq should follow; maybe stall then.
  2643. * driver must queue a request (even zlp) or halt ep0
  2644. * before the host times out.
  2645. */
  2646. }
  2647. #undef w_value
  2648. #undef w_index
  2649. #undef w_length
  2650. next_endpoints:
  2651. /* endpoint data irq ? */
  2652. scratch = stat & 0x7f;
  2653. stat &= ~0x7f;
  2654. for (num = 0; scratch; num++) {
  2655. u32 t;
  2656. /* do this endpoint's FIFO and queue need tending? */
  2657. t = BIT(num);
  2658. if ((scratch & t) == 0)
  2659. continue;
  2660. scratch ^= t;
  2661. ep = &dev->ep[num];
  2662. handle_ep_small(ep);
  2663. }
  2664. if (stat)
  2665. ep_dbg(dev, "unhandled irqstat0 %08x\n", stat);
  2666. }
  2667. #define DMA_INTERRUPTS (BIT(DMA_D_INTERRUPT) | \
  2668. BIT(DMA_C_INTERRUPT) | \
  2669. BIT(DMA_B_INTERRUPT) | \
  2670. BIT(DMA_A_INTERRUPT))
  2671. #define PCI_ERROR_INTERRUPTS ( \
  2672. BIT(PCI_MASTER_ABORT_RECEIVED_INTERRUPT) | \
  2673. BIT(PCI_TARGET_ABORT_RECEIVED_INTERRUPT) | \
  2674. BIT(PCI_RETRY_ABORT_INTERRUPT))
  2675. static void handle_stat1_irqs(struct net2280 *dev, u32 stat)
  2676. __releases(dev->lock)
  2677. __acquires(dev->lock)
  2678. {
  2679. struct net2280_ep *ep;
  2680. u32 tmp, num, mask, scratch;
  2681. /* after disconnect there's nothing else to do! */
  2682. tmp = BIT(VBUS_INTERRUPT) | BIT(ROOT_PORT_RESET_INTERRUPT);
  2683. mask = BIT(SUPER_SPEED) | BIT(HIGH_SPEED) | BIT(FULL_SPEED);
  2684. /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
  2685. * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRUPT set and
  2686. * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
  2687. * only indicates a change in the reset state).
  2688. */
  2689. if (stat & tmp) {
  2690. bool reset = false;
  2691. bool disconnect = false;
  2692. /*
  2693. * Ignore disconnects and resets if the speed hasn't been set.
  2694. * VBUS can bounce and there's always an initial reset.
  2695. */
  2696. writel(tmp, &dev->regs->irqstat1);
  2697. if (dev->gadget.speed != USB_SPEED_UNKNOWN) {
  2698. if ((stat & BIT(VBUS_INTERRUPT)) &&
  2699. (readl(&dev->usb->usbctl) &
  2700. BIT(VBUS_PIN)) == 0) {
  2701. disconnect = true;
  2702. ep_dbg(dev, "disconnect %s\n",
  2703. dev->driver->driver.name);
  2704. } else if ((stat & BIT(ROOT_PORT_RESET_INTERRUPT)) &&
  2705. (readl(&dev->usb->usbstat) & mask)
  2706. == 0) {
  2707. reset = true;
  2708. ep_dbg(dev, "reset %s\n",
  2709. dev->driver->driver.name);
  2710. }
  2711. if (disconnect || reset) {
  2712. stop_activity(dev, dev->driver);
  2713. ep0_start(dev);
  2714. spin_unlock(&dev->lock);
  2715. if (reset)
  2716. usb_gadget_udc_reset
  2717. (&dev->gadget, dev->driver);
  2718. else
  2719. (dev->driver->disconnect)
  2720. (&dev->gadget);
  2721. spin_lock(&dev->lock);
  2722. return;
  2723. }
  2724. }
  2725. stat &= ~tmp;
  2726. /* vBUS can bounce ... one of many reasons to ignore the
  2727. * notion of hotplug events on bus connect/disconnect!
  2728. */
  2729. if (!stat)
  2730. return;
  2731. }
  2732. /* NOTE: chip stays in PCI D0 state for now, but it could
  2733. * enter D1 to save more power
  2734. */
  2735. tmp = BIT(SUSPEND_REQUEST_CHANGE_INTERRUPT);
  2736. if (stat & tmp) {
  2737. writel(tmp, &dev->regs->irqstat1);
  2738. if (stat & BIT(SUSPEND_REQUEST_INTERRUPT)) {
  2739. if (dev->driver->suspend)
  2740. dev->driver->suspend(&dev->gadget);
  2741. if (!enable_suspend)
  2742. stat &= ~BIT(SUSPEND_REQUEST_INTERRUPT);
  2743. } else {
  2744. if (dev->driver->resume)
  2745. dev->driver->resume(&dev->gadget);
  2746. /* at high speed, note erratum 0133 */
  2747. }
  2748. stat &= ~tmp;
  2749. }
  2750. /* clear any other status/irqs */
  2751. if (stat)
  2752. writel(stat, &dev->regs->irqstat1);
  2753. /* some status we can just ignore */
  2754. if (dev->quirks & PLX_2280)
  2755. stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
  2756. BIT(SUSPEND_REQUEST_INTERRUPT) |
  2757. BIT(RESUME_INTERRUPT) |
  2758. BIT(SOF_INTERRUPT));
  2759. else
  2760. stat &= ~(BIT(CONTROL_STATUS_INTERRUPT) |
  2761. BIT(RESUME_INTERRUPT) |
  2762. BIT(SOF_DOWN_INTERRUPT) |
  2763. BIT(SOF_INTERRUPT));
  2764. if (!stat)
  2765. return;
  2766. /* ep_dbg(dev, "irqstat1 %08x\n", stat);*/
  2767. /* DMA status, for ep-{a,b,c,d} */
  2768. scratch = stat & DMA_INTERRUPTS;
  2769. stat &= ~DMA_INTERRUPTS;
  2770. scratch >>= 9;
  2771. for (num = 0; scratch; num++) {
  2772. struct net2280_dma_regs __iomem *dma;
  2773. tmp = BIT(num);
  2774. if ((tmp & scratch) == 0)
  2775. continue;
  2776. scratch ^= tmp;
  2777. ep = &dev->ep[num + 1];
  2778. dma = ep->dma;
  2779. if (!dma)
  2780. continue;
  2781. /* clear ep's dma status */
  2782. tmp = readl(&dma->dmastat);
  2783. writel(tmp, &dma->dmastat);
  2784. /* dma sync*/
  2785. if (dev->quirks & PLX_SUPERSPEED) {
  2786. u32 r_dmacount = readl(&dma->dmacount);
  2787. if (!ep->is_in && (r_dmacount & 0x00FFFFFF) &&
  2788. (tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT)))
  2789. continue;
  2790. }
  2791. if (!(tmp & BIT(DMA_TRANSACTION_DONE_INTERRUPT))) {
  2792. ep_dbg(ep->dev, "%s no xact done? %08x\n",
  2793. ep->ep.name, tmp);
  2794. continue;
  2795. }
  2796. stop_dma(ep->dma);
  2797. /* OUT transfers terminate when the data from the
  2798. * host is in our memory. Process whatever's done.
  2799. * On this path, we know transfer's last packet wasn't
  2800. * less than req->length. NAK_OUT_PACKETS may be set,
  2801. * or the FIFO may already be holding new packets.
  2802. *
  2803. * IN transfers can linger in the FIFO for a very
  2804. * long time ... we ignore that for now, accounting
  2805. * precisely (like PIO does) needs per-packet irqs
  2806. */
  2807. scan_dma_completions(ep);
  2808. /* disable dma on inactive queues; else maybe restart */
  2809. if (!list_empty(&ep->queue)) {
  2810. tmp = readl(&dma->dmactl);
  2811. restart_dma(ep);
  2812. }
  2813. ep->irqs++;
  2814. }
  2815. /* NOTE: there are other PCI errors we might usefully notice.
  2816. * if they appear very often, here's where to try recovering.
  2817. */
  2818. if (stat & PCI_ERROR_INTERRUPTS) {
  2819. ep_err(dev, "pci dma error; stat %08x\n", stat);
  2820. stat &= ~PCI_ERROR_INTERRUPTS;
  2821. /* these are fatal errors, but "maybe" they won't
  2822. * happen again ...
  2823. */
  2824. stop_activity(dev, dev->driver);
  2825. ep0_start(dev);
  2826. stat = 0;
  2827. }
  2828. if (stat)
  2829. ep_dbg(dev, "unhandled irqstat1 %08x\n", stat);
  2830. }
  2831. static irqreturn_t net2280_irq(int irq, void *_dev)
  2832. {
  2833. struct net2280 *dev = _dev;
  2834. /* shared interrupt, not ours */
  2835. if ((dev->quirks & PLX_LEGACY) &&
  2836. (!(readl(&dev->regs->irqstat0) & BIT(INTA_ASSERTED))))
  2837. return IRQ_NONE;
  2838. spin_lock(&dev->lock);
  2839. /* handle disconnect, dma, and more */
  2840. handle_stat1_irqs(dev, readl(&dev->regs->irqstat1));
  2841. /* control requests and PIO */
  2842. handle_stat0_irqs(dev, readl(&dev->regs->irqstat0));
  2843. if (dev->quirks & PLX_SUPERSPEED) {
  2844. /* re-enable interrupt to trigger any possible new interrupt */
  2845. u32 pciirqenb1 = readl(&dev->regs->pciirqenb1);
  2846. writel(pciirqenb1 & 0x7FFFFFFF, &dev->regs->pciirqenb1);
  2847. writel(pciirqenb1, &dev->regs->pciirqenb1);
  2848. }
  2849. spin_unlock(&dev->lock);
  2850. return IRQ_HANDLED;
  2851. }
  2852. /*-------------------------------------------------------------------------*/
  2853. static void gadget_release(struct device *_dev)
  2854. {
  2855. struct net2280 *dev = dev_get_drvdata(_dev);
  2856. kfree(dev);
  2857. }
  2858. /* tear down the binding between this driver and the pci device */
  2859. static void net2280_remove(struct pci_dev *pdev)
  2860. {
  2861. struct net2280 *dev = pci_get_drvdata(pdev);
  2862. usb_del_gadget_udc(&dev->gadget);
  2863. BUG_ON(dev->driver);
  2864. /* then clean up the resources we allocated during probe() */
  2865. net2280_led_shutdown(dev);
  2866. if (dev->requests) {
  2867. int i;
  2868. for (i = 1; i < 5; i++) {
  2869. if (!dev->ep[i].dummy)
  2870. continue;
  2871. pci_pool_free(dev->requests, dev->ep[i].dummy,
  2872. dev->ep[i].td_dma);
  2873. }
  2874. pci_pool_destroy(dev->requests);
  2875. }
  2876. if (dev->got_irq)
  2877. free_irq(pdev->irq, dev);
  2878. if (dev->quirks & PLX_SUPERSPEED)
  2879. pci_disable_msi(pdev);
  2880. if (dev->regs)
  2881. iounmap(dev->regs);
  2882. if (dev->region)
  2883. release_mem_region(pci_resource_start(pdev, 0),
  2884. pci_resource_len(pdev, 0));
  2885. if (dev->enabled)
  2886. pci_disable_device(pdev);
  2887. device_remove_file(&pdev->dev, &dev_attr_registers);
  2888. ep_info(dev, "unbind\n");
  2889. }
  2890. /* wrap this driver around the specified device, but
  2891. * don't respond over USB until a gadget driver binds to us.
  2892. */
  2893. static int net2280_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  2894. {
  2895. struct net2280 *dev;
  2896. unsigned long resource, len;
  2897. void __iomem *base = NULL;
  2898. int retval, i;
  2899. /* alloc, and start init */
  2900. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  2901. if (dev == NULL) {
  2902. retval = -ENOMEM;
  2903. goto done;
  2904. }
  2905. pci_set_drvdata(pdev, dev);
  2906. spin_lock_init(&dev->lock);
  2907. dev->quirks = id->driver_data;
  2908. dev->pdev = pdev;
  2909. dev->gadget.ops = &net2280_ops;
  2910. dev->gadget.max_speed = (dev->quirks & PLX_SUPERSPEED) ?
  2911. USB_SPEED_SUPER : USB_SPEED_HIGH;
  2912. /* the "gadget" abstracts/virtualizes the controller */
  2913. dev->gadget.name = driver_name;
  2914. /* now all the pci goodies ... */
  2915. if (pci_enable_device(pdev) < 0) {
  2916. retval = -ENODEV;
  2917. goto done;
  2918. }
  2919. dev->enabled = 1;
  2920. /* BAR 0 holds all the registers
  2921. * BAR 1 is 8051 memory; unused here (note erratum 0103)
  2922. * BAR 2 is fifo memory; unused here
  2923. */
  2924. resource = pci_resource_start(pdev, 0);
  2925. len = pci_resource_len(pdev, 0);
  2926. if (!request_mem_region(resource, len, driver_name)) {
  2927. ep_dbg(dev, "controller already in use\n");
  2928. retval = -EBUSY;
  2929. goto done;
  2930. }
  2931. dev->region = 1;
  2932. /* FIXME provide firmware download interface to put
  2933. * 8051 code into the chip, e.g. to turn on PCI PM.
  2934. */
  2935. base = ioremap_nocache(resource, len);
  2936. if (base == NULL) {
  2937. ep_dbg(dev, "can't map memory\n");
  2938. retval = -EFAULT;
  2939. goto done;
  2940. }
  2941. dev->regs = (struct net2280_regs __iomem *) base;
  2942. dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
  2943. dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
  2944. dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
  2945. dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
  2946. dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
  2947. if (dev->quirks & PLX_SUPERSPEED) {
  2948. u32 fsmvalue;
  2949. u32 usbstat;
  2950. dev->usb_ext = (struct usb338x_usb_ext_regs __iomem *)
  2951. (base + 0x00b4);
  2952. dev->llregs = (struct usb338x_ll_regs __iomem *)
  2953. (base + 0x0700);
  2954. dev->ll_lfps_regs = (struct usb338x_ll_lfps_regs __iomem *)
  2955. (base + 0x0748);
  2956. dev->ll_tsn_regs = (struct usb338x_ll_tsn_regs __iomem *)
  2957. (base + 0x077c);
  2958. dev->ll_chicken_reg = (struct usb338x_ll_chi_regs __iomem *)
  2959. (base + 0x079c);
  2960. dev->plregs = (struct usb338x_pl_regs __iomem *)
  2961. (base + 0x0800);
  2962. usbstat = readl(&dev->usb->usbstat);
  2963. dev->enhanced_mode = !!(usbstat & BIT(11));
  2964. dev->n_ep = (dev->enhanced_mode) ? 9 : 5;
  2965. /* put into initial config, link up all endpoints */
  2966. fsmvalue = get_idx_reg(dev->regs, SCRATCH) &
  2967. (0xf << DEFECT7374_FSM_FIELD);
  2968. /* See if firmware needs to set up for workaround: */
  2969. if (fsmvalue == DEFECT7374_FSM_SS_CONTROL_READ) {
  2970. dev->bug7734_patched = 1;
  2971. writel(0, &dev->usb->usbctl);
  2972. } else
  2973. dev->bug7734_patched = 0;
  2974. } else {
  2975. dev->enhanced_mode = 0;
  2976. dev->n_ep = 7;
  2977. /* put into initial config, link up all endpoints */
  2978. writel(0, &dev->usb->usbctl);
  2979. }
  2980. usb_reset(dev);
  2981. usb_reinit(dev);
  2982. /* irq setup after old hardware is cleaned up */
  2983. if (!pdev->irq) {
  2984. ep_err(dev, "No IRQ. Check PCI setup!\n");
  2985. retval = -ENODEV;
  2986. goto done;
  2987. }
  2988. if (dev->quirks & PLX_SUPERSPEED)
  2989. if (pci_enable_msi(pdev))
  2990. ep_err(dev, "Failed to enable MSI mode\n");
  2991. if (request_irq(pdev->irq, net2280_irq, IRQF_SHARED,
  2992. driver_name, dev)) {
  2993. ep_err(dev, "request interrupt %d failed\n", pdev->irq);
  2994. retval = -EBUSY;
  2995. goto done;
  2996. }
  2997. dev->got_irq = 1;
  2998. /* DMA setup */
  2999. /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
  3000. dev->requests = pci_pool_create("requests", pdev,
  3001. sizeof(struct net2280_dma),
  3002. 0 /* no alignment requirements */,
  3003. 0 /* or page-crossing issues */);
  3004. if (!dev->requests) {
  3005. ep_dbg(dev, "can't get request pool\n");
  3006. retval = -ENOMEM;
  3007. goto done;
  3008. }
  3009. for (i = 1; i < 5; i++) {
  3010. struct net2280_dma *td;
  3011. td = pci_pool_alloc(dev->requests, GFP_KERNEL,
  3012. &dev->ep[i].td_dma);
  3013. if (!td) {
  3014. ep_dbg(dev, "can't get dummy %d\n", i);
  3015. retval = -ENOMEM;
  3016. goto done;
  3017. }
  3018. td->dmacount = 0; /* not VALID */
  3019. td->dmadesc = td->dmaaddr;
  3020. dev->ep[i].dummy = td;
  3021. }
  3022. /* enable lower-overhead pci memory bursts during DMA */
  3023. if (dev->quirks & PLX_LEGACY)
  3024. writel(BIT(DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE) |
  3025. /*
  3026. * 256 write retries may not be enough...
  3027. BIT(PCI_RETRY_ABORT_ENABLE) |
  3028. */
  3029. BIT(DMA_READ_MULTIPLE_ENABLE) |
  3030. BIT(DMA_READ_LINE_ENABLE),
  3031. &dev->pci->pcimstctl);
  3032. /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
  3033. pci_set_master(pdev);
  3034. pci_try_set_mwi(pdev);
  3035. /* ... also flushes any posted pci writes */
  3036. dev->chiprev = get_idx_reg(dev->regs, REG_CHIPREV) & 0xffff;
  3037. /* done */
  3038. ep_info(dev, "%s\n", driver_desc);
  3039. ep_info(dev, "irq %d, pci mem %p, chip rev %04x\n",
  3040. pdev->irq, base, dev->chiprev);
  3041. ep_info(dev, "version: " DRIVER_VERSION "; %s\n",
  3042. dev->enhanced_mode ? "enhanced mode" : "legacy mode");
  3043. retval = device_create_file(&pdev->dev, &dev_attr_registers);
  3044. if (retval)
  3045. goto done;
  3046. retval = usb_add_gadget_udc_release(&pdev->dev, &dev->gadget,
  3047. gadget_release);
  3048. if (retval)
  3049. goto done;
  3050. return 0;
  3051. done:
  3052. if (dev)
  3053. net2280_remove(pdev);
  3054. return retval;
  3055. }
  3056. /* make sure the board is quiescent; otherwise it will continue
  3057. * generating IRQs across the upcoming reboot.
  3058. */
  3059. static void net2280_shutdown(struct pci_dev *pdev)
  3060. {
  3061. struct net2280 *dev = pci_get_drvdata(pdev);
  3062. /* disable IRQs */
  3063. writel(0, &dev->regs->pciirqenb0);
  3064. writel(0, &dev->regs->pciirqenb1);
  3065. /* disable the pullup so the host will think we're gone */
  3066. writel(0, &dev->usb->usbctl);
  3067. }
  3068. /*-------------------------------------------------------------------------*/
  3069. static const struct pci_device_id pci_ids[] = { {
  3070. .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
  3071. .class_mask = ~0,
  3072. .vendor = PCI_VENDOR_ID_PLX_LEGACY,
  3073. .device = 0x2280,
  3074. .subvendor = PCI_ANY_ID,
  3075. .subdevice = PCI_ANY_ID,
  3076. .driver_data = PLX_LEGACY | PLX_2280,
  3077. }, {
  3078. .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
  3079. .class_mask = ~0,
  3080. .vendor = PCI_VENDOR_ID_PLX_LEGACY,
  3081. .device = 0x2282,
  3082. .subvendor = PCI_ANY_ID,
  3083. .subdevice = PCI_ANY_ID,
  3084. .driver_data = PLX_LEGACY,
  3085. },
  3086. {
  3087. .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
  3088. .class_mask = ~0,
  3089. .vendor = PCI_VENDOR_ID_PLX,
  3090. .device = 0x3380,
  3091. .subvendor = PCI_ANY_ID,
  3092. .subdevice = PCI_ANY_ID,
  3093. .driver_data = PLX_SUPERSPEED,
  3094. },
  3095. {
  3096. .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
  3097. .class_mask = ~0,
  3098. .vendor = PCI_VENDOR_ID_PLX,
  3099. .device = 0x3382,
  3100. .subvendor = PCI_ANY_ID,
  3101. .subdevice = PCI_ANY_ID,
  3102. .driver_data = PLX_SUPERSPEED,
  3103. },
  3104. { /* end: all zeroes */ }
  3105. };
  3106. MODULE_DEVICE_TABLE(pci, pci_ids);
  3107. /* pci driver glue; this is a "new style" PCI driver module */
  3108. static struct pci_driver net2280_pci_driver = {
  3109. .name = (char *) driver_name,
  3110. .id_table = pci_ids,
  3111. .probe = net2280_probe,
  3112. .remove = net2280_remove,
  3113. .shutdown = net2280_shutdown,
  3114. /* FIXME add power management support */
  3115. };
  3116. module_pci_driver(net2280_pci_driver);
  3117. MODULE_DESCRIPTION(DRIVER_DESC);
  3118. MODULE_AUTHOR("David Brownell");
  3119. MODULE_LICENSE("GPL");