amd5536udc.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420
  1. /*
  2. * amd5536.c -- AMD 5536 UDC high/full speed USB device controller
  3. *
  4. * Copyright (C) 2005-2007 AMD (http://www.amd.com)
  5. * Author: Thomas Dahlmann
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. /*
  13. * The AMD5536 UDC is part of the x86 southbridge AMD Geode CS5536.
  14. * It is a USB Highspeed DMA capable USB device controller. Beside ep0 it
  15. * provides 4 IN and 4 OUT endpoints (bulk or interrupt type).
  16. *
  17. * Make sure that UDC is assigned to port 4 by BIOS settings (port can also
  18. * be used as host port) and UOC bits PAD_EN and APU are set (should be done
  19. * by BIOS init).
  20. *
  21. * UDC DMA requires 32-bit aligned buffers so DMA with gadget ether does not
  22. * work without updating NET_IP_ALIGN. Or PIO mode (module param "use_dma=0")
  23. * can be used with gadget ether.
  24. */
  25. /* debug control */
  26. /* #define UDC_VERBOSE */
  27. /* Driver strings */
  28. #define UDC_MOD_DESCRIPTION "AMD 5536 UDC - USB Device Controller"
  29. #define UDC_DRIVER_VERSION_STRING "01.00.0206"
  30. /* system */
  31. #include <linux/module.h>
  32. #include <linux/pci.h>
  33. #include <linux/kernel.h>
  34. #include <linux/delay.h>
  35. #include <linux/ioport.h>
  36. #include <linux/sched.h>
  37. #include <linux/slab.h>
  38. #include <linux/errno.h>
  39. #include <linux/timer.h>
  40. #include <linux/list.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/ioctl.h>
  43. #include <linux/fs.h>
  44. #include <linux/dmapool.h>
  45. #include <linux/moduleparam.h>
  46. #include <linux/device.h>
  47. #include <linux/io.h>
  48. #include <linux/irq.h>
  49. #include <linux/prefetch.h>
  50. #include <asm/byteorder.h>
  51. #include <asm/unaligned.h>
  52. /* gadget stack */
  53. #include <linux/usb/ch9.h>
  54. #include <linux/usb/gadget.h>
  55. /* udc specific */
  56. #include "amd5536udc.h"
  57. static void udc_tasklet_disconnect(unsigned long);
  58. static void empty_req_queue(struct udc_ep *);
  59. static void udc_setup_endpoints(struct udc *dev);
  60. static void udc_soft_reset(struct udc *dev);
  61. static struct udc_request *udc_alloc_bna_dummy(struct udc_ep *ep);
  62. static void udc_free_request(struct usb_ep *usbep, struct usb_request *usbreq);
  63. /* description */
  64. static const char mod_desc[] = UDC_MOD_DESCRIPTION;
  65. static const char name[] = "amd5536udc";
  66. /* structure to hold endpoint function pointers */
  67. static const struct usb_ep_ops udc_ep_ops;
  68. /* received setup data */
  69. static union udc_setup_data setup_data;
  70. /* pointer to device object */
  71. static struct udc *udc;
  72. /* irq spin lock for soft reset */
  73. static DEFINE_SPINLOCK(udc_irq_spinlock);
  74. /* stall spin lock */
  75. static DEFINE_SPINLOCK(udc_stall_spinlock);
  76. /*
  77. * slave mode: pending bytes in rx fifo after nyet,
  78. * used if EPIN irq came but no req was available
  79. */
  80. static unsigned int udc_rxfifo_pending;
  81. /* count soft resets after suspend to avoid loop */
  82. static int soft_reset_occured;
  83. static int soft_reset_after_usbreset_occured;
  84. /* timer */
  85. static struct timer_list udc_timer;
  86. static int stop_timer;
  87. /* set_rde -- Is used to control enabling of RX DMA. Problem is
  88. * that UDC has only one bit (RDE) to enable/disable RX DMA for
  89. * all OUT endpoints. So we have to handle race conditions like
  90. * when OUT data reaches the fifo but no request was queued yet.
  91. * This cannot be solved by letting the RX DMA disabled until a
  92. * request gets queued because there may be other OUT packets
  93. * in the FIFO (important for not blocking control traffic).
  94. * The value of set_rde controls the correspondig timer.
  95. *
  96. * set_rde -1 == not used, means it is alloed to be set to 0 or 1
  97. * set_rde 0 == do not touch RDE, do no start the RDE timer
  98. * set_rde 1 == timer function will look whether FIFO has data
  99. * set_rde 2 == set by timer function to enable RX DMA on next call
  100. */
  101. static int set_rde = -1;
  102. static DECLARE_COMPLETION(on_exit);
  103. static struct timer_list udc_pollstall_timer;
  104. static int stop_pollstall_timer;
  105. static DECLARE_COMPLETION(on_pollstall_exit);
  106. /* tasklet for usb disconnect */
  107. static DECLARE_TASKLET(disconnect_tasklet, udc_tasklet_disconnect,
  108. (unsigned long) &udc);
  109. /* endpoint names used for print */
  110. static const char ep0_string[] = "ep0in";
  111. static const struct {
  112. const char *name;
  113. const struct usb_ep_caps caps;
  114. } ep_info[] = {
  115. #define EP_INFO(_name, _caps) \
  116. { \
  117. .name = _name, \
  118. .caps = _caps, \
  119. }
  120. EP_INFO(ep0_string,
  121. USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL, USB_EP_CAPS_DIR_IN)),
  122. EP_INFO("ep1in-int",
  123. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
  124. EP_INFO("ep2in-bulk",
  125. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
  126. EP_INFO("ep3in-bulk",
  127. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
  128. EP_INFO("ep4in-bulk",
  129. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
  130. EP_INFO("ep5in-bulk",
  131. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
  132. EP_INFO("ep6in-bulk",
  133. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
  134. EP_INFO("ep7in-bulk",
  135. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
  136. EP_INFO("ep8in-bulk",
  137. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
  138. EP_INFO("ep9in-bulk",
  139. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
  140. EP_INFO("ep10in-bulk",
  141. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
  142. EP_INFO("ep11in-bulk",
  143. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
  144. EP_INFO("ep12in-bulk",
  145. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
  146. EP_INFO("ep13in-bulk",
  147. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
  148. EP_INFO("ep14in-bulk",
  149. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
  150. EP_INFO("ep15in-bulk",
  151. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_IN)),
  152. EP_INFO("ep0out",
  153. USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL, USB_EP_CAPS_DIR_OUT)),
  154. EP_INFO("ep1out-bulk",
  155. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
  156. EP_INFO("ep2out-bulk",
  157. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
  158. EP_INFO("ep3out-bulk",
  159. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
  160. EP_INFO("ep4out-bulk",
  161. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
  162. EP_INFO("ep5out-bulk",
  163. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
  164. EP_INFO("ep6out-bulk",
  165. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
  166. EP_INFO("ep7out-bulk",
  167. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
  168. EP_INFO("ep8out-bulk",
  169. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
  170. EP_INFO("ep9out-bulk",
  171. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
  172. EP_INFO("ep10out-bulk",
  173. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
  174. EP_INFO("ep11out-bulk",
  175. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
  176. EP_INFO("ep12out-bulk",
  177. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
  178. EP_INFO("ep13out-bulk",
  179. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
  180. EP_INFO("ep14out-bulk",
  181. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
  182. EP_INFO("ep15out-bulk",
  183. USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK, USB_EP_CAPS_DIR_OUT)),
  184. #undef EP_INFO
  185. };
  186. /* DMA usage flag */
  187. static bool use_dma = 1;
  188. /* packet per buffer dma */
  189. static bool use_dma_ppb = 1;
  190. /* with per descr. update */
  191. static bool use_dma_ppb_du;
  192. /* buffer fill mode */
  193. static int use_dma_bufferfill_mode;
  194. /* full speed only mode */
  195. static bool use_fullspeed;
  196. /* tx buffer size for high speed */
  197. static unsigned long hs_tx_buf = UDC_EPIN_BUFF_SIZE;
  198. /* module parameters */
  199. module_param(use_dma, bool, S_IRUGO);
  200. MODULE_PARM_DESC(use_dma, "true for DMA");
  201. module_param(use_dma_ppb, bool, S_IRUGO);
  202. MODULE_PARM_DESC(use_dma_ppb, "true for DMA in packet per buffer mode");
  203. module_param(use_dma_ppb_du, bool, S_IRUGO);
  204. MODULE_PARM_DESC(use_dma_ppb_du,
  205. "true for DMA in packet per buffer mode with descriptor update");
  206. module_param(use_fullspeed, bool, S_IRUGO);
  207. MODULE_PARM_DESC(use_fullspeed, "true for fullspeed only");
  208. /*---------------------------------------------------------------------------*/
  209. /* Prints UDC device registers and endpoint irq registers */
  210. static void print_regs(struct udc *dev)
  211. {
  212. DBG(dev, "------- Device registers -------\n");
  213. DBG(dev, "dev config = %08x\n", readl(&dev->regs->cfg));
  214. DBG(dev, "dev control = %08x\n", readl(&dev->regs->ctl));
  215. DBG(dev, "dev status = %08x\n", readl(&dev->regs->sts));
  216. DBG(dev, "\n");
  217. DBG(dev, "dev int's = %08x\n", readl(&dev->regs->irqsts));
  218. DBG(dev, "dev intmask = %08x\n", readl(&dev->regs->irqmsk));
  219. DBG(dev, "\n");
  220. DBG(dev, "dev ep int's = %08x\n", readl(&dev->regs->ep_irqsts));
  221. DBG(dev, "dev ep intmask = %08x\n", readl(&dev->regs->ep_irqmsk));
  222. DBG(dev, "\n");
  223. DBG(dev, "USE DMA = %d\n", use_dma);
  224. if (use_dma && use_dma_ppb && !use_dma_ppb_du) {
  225. DBG(dev, "DMA mode = PPBNDU (packet per buffer "
  226. "WITHOUT desc. update)\n");
  227. dev_info(&dev->pdev->dev, "DMA mode (%s)\n", "PPBNDU");
  228. } else if (use_dma && use_dma_ppb && use_dma_ppb_du) {
  229. DBG(dev, "DMA mode = PPBDU (packet per buffer "
  230. "WITH desc. update)\n");
  231. dev_info(&dev->pdev->dev, "DMA mode (%s)\n", "PPBDU");
  232. }
  233. if (use_dma && use_dma_bufferfill_mode) {
  234. DBG(dev, "DMA mode = BF (buffer fill mode)\n");
  235. dev_info(&dev->pdev->dev, "DMA mode (%s)\n", "BF");
  236. }
  237. if (!use_dma)
  238. dev_info(&dev->pdev->dev, "FIFO mode\n");
  239. DBG(dev, "-------------------------------------------------------\n");
  240. }
  241. /* Masks unused interrupts */
  242. static int udc_mask_unused_interrupts(struct udc *dev)
  243. {
  244. u32 tmp;
  245. /* mask all dev interrupts */
  246. tmp = AMD_BIT(UDC_DEVINT_SVC) |
  247. AMD_BIT(UDC_DEVINT_ENUM) |
  248. AMD_BIT(UDC_DEVINT_US) |
  249. AMD_BIT(UDC_DEVINT_UR) |
  250. AMD_BIT(UDC_DEVINT_ES) |
  251. AMD_BIT(UDC_DEVINT_SI) |
  252. AMD_BIT(UDC_DEVINT_SOF)|
  253. AMD_BIT(UDC_DEVINT_SC);
  254. writel(tmp, &dev->regs->irqmsk);
  255. /* mask all ep interrupts */
  256. writel(UDC_EPINT_MSK_DISABLE_ALL, &dev->regs->ep_irqmsk);
  257. return 0;
  258. }
  259. /* Enables endpoint 0 interrupts */
  260. static int udc_enable_ep0_interrupts(struct udc *dev)
  261. {
  262. u32 tmp;
  263. DBG(dev, "udc_enable_ep0_interrupts()\n");
  264. /* read irq mask */
  265. tmp = readl(&dev->regs->ep_irqmsk);
  266. /* enable ep0 irq's */
  267. tmp &= AMD_UNMASK_BIT(UDC_EPINT_IN_EP0)
  268. & AMD_UNMASK_BIT(UDC_EPINT_OUT_EP0);
  269. writel(tmp, &dev->regs->ep_irqmsk);
  270. return 0;
  271. }
  272. /* Enables device interrupts for SET_INTF and SET_CONFIG */
  273. static int udc_enable_dev_setup_interrupts(struct udc *dev)
  274. {
  275. u32 tmp;
  276. DBG(dev, "enable device interrupts for setup data\n");
  277. /* read irq mask */
  278. tmp = readl(&dev->regs->irqmsk);
  279. /* enable SET_INTERFACE, SET_CONFIG and other needed irq's */
  280. tmp &= AMD_UNMASK_BIT(UDC_DEVINT_SI)
  281. & AMD_UNMASK_BIT(UDC_DEVINT_SC)
  282. & AMD_UNMASK_BIT(UDC_DEVINT_UR)
  283. & AMD_UNMASK_BIT(UDC_DEVINT_SVC)
  284. & AMD_UNMASK_BIT(UDC_DEVINT_ENUM);
  285. writel(tmp, &dev->regs->irqmsk);
  286. return 0;
  287. }
  288. /* Calculates fifo start of endpoint based on preceding endpoints */
  289. static int udc_set_txfifo_addr(struct udc_ep *ep)
  290. {
  291. struct udc *dev;
  292. u32 tmp;
  293. int i;
  294. if (!ep || !(ep->in))
  295. return -EINVAL;
  296. dev = ep->dev;
  297. ep->txfifo = dev->txfifo;
  298. /* traverse ep's */
  299. for (i = 0; i < ep->num; i++) {
  300. if (dev->ep[i].regs) {
  301. /* read fifo size */
  302. tmp = readl(&dev->ep[i].regs->bufin_framenum);
  303. tmp = AMD_GETBITS(tmp, UDC_EPIN_BUFF_SIZE);
  304. ep->txfifo += tmp;
  305. }
  306. }
  307. return 0;
  308. }
  309. /* CNAK pending field: bit0 = ep0in, bit16 = ep0out */
  310. static u32 cnak_pending;
  311. static void UDC_QUEUE_CNAK(struct udc_ep *ep, unsigned num)
  312. {
  313. if (readl(&ep->regs->ctl) & AMD_BIT(UDC_EPCTL_NAK)) {
  314. DBG(ep->dev, "NAK could not be cleared for ep%d\n", num);
  315. cnak_pending |= 1 << (num);
  316. ep->naking = 1;
  317. } else
  318. cnak_pending = cnak_pending & (~(1 << (num)));
  319. }
  320. /* Enables endpoint, is called by gadget driver */
  321. static int
  322. udc_ep_enable(struct usb_ep *usbep, const struct usb_endpoint_descriptor *desc)
  323. {
  324. struct udc_ep *ep;
  325. struct udc *dev;
  326. u32 tmp;
  327. unsigned long iflags;
  328. u8 udc_csr_epix;
  329. unsigned maxpacket;
  330. if (!usbep
  331. || usbep->name == ep0_string
  332. || !desc
  333. || desc->bDescriptorType != USB_DT_ENDPOINT)
  334. return -EINVAL;
  335. ep = container_of(usbep, struct udc_ep, ep);
  336. dev = ep->dev;
  337. DBG(dev, "udc_ep_enable() ep %d\n", ep->num);
  338. if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
  339. return -ESHUTDOWN;
  340. spin_lock_irqsave(&dev->lock, iflags);
  341. ep->ep.desc = desc;
  342. ep->halted = 0;
  343. /* set traffic type */
  344. tmp = readl(&dev->ep[ep->num].regs->ctl);
  345. tmp = AMD_ADDBITS(tmp, desc->bmAttributes, UDC_EPCTL_ET);
  346. writel(tmp, &dev->ep[ep->num].regs->ctl);
  347. /* set max packet size */
  348. maxpacket = usb_endpoint_maxp(desc);
  349. tmp = readl(&dev->ep[ep->num].regs->bufout_maxpkt);
  350. tmp = AMD_ADDBITS(tmp, maxpacket, UDC_EP_MAX_PKT_SIZE);
  351. ep->ep.maxpacket = maxpacket;
  352. writel(tmp, &dev->ep[ep->num].regs->bufout_maxpkt);
  353. /* IN ep */
  354. if (ep->in) {
  355. /* ep ix in UDC CSR register space */
  356. udc_csr_epix = ep->num;
  357. /* set buffer size (tx fifo entries) */
  358. tmp = readl(&dev->ep[ep->num].regs->bufin_framenum);
  359. /* double buffering: fifo size = 2 x max packet size */
  360. tmp = AMD_ADDBITS(
  361. tmp,
  362. maxpacket * UDC_EPIN_BUFF_SIZE_MULT
  363. / UDC_DWORD_BYTES,
  364. UDC_EPIN_BUFF_SIZE);
  365. writel(tmp, &dev->ep[ep->num].regs->bufin_framenum);
  366. /* calc. tx fifo base addr */
  367. udc_set_txfifo_addr(ep);
  368. /* flush fifo */
  369. tmp = readl(&ep->regs->ctl);
  370. tmp |= AMD_BIT(UDC_EPCTL_F);
  371. writel(tmp, &ep->regs->ctl);
  372. /* OUT ep */
  373. } else {
  374. /* ep ix in UDC CSR register space */
  375. udc_csr_epix = ep->num - UDC_CSR_EP_OUT_IX_OFS;
  376. /* set max packet size UDC CSR */
  377. tmp = readl(&dev->csr->ne[ep->num - UDC_CSR_EP_OUT_IX_OFS]);
  378. tmp = AMD_ADDBITS(tmp, maxpacket,
  379. UDC_CSR_NE_MAX_PKT);
  380. writel(tmp, &dev->csr->ne[ep->num - UDC_CSR_EP_OUT_IX_OFS]);
  381. if (use_dma && !ep->in) {
  382. /* alloc and init BNA dummy request */
  383. ep->bna_dummy_req = udc_alloc_bna_dummy(ep);
  384. ep->bna_occurred = 0;
  385. }
  386. if (ep->num != UDC_EP0OUT_IX)
  387. dev->data_ep_enabled = 1;
  388. }
  389. /* set ep values */
  390. tmp = readl(&dev->csr->ne[udc_csr_epix]);
  391. /* max packet */
  392. tmp = AMD_ADDBITS(tmp, maxpacket, UDC_CSR_NE_MAX_PKT);
  393. /* ep number */
  394. tmp = AMD_ADDBITS(tmp, desc->bEndpointAddress, UDC_CSR_NE_NUM);
  395. /* ep direction */
  396. tmp = AMD_ADDBITS(tmp, ep->in, UDC_CSR_NE_DIR);
  397. /* ep type */
  398. tmp = AMD_ADDBITS(tmp, desc->bmAttributes, UDC_CSR_NE_TYPE);
  399. /* ep config */
  400. tmp = AMD_ADDBITS(tmp, ep->dev->cur_config, UDC_CSR_NE_CFG);
  401. /* ep interface */
  402. tmp = AMD_ADDBITS(tmp, ep->dev->cur_intf, UDC_CSR_NE_INTF);
  403. /* ep alt */
  404. tmp = AMD_ADDBITS(tmp, ep->dev->cur_alt, UDC_CSR_NE_ALT);
  405. /* write reg */
  406. writel(tmp, &dev->csr->ne[udc_csr_epix]);
  407. /* enable ep irq */
  408. tmp = readl(&dev->regs->ep_irqmsk);
  409. tmp &= AMD_UNMASK_BIT(ep->num);
  410. writel(tmp, &dev->regs->ep_irqmsk);
  411. /*
  412. * clear NAK by writing CNAK
  413. * avoid BNA for OUT DMA, don't clear NAK until DMA desc. written
  414. */
  415. if (!use_dma || ep->in) {
  416. tmp = readl(&ep->regs->ctl);
  417. tmp |= AMD_BIT(UDC_EPCTL_CNAK);
  418. writel(tmp, &ep->regs->ctl);
  419. ep->naking = 0;
  420. UDC_QUEUE_CNAK(ep, ep->num);
  421. }
  422. tmp = desc->bEndpointAddress;
  423. DBG(dev, "%s enabled\n", usbep->name);
  424. spin_unlock_irqrestore(&dev->lock, iflags);
  425. return 0;
  426. }
  427. /* Resets endpoint */
  428. static void ep_init(struct udc_regs __iomem *regs, struct udc_ep *ep)
  429. {
  430. u32 tmp;
  431. VDBG(ep->dev, "ep-%d reset\n", ep->num);
  432. ep->ep.desc = NULL;
  433. ep->ep.ops = &udc_ep_ops;
  434. INIT_LIST_HEAD(&ep->queue);
  435. usb_ep_set_maxpacket_limit(&ep->ep,(u16) ~0);
  436. /* set NAK */
  437. tmp = readl(&ep->regs->ctl);
  438. tmp |= AMD_BIT(UDC_EPCTL_SNAK);
  439. writel(tmp, &ep->regs->ctl);
  440. ep->naking = 1;
  441. /* disable interrupt */
  442. tmp = readl(&regs->ep_irqmsk);
  443. tmp |= AMD_BIT(ep->num);
  444. writel(tmp, &regs->ep_irqmsk);
  445. if (ep->in) {
  446. /* unset P and IN bit of potential former DMA */
  447. tmp = readl(&ep->regs->ctl);
  448. tmp &= AMD_UNMASK_BIT(UDC_EPCTL_P);
  449. writel(tmp, &ep->regs->ctl);
  450. tmp = readl(&ep->regs->sts);
  451. tmp |= AMD_BIT(UDC_EPSTS_IN);
  452. writel(tmp, &ep->regs->sts);
  453. /* flush the fifo */
  454. tmp = readl(&ep->regs->ctl);
  455. tmp |= AMD_BIT(UDC_EPCTL_F);
  456. writel(tmp, &ep->regs->ctl);
  457. }
  458. /* reset desc pointer */
  459. writel(0, &ep->regs->desptr);
  460. }
  461. /* Disables endpoint, is called by gadget driver */
  462. static int udc_ep_disable(struct usb_ep *usbep)
  463. {
  464. struct udc_ep *ep = NULL;
  465. unsigned long iflags;
  466. if (!usbep)
  467. return -EINVAL;
  468. ep = container_of(usbep, struct udc_ep, ep);
  469. if (usbep->name == ep0_string || !ep->ep.desc)
  470. return -EINVAL;
  471. DBG(ep->dev, "Disable ep-%d\n", ep->num);
  472. spin_lock_irqsave(&ep->dev->lock, iflags);
  473. udc_free_request(&ep->ep, &ep->bna_dummy_req->req);
  474. empty_req_queue(ep);
  475. ep_init(ep->dev->regs, ep);
  476. spin_unlock_irqrestore(&ep->dev->lock, iflags);
  477. return 0;
  478. }
  479. /* Allocates request packet, called by gadget driver */
  480. static struct usb_request *
  481. udc_alloc_request(struct usb_ep *usbep, gfp_t gfp)
  482. {
  483. struct udc_request *req;
  484. struct udc_data_dma *dma_desc;
  485. struct udc_ep *ep;
  486. if (!usbep)
  487. return NULL;
  488. ep = container_of(usbep, struct udc_ep, ep);
  489. VDBG(ep->dev, "udc_alloc_req(): ep%d\n", ep->num);
  490. req = kzalloc(sizeof(struct udc_request), gfp);
  491. if (!req)
  492. return NULL;
  493. req->req.dma = DMA_DONT_USE;
  494. INIT_LIST_HEAD(&req->queue);
  495. if (ep->dma) {
  496. /* ep0 in requests are allocated from data pool here */
  497. dma_desc = pci_pool_alloc(ep->dev->data_requests, gfp,
  498. &req->td_phys);
  499. if (!dma_desc) {
  500. kfree(req);
  501. return NULL;
  502. }
  503. VDBG(ep->dev, "udc_alloc_req: req = %p dma_desc = %p, "
  504. "td_phys = %lx\n",
  505. req, dma_desc,
  506. (unsigned long)req->td_phys);
  507. /* prevent from using desc. - set HOST BUSY */
  508. dma_desc->status = AMD_ADDBITS(dma_desc->status,
  509. UDC_DMA_STP_STS_BS_HOST_BUSY,
  510. UDC_DMA_STP_STS_BS);
  511. dma_desc->bufptr = cpu_to_le32(DMA_DONT_USE);
  512. req->td_data = dma_desc;
  513. req->td_data_last = NULL;
  514. req->chain_len = 1;
  515. }
  516. return &req->req;
  517. }
  518. /* frees pci pool descriptors of a DMA chain */
  519. static int udc_free_dma_chain(struct udc *dev, struct udc_request *req)
  520. {
  521. int ret_val = 0;
  522. struct udc_data_dma *td;
  523. struct udc_data_dma *td_last = NULL;
  524. unsigned int i;
  525. DBG(dev, "free chain req = %p\n", req);
  526. /* do not free first desc., will be done by free for request */
  527. td_last = req->td_data;
  528. td = phys_to_virt(td_last->next);
  529. for (i = 1; i < req->chain_len; i++) {
  530. pci_pool_free(dev->data_requests, td,
  531. (dma_addr_t)td_last->next);
  532. td_last = td;
  533. td = phys_to_virt(td_last->next);
  534. }
  535. return ret_val;
  536. }
  537. /* Frees request packet, called by gadget driver */
  538. static void
  539. udc_free_request(struct usb_ep *usbep, struct usb_request *usbreq)
  540. {
  541. struct udc_ep *ep;
  542. struct udc_request *req;
  543. if (!usbep || !usbreq)
  544. return;
  545. ep = container_of(usbep, struct udc_ep, ep);
  546. req = container_of(usbreq, struct udc_request, req);
  547. VDBG(ep->dev, "free_req req=%p\n", req);
  548. BUG_ON(!list_empty(&req->queue));
  549. if (req->td_data) {
  550. VDBG(ep->dev, "req->td_data=%p\n", req->td_data);
  551. /* free dma chain if created */
  552. if (req->chain_len > 1)
  553. udc_free_dma_chain(ep->dev, req);
  554. pci_pool_free(ep->dev->data_requests, req->td_data,
  555. req->td_phys);
  556. }
  557. kfree(req);
  558. }
  559. /* Init BNA dummy descriptor for HOST BUSY and pointing to itself */
  560. static void udc_init_bna_dummy(struct udc_request *req)
  561. {
  562. if (req) {
  563. /* set last bit */
  564. req->td_data->status |= AMD_BIT(UDC_DMA_IN_STS_L);
  565. /* set next pointer to itself */
  566. req->td_data->next = req->td_phys;
  567. /* set HOST BUSY */
  568. req->td_data->status
  569. = AMD_ADDBITS(req->td_data->status,
  570. UDC_DMA_STP_STS_BS_DMA_DONE,
  571. UDC_DMA_STP_STS_BS);
  572. #ifdef UDC_VERBOSE
  573. pr_debug("bna desc = %p, sts = %08x\n",
  574. req->td_data, req->td_data->status);
  575. #endif
  576. }
  577. }
  578. /* Allocate BNA dummy descriptor */
  579. static struct udc_request *udc_alloc_bna_dummy(struct udc_ep *ep)
  580. {
  581. struct udc_request *req = NULL;
  582. struct usb_request *_req = NULL;
  583. /* alloc the dummy request */
  584. _req = udc_alloc_request(&ep->ep, GFP_ATOMIC);
  585. if (_req) {
  586. req = container_of(_req, struct udc_request, req);
  587. ep->bna_dummy_req = req;
  588. udc_init_bna_dummy(req);
  589. }
  590. return req;
  591. }
  592. /* Write data to TX fifo for IN packets */
  593. static void
  594. udc_txfifo_write(struct udc_ep *ep, struct usb_request *req)
  595. {
  596. u8 *req_buf;
  597. u32 *buf;
  598. int i, j;
  599. unsigned bytes = 0;
  600. unsigned remaining = 0;
  601. if (!req || !ep)
  602. return;
  603. req_buf = req->buf + req->actual;
  604. prefetch(req_buf);
  605. remaining = req->length - req->actual;
  606. buf = (u32 *) req_buf;
  607. bytes = ep->ep.maxpacket;
  608. if (bytes > remaining)
  609. bytes = remaining;
  610. /* dwords first */
  611. for (i = 0; i < bytes / UDC_DWORD_BYTES; i++)
  612. writel(*(buf + i), ep->txfifo);
  613. /* remaining bytes must be written by byte access */
  614. for (j = 0; j < bytes % UDC_DWORD_BYTES; j++) {
  615. writeb((u8)(*(buf + i) >> (j << UDC_BITS_PER_BYTE_SHIFT)),
  616. ep->txfifo);
  617. }
  618. /* dummy write confirm */
  619. writel(0, &ep->regs->confirm);
  620. }
  621. /* Read dwords from RX fifo for OUT transfers */
  622. static int udc_rxfifo_read_dwords(struct udc *dev, u32 *buf, int dwords)
  623. {
  624. int i;
  625. VDBG(dev, "udc_read_dwords(): %d dwords\n", dwords);
  626. for (i = 0; i < dwords; i++)
  627. *(buf + i) = readl(dev->rxfifo);
  628. return 0;
  629. }
  630. /* Read bytes from RX fifo for OUT transfers */
  631. static int udc_rxfifo_read_bytes(struct udc *dev, u8 *buf, int bytes)
  632. {
  633. int i, j;
  634. u32 tmp;
  635. VDBG(dev, "udc_read_bytes(): %d bytes\n", bytes);
  636. /* dwords first */
  637. for (i = 0; i < bytes / UDC_DWORD_BYTES; i++)
  638. *((u32 *)(buf + (i<<2))) = readl(dev->rxfifo);
  639. /* remaining bytes must be read by byte access */
  640. if (bytes % UDC_DWORD_BYTES) {
  641. tmp = readl(dev->rxfifo);
  642. for (j = 0; j < bytes % UDC_DWORD_BYTES; j++) {
  643. *(buf + (i<<2) + j) = (u8)(tmp & UDC_BYTE_MASK);
  644. tmp = tmp >> UDC_BITS_PER_BYTE;
  645. }
  646. }
  647. return 0;
  648. }
  649. /* Read data from RX fifo for OUT transfers */
  650. static int
  651. udc_rxfifo_read(struct udc_ep *ep, struct udc_request *req)
  652. {
  653. u8 *buf;
  654. unsigned buf_space;
  655. unsigned bytes = 0;
  656. unsigned finished = 0;
  657. /* received number bytes */
  658. bytes = readl(&ep->regs->sts);
  659. bytes = AMD_GETBITS(bytes, UDC_EPSTS_RX_PKT_SIZE);
  660. buf_space = req->req.length - req->req.actual;
  661. buf = req->req.buf + req->req.actual;
  662. if (bytes > buf_space) {
  663. if ((buf_space % ep->ep.maxpacket) != 0) {
  664. DBG(ep->dev,
  665. "%s: rx %d bytes, rx-buf space = %d bytesn\n",
  666. ep->ep.name, bytes, buf_space);
  667. req->req.status = -EOVERFLOW;
  668. }
  669. bytes = buf_space;
  670. }
  671. req->req.actual += bytes;
  672. /* last packet ? */
  673. if (((bytes % ep->ep.maxpacket) != 0) || (!bytes)
  674. || ((req->req.actual == req->req.length) && !req->req.zero))
  675. finished = 1;
  676. /* read rx fifo bytes */
  677. VDBG(ep->dev, "ep %s: rxfifo read %d bytes\n", ep->ep.name, bytes);
  678. udc_rxfifo_read_bytes(ep->dev, buf, bytes);
  679. return finished;
  680. }
  681. /* Creates or re-inits a DMA chain */
  682. static int udc_create_dma_chain(
  683. struct udc_ep *ep,
  684. struct udc_request *req,
  685. unsigned long buf_len, gfp_t gfp_flags
  686. )
  687. {
  688. unsigned long bytes = req->req.length;
  689. unsigned int i;
  690. dma_addr_t dma_addr;
  691. struct udc_data_dma *td = NULL;
  692. struct udc_data_dma *last = NULL;
  693. unsigned long txbytes;
  694. unsigned create_new_chain = 0;
  695. unsigned len;
  696. VDBG(ep->dev, "udc_create_dma_chain: bytes=%ld buf_len=%ld\n",
  697. bytes, buf_len);
  698. dma_addr = DMA_DONT_USE;
  699. /* unset L bit in first desc for OUT */
  700. if (!ep->in)
  701. req->td_data->status &= AMD_CLEAR_BIT(UDC_DMA_IN_STS_L);
  702. /* alloc only new desc's if not already available */
  703. len = req->req.length / ep->ep.maxpacket;
  704. if (req->req.length % ep->ep.maxpacket)
  705. len++;
  706. if (len > req->chain_len) {
  707. /* shorter chain already allocated before */
  708. if (req->chain_len > 1)
  709. udc_free_dma_chain(ep->dev, req);
  710. req->chain_len = len;
  711. create_new_chain = 1;
  712. }
  713. td = req->td_data;
  714. /* gen. required number of descriptors and buffers */
  715. for (i = buf_len; i < bytes; i += buf_len) {
  716. /* create or determine next desc. */
  717. if (create_new_chain) {
  718. td = pci_pool_alloc(ep->dev->data_requests,
  719. gfp_flags, &dma_addr);
  720. if (!td)
  721. return -ENOMEM;
  722. td->status = 0;
  723. } else if (i == buf_len) {
  724. /* first td */
  725. td = (struct udc_data_dma *)phys_to_virt(
  726. req->td_data->next);
  727. td->status = 0;
  728. } else {
  729. td = (struct udc_data_dma *)phys_to_virt(last->next);
  730. td->status = 0;
  731. }
  732. if (td)
  733. td->bufptr = req->req.dma + i; /* assign buffer */
  734. else
  735. break;
  736. /* short packet ? */
  737. if ((bytes - i) >= buf_len) {
  738. txbytes = buf_len;
  739. } else {
  740. /* short packet */
  741. txbytes = bytes - i;
  742. }
  743. /* link td and assign tx bytes */
  744. if (i == buf_len) {
  745. if (create_new_chain)
  746. req->td_data->next = dma_addr;
  747. /*
  748. * else
  749. * req->td_data->next = virt_to_phys(td);
  750. */
  751. /* write tx bytes */
  752. if (ep->in) {
  753. /* first desc */
  754. req->td_data->status =
  755. AMD_ADDBITS(req->td_data->status,
  756. ep->ep.maxpacket,
  757. UDC_DMA_IN_STS_TXBYTES);
  758. /* second desc */
  759. td->status = AMD_ADDBITS(td->status,
  760. txbytes,
  761. UDC_DMA_IN_STS_TXBYTES);
  762. }
  763. } else {
  764. if (create_new_chain)
  765. last->next = dma_addr;
  766. /*
  767. * else
  768. * last->next = virt_to_phys(td);
  769. */
  770. if (ep->in) {
  771. /* write tx bytes */
  772. td->status = AMD_ADDBITS(td->status,
  773. txbytes,
  774. UDC_DMA_IN_STS_TXBYTES);
  775. }
  776. }
  777. last = td;
  778. }
  779. /* set last bit */
  780. if (td) {
  781. td->status |= AMD_BIT(UDC_DMA_IN_STS_L);
  782. /* last desc. points to itself */
  783. req->td_data_last = td;
  784. }
  785. return 0;
  786. }
  787. /* create/re-init a DMA descriptor or a DMA descriptor chain */
  788. static int prep_dma(struct udc_ep *ep, struct udc_request *req, gfp_t gfp)
  789. {
  790. int retval = 0;
  791. u32 tmp;
  792. VDBG(ep->dev, "prep_dma\n");
  793. VDBG(ep->dev, "prep_dma ep%d req->td_data=%p\n",
  794. ep->num, req->td_data);
  795. /* set buffer pointer */
  796. req->td_data->bufptr = req->req.dma;
  797. /* set last bit */
  798. req->td_data->status |= AMD_BIT(UDC_DMA_IN_STS_L);
  799. /* build/re-init dma chain if maxpkt scatter mode, not for EP0 */
  800. if (use_dma_ppb) {
  801. retval = udc_create_dma_chain(ep, req, ep->ep.maxpacket, gfp);
  802. if (retval != 0) {
  803. if (retval == -ENOMEM)
  804. DBG(ep->dev, "Out of DMA memory\n");
  805. return retval;
  806. }
  807. if (ep->in) {
  808. if (req->req.length == ep->ep.maxpacket) {
  809. /* write tx bytes */
  810. req->td_data->status =
  811. AMD_ADDBITS(req->td_data->status,
  812. ep->ep.maxpacket,
  813. UDC_DMA_IN_STS_TXBYTES);
  814. }
  815. }
  816. }
  817. if (ep->in) {
  818. VDBG(ep->dev, "IN: use_dma_ppb=%d req->req.len=%d "
  819. "maxpacket=%d ep%d\n",
  820. use_dma_ppb, req->req.length,
  821. ep->ep.maxpacket, ep->num);
  822. /*
  823. * if bytes < max packet then tx bytes must
  824. * be written in packet per buffer mode
  825. */
  826. if (!use_dma_ppb || req->req.length < ep->ep.maxpacket
  827. || ep->num == UDC_EP0OUT_IX
  828. || ep->num == UDC_EP0IN_IX) {
  829. /* write tx bytes */
  830. req->td_data->status =
  831. AMD_ADDBITS(req->td_data->status,
  832. req->req.length,
  833. UDC_DMA_IN_STS_TXBYTES);
  834. /* reset frame num */
  835. req->td_data->status =
  836. AMD_ADDBITS(req->td_data->status,
  837. 0,
  838. UDC_DMA_IN_STS_FRAMENUM);
  839. }
  840. /* set HOST BUSY */
  841. req->td_data->status =
  842. AMD_ADDBITS(req->td_data->status,
  843. UDC_DMA_STP_STS_BS_HOST_BUSY,
  844. UDC_DMA_STP_STS_BS);
  845. } else {
  846. VDBG(ep->dev, "OUT set host ready\n");
  847. /* set HOST READY */
  848. req->td_data->status =
  849. AMD_ADDBITS(req->td_data->status,
  850. UDC_DMA_STP_STS_BS_HOST_READY,
  851. UDC_DMA_STP_STS_BS);
  852. /* clear NAK by writing CNAK */
  853. if (ep->naking) {
  854. tmp = readl(&ep->regs->ctl);
  855. tmp |= AMD_BIT(UDC_EPCTL_CNAK);
  856. writel(tmp, &ep->regs->ctl);
  857. ep->naking = 0;
  858. UDC_QUEUE_CNAK(ep, ep->num);
  859. }
  860. }
  861. return retval;
  862. }
  863. /* Completes request packet ... caller MUST hold lock */
  864. static void
  865. complete_req(struct udc_ep *ep, struct udc_request *req, int sts)
  866. __releases(ep->dev->lock)
  867. __acquires(ep->dev->lock)
  868. {
  869. struct udc *dev;
  870. unsigned halted;
  871. VDBG(ep->dev, "complete_req(): ep%d\n", ep->num);
  872. dev = ep->dev;
  873. /* unmap DMA */
  874. if (ep->dma)
  875. usb_gadget_unmap_request(&dev->gadget, &req->req, ep->in);
  876. halted = ep->halted;
  877. ep->halted = 1;
  878. /* set new status if pending */
  879. if (req->req.status == -EINPROGRESS)
  880. req->req.status = sts;
  881. /* remove from ep queue */
  882. list_del_init(&req->queue);
  883. VDBG(ep->dev, "req %p => complete %d bytes at %s with sts %d\n",
  884. &req->req, req->req.length, ep->ep.name, sts);
  885. spin_unlock(&dev->lock);
  886. usb_gadget_giveback_request(&ep->ep, &req->req);
  887. spin_lock(&dev->lock);
  888. ep->halted = halted;
  889. }
  890. /* Iterates to the end of a DMA chain and returns last descriptor */
  891. static struct udc_data_dma *udc_get_last_dma_desc(struct udc_request *req)
  892. {
  893. struct udc_data_dma *td;
  894. td = req->td_data;
  895. while (td && !(td->status & AMD_BIT(UDC_DMA_IN_STS_L)))
  896. td = phys_to_virt(td->next);
  897. return td;
  898. }
  899. /* Iterates to the end of a DMA chain and counts bytes received */
  900. static u32 udc_get_ppbdu_rxbytes(struct udc_request *req)
  901. {
  902. struct udc_data_dma *td;
  903. u32 count;
  904. td = req->td_data;
  905. /* received number bytes */
  906. count = AMD_GETBITS(td->status, UDC_DMA_OUT_STS_RXBYTES);
  907. while (td && !(td->status & AMD_BIT(UDC_DMA_IN_STS_L))) {
  908. td = phys_to_virt(td->next);
  909. /* received number bytes */
  910. if (td) {
  911. count += AMD_GETBITS(td->status,
  912. UDC_DMA_OUT_STS_RXBYTES);
  913. }
  914. }
  915. return count;
  916. }
  917. /* Enabling RX DMA */
  918. static void udc_set_rde(struct udc *dev)
  919. {
  920. u32 tmp;
  921. VDBG(dev, "udc_set_rde()\n");
  922. /* stop RDE timer */
  923. if (timer_pending(&udc_timer)) {
  924. set_rde = 0;
  925. mod_timer(&udc_timer, jiffies - 1);
  926. }
  927. /* set RDE */
  928. tmp = readl(&dev->regs->ctl);
  929. tmp |= AMD_BIT(UDC_DEVCTL_RDE);
  930. writel(tmp, &dev->regs->ctl);
  931. }
  932. /* Queues a request packet, called by gadget driver */
  933. static int
  934. udc_queue(struct usb_ep *usbep, struct usb_request *usbreq, gfp_t gfp)
  935. {
  936. int retval = 0;
  937. u8 open_rxfifo = 0;
  938. unsigned long iflags;
  939. struct udc_ep *ep;
  940. struct udc_request *req;
  941. struct udc *dev;
  942. u32 tmp;
  943. /* check the inputs */
  944. req = container_of(usbreq, struct udc_request, req);
  945. if (!usbep || !usbreq || !usbreq->complete || !usbreq->buf
  946. || !list_empty(&req->queue))
  947. return -EINVAL;
  948. ep = container_of(usbep, struct udc_ep, ep);
  949. if (!ep->ep.desc && (ep->num != 0 && ep->num != UDC_EP0OUT_IX))
  950. return -EINVAL;
  951. VDBG(ep->dev, "udc_queue(): ep%d-in=%d\n", ep->num, ep->in);
  952. dev = ep->dev;
  953. if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
  954. return -ESHUTDOWN;
  955. /* map dma (usually done before) */
  956. if (ep->dma) {
  957. VDBG(dev, "DMA map req %p\n", req);
  958. retval = usb_gadget_map_request(&udc->gadget, usbreq, ep->in);
  959. if (retval)
  960. return retval;
  961. }
  962. VDBG(dev, "%s queue req %p, len %d req->td_data=%p buf %p\n",
  963. usbep->name, usbreq, usbreq->length,
  964. req->td_data, usbreq->buf);
  965. spin_lock_irqsave(&dev->lock, iflags);
  966. usbreq->actual = 0;
  967. usbreq->status = -EINPROGRESS;
  968. req->dma_done = 0;
  969. /* on empty queue just do first transfer */
  970. if (list_empty(&ep->queue)) {
  971. /* zlp */
  972. if (usbreq->length == 0) {
  973. /* IN zlp's are handled by hardware */
  974. complete_req(ep, req, 0);
  975. VDBG(dev, "%s: zlp\n", ep->ep.name);
  976. /*
  977. * if set_config or set_intf is waiting for ack by zlp
  978. * then set CSR_DONE
  979. */
  980. if (dev->set_cfg_not_acked) {
  981. tmp = readl(&dev->regs->ctl);
  982. tmp |= AMD_BIT(UDC_DEVCTL_CSR_DONE);
  983. writel(tmp, &dev->regs->ctl);
  984. dev->set_cfg_not_acked = 0;
  985. }
  986. /* setup command is ACK'ed now by zlp */
  987. if (dev->waiting_zlp_ack_ep0in) {
  988. /* clear NAK by writing CNAK in EP0_IN */
  989. tmp = readl(&dev->ep[UDC_EP0IN_IX].regs->ctl);
  990. tmp |= AMD_BIT(UDC_EPCTL_CNAK);
  991. writel(tmp, &dev->ep[UDC_EP0IN_IX].regs->ctl);
  992. dev->ep[UDC_EP0IN_IX].naking = 0;
  993. UDC_QUEUE_CNAK(&dev->ep[UDC_EP0IN_IX],
  994. UDC_EP0IN_IX);
  995. dev->waiting_zlp_ack_ep0in = 0;
  996. }
  997. goto finished;
  998. }
  999. if (ep->dma) {
  1000. retval = prep_dma(ep, req, GFP_ATOMIC);
  1001. if (retval != 0)
  1002. goto finished;
  1003. /* write desc pointer to enable DMA */
  1004. if (ep->in) {
  1005. /* set HOST READY */
  1006. req->td_data->status =
  1007. AMD_ADDBITS(req->td_data->status,
  1008. UDC_DMA_IN_STS_BS_HOST_READY,
  1009. UDC_DMA_IN_STS_BS);
  1010. }
  1011. /* disabled rx dma while descriptor update */
  1012. if (!ep->in) {
  1013. /* stop RDE timer */
  1014. if (timer_pending(&udc_timer)) {
  1015. set_rde = 0;
  1016. mod_timer(&udc_timer, jiffies - 1);
  1017. }
  1018. /* clear RDE */
  1019. tmp = readl(&dev->regs->ctl);
  1020. tmp &= AMD_UNMASK_BIT(UDC_DEVCTL_RDE);
  1021. writel(tmp, &dev->regs->ctl);
  1022. open_rxfifo = 1;
  1023. /*
  1024. * if BNA occurred then let BNA dummy desc.
  1025. * point to current desc.
  1026. */
  1027. if (ep->bna_occurred) {
  1028. VDBG(dev, "copy to BNA dummy desc.\n");
  1029. memcpy(ep->bna_dummy_req->td_data,
  1030. req->td_data,
  1031. sizeof(struct udc_data_dma));
  1032. }
  1033. }
  1034. /* write desc pointer */
  1035. writel(req->td_phys, &ep->regs->desptr);
  1036. /* clear NAK by writing CNAK */
  1037. if (ep->naking) {
  1038. tmp = readl(&ep->regs->ctl);
  1039. tmp |= AMD_BIT(UDC_EPCTL_CNAK);
  1040. writel(tmp, &ep->regs->ctl);
  1041. ep->naking = 0;
  1042. UDC_QUEUE_CNAK(ep, ep->num);
  1043. }
  1044. if (ep->in) {
  1045. /* enable ep irq */
  1046. tmp = readl(&dev->regs->ep_irqmsk);
  1047. tmp &= AMD_UNMASK_BIT(ep->num);
  1048. writel(tmp, &dev->regs->ep_irqmsk);
  1049. }
  1050. } else if (ep->in) {
  1051. /* enable ep irq */
  1052. tmp = readl(&dev->regs->ep_irqmsk);
  1053. tmp &= AMD_UNMASK_BIT(ep->num);
  1054. writel(tmp, &dev->regs->ep_irqmsk);
  1055. }
  1056. } else if (ep->dma) {
  1057. /*
  1058. * prep_dma not used for OUT ep's, this is not possible
  1059. * for PPB modes, because of chain creation reasons
  1060. */
  1061. if (ep->in) {
  1062. retval = prep_dma(ep, req, GFP_ATOMIC);
  1063. if (retval != 0)
  1064. goto finished;
  1065. }
  1066. }
  1067. VDBG(dev, "list_add\n");
  1068. /* add request to ep queue */
  1069. if (req) {
  1070. list_add_tail(&req->queue, &ep->queue);
  1071. /* open rxfifo if out data queued */
  1072. if (open_rxfifo) {
  1073. /* enable DMA */
  1074. req->dma_going = 1;
  1075. udc_set_rde(dev);
  1076. if (ep->num != UDC_EP0OUT_IX)
  1077. dev->data_ep_queued = 1;
  1078. }
  1079. /* stop OUT naking */
  1080. if (!ep->in) {
  1081. if (!use_dma && udc_rxfifo_pending) {
  1082. DBG(dev, "udc_queue(): pending bytes in "
  1083. "rxfifo after nyet\n");
  1084. /*
  1085. * read pending bytes afer nyet:
  1086. * referring to isr
  1087. */
  1088. if (udc_rxfifo_read(ep, req)) {
  1089. /* finish */
  1090. complete_req(ep, req, 0);
  1091. }
  1092. udc_rxfifo_pending = 0;
  1093. }
  1094. }
  1095. }
  1096. finished:
  1097. spin_unlock_irqrestore(&dev->lock, iflags);
  1098. return retval;
  1099. }
  1100. /* Empty request queue of an endpoint; caller holds spinlock */
  1101. static void empty_req_queue(struct udc_ep *ep)
  1102. {
  1103. struct udc_request *req;
  1104. ep->halted = 1;
  1105. while (!list_empty(&ep->queue)) {
  1106. req = list_entry(ep->queue.next,
  1107. struct udc_request,
  1108. queue);
  1109. complete_req(ep, req, -ESHUTDOWN);
  1110. }
  1111. }
  1112. /* Dequeues a request packet, called by gadget driver */
  1113. static int udc_dequeue(struct usb_ep *usbep, struct usb_request *usbreq)
  1114. {
  1115. struct udc_ep *ep;
  1116. struct udc_request *req;
  1117. unsigned halted;
  1118. unsigned long iflags;
  1119. ep = container_of(usbep, struct udc_ep, ep);
  1120. if (!usbep || !usbreq || (!ep->ep.desc && (ep->num != 0
  1121. && ep->num != UDC_EP0OUT_IX)))
  1122. return -EINVAL;
  1123. req = container_of(usbreq, struct udc_request, req);
  1124. spin_lock_irqsave(&ep->dev->lock, iflags);
  1125. halted = ep->halted;
  1126. ep->halted = 1;
  1127. /* request in processing or next one */
  1128. if (ep->queue.next == &req->queue) {
  1129. if (ep->dma && req->dma_going) {
  1130. if (ep->in)
  1131. ep->cancel_transfer = 1;
  1132. else {
  1133. u32 tmp;
  1134. u32 dma_sts;
  1135. /* stop potential receive DMA */
  1136. tmp = readl(&udc->regs->ctl);
  1137. writel(tmp & AMD_UNMASK_BIT(UDC_DEVCTL_RDE),
  1138. &udc->regs->ctl);
  1139. /*
  1140. * Cancel transfer later in ISR
  1141. * if descriptor was touched.
  1142. */
  1143. dma_sts = AMD_GETBITS(req->td_data->status,
  1144. UDC_DMA_OUT_STS_BS);
  1145. if (dma_sts != UDC_DMA_OUT_STS_BS_HOST_READY)
  1146. ep->cancel_transfer = 1;
  1147. else {
  1148. udc_init_bna_dummy(ep->req);
  1149. writel(ep->bna_dummy_req->td_phys,
  1150. &ep->regs->desptr);
  1151. }
  1152. writel(tmp, &udc->regs->ctl);
  1153. }
  1154. }
  1155. }
  1156. complete_req(ep, req, -ECONNRESET);
  1157. ep->halted = halted;
  1158. spin_unlock_irqrestore(&ep->dev->lock, iflags);
  1159. return 0;
  1160. }
  1161. /* Halt or clear halt of endpoint */
  1162. static int
  1163. udc_set_halt(struct usb_ep *usbep, int halt)
  1164. {
  1165. struct udc_ep *ep;
  1166. u32 tmp;
  1167. unsigned long iflags;
  1168. int retval = 0;
  1169. if (!usbep)
  1170. return -EINVAL;
  1171. pr_debug("set_halt %s: halt=%d\n", usbep->name, halt);
  1172. ep = container_of(usbep, struct udc_ep, ep);
  1173. if (!ep->ep.desc && (ep->num != 0 && ep->num != UDC_EP0OUT_IX))
  1174. return -EINVAL;
  1175. if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
  1176. return -ESHUTDOWN;
  1177. spin_lock_irqsave(&udc_stall_spinlock, iflags);
  1178. /* halt or clear halt */
  1179. if (halt) {
  1180. if (ep->num == 0)
  1181. ep->dev->stall_ep0in = 1;
  1182. else {
  1183. /*
  1184. * set STALL
  1185. * rxfifo empty not taken into acount
  1186. */
  1187. tmp = readl(&ep->regs->ctl);
  1188. tmp |= AMD_BIT(UDC_EPCTL_S);
  1189. writel(tmp, &ep->regs->ctl);
  1190. ep->halted = 1;
  1191. /* setup poll timer */
  1192. if (!timer_pending(&udc_pollstall_timer)) {
  1193. udc_pollstall_timer.expires = jiffies +
  1194. HZ * UDC_POLLSTALL_TIMER_USECONDS
  1195. / (1000 * 1000);
  1196. if (!stop_pollstall_timer) {
  1197. DBG(ep->dev, "start polltimer\n");
  1198. add_timer(&udc_pollstall_timer);
  1199. }
  1200. }
  1201. }
  1202. } else {
  1203. /* ep is halted by set_halt() before */
  1204. if (ep->halted) {
  1205. tmp = readl(&ep->regs->ctl);
  1206. /* clear stall bit */
  1207. tmp = tmp & AMD_CLEAR_BIT(UDC_EPCTL_S);
  1208. /* clear NAK by writing CNAK */
  1209. tmp |= AMD_BIT(UDC_EPCTL_CNAK);
  1210. writel(tmp, &ep->regs->ctl);
  1211. ep->halted = 0;
  1212. UDC_QUEUE_CNAK(ep, ep->num);
  1213. }
  1214. }
  1215. spin_unlock_irqrestore(&udc_stall_spinlock, iflags);
  1216. return retval;
  1217. }
  1218. /* gadget interface */
  1219. static const struct usb_ep_ops udc_ep_ops = {
  1220. .enable = udc_ep_enable,
  1221. .disable = udc_ep_disable,
  1222. .alloc_request = udc_alloc_request,
  1223. .free_request = udc_free_request,
  1224. .queue = udc_queue,
  1225. .dequeue = udc_dequeue,
  1226. .set_halt = udc_set_halt,
  1227. /* fifo ops not implemented */
  1228. };
  1229. /*-------------------------------------------------------------------------*/
  1230. /* Get frame counter (not implemented) */
  1231. static int udc_get_frame(struct usb_gadget *gadget)
  1232. {
  1233. return -EOPNOTSUPP;
  1234. }
  1235. /* Initiates a remote wakeup */
  1236. static int udc_remote_wakeup(struct udc *dev)
  1237. {
  1238. unsigned long flags;
  1239. u32 tmp;
  1240. DBG(dev, "UDC initiates remote wakeup\n");
  1241. spin_lock_irqsave(&dev->lock, flags);
  1242. tmp = readl(&dev->regs->ctl);
  1243. tmp |= AMD_BIT(UDC_DEVCTL_RES);
  1244. writel(tmp, &dev->regs->ctl);
  1245. tmp &= AMD_CLEAR_BIT(UDC_DEVCTL_RES);
  1246. writel(tmp, &dev->regs->ctl);
  1247. spin_unlock_irqrestore(&dev->lock, flags);
  1248. return 0;
  1249. }
  1250. /* Remote wakeup gadget interface */
  1251. static int udc_wakeup(struct usb_gadget *gadget)
  1252. {
  1253. struct udc *dev;
  1254. if (!gadget)
  1255. return -EINVAL;
  1256. dev = container_of(gadget, struct udc, gadget);
  1257. udc_remote_wakeup(dev);
  1258. return 0;
  1259. }
  1260. static int amd5536_udc_start(struct usb_gadget *g,
  1261. struct usb_gadget_driver *driver);
  1262. static int amd5536_udc_stop(struct usb_gadget *g);
  1263. static const struct usb_gadget_ops udc_ops = {
  1264. .wakeup = udc_wakeup,
  1265. .get_frame = udc_get_frame,
  1266. .udc_start = amd5536_udc_start,
  1267. .udc_stop = amd5536_udc_stop,
  1268. };
  1269. /* Setups endpoint parameters, adds endpoints to linked list */
  1270. static void make_ep_lists(struct udc *dev)
  1271. {
  1272. /* make gadget ep lists */
  1273. INIT_LIST_HEAD(&dev->gadget.ep_list);
  1274. list_add_tail(&dev->ep[UDC_EPIN_STATUS_IX].ep.ep_list,
  1275. &dev->gadget.ep_list);
  1276. list_add_tail(&dev->ep[UDC_EPIN_IX].ep.ep_list,
  1277. &dev->gadget.ep_list);
  1278. list_add_tail(&dev->ep[UDC_EPOUT_IX].ep.ep_list,
  1279. &dev->gadget.ep_list);
  1280. /* fifo config */
  1281. dev->ep[UDC_EPIN_STATUS_IX].fifo_depth = UDC_EPIN_SMALLINT_BUFF_SIZE;
  1282. if (dev->gadget.speed == USB_SPEED_FULL)
  1283. dev->ep[UDC_EPIN_IX].fifo_depth = UDC_FS_EPIN_BUFF_SIZE;
  1284. else if (dev->gadget.speed == USB_SPEED_HIGH)
  1285. dev->ep[UDC_EPIN_IX].fifo_depth = hs_tx_buf;
  1286. dev->ep[UDC_EPOUT_IX].fifo_depth = UDC_RXFIFO_SIZE;
  1287. }
  1288. /* Inits UDC context */
  1289. static void udc_basic_init(struct udc *dev)
  1290. {
  1291. u32 tmp;
  1292. DBG(dev, "udc_basic_init()\n");
  1293. dev->gadget.speed = USB_SPEED_UNKNOWN;
  1294. /* stop RDE timer */
  1295. if (timer_pending(&udc_timer)) {
  1296. set_rde = 0;
  1297. mod_timer(&udc_timer, jiffies - 1);
  1298. }
  1299. /* stop poll stall timer */
  1300. if (timer_pending(&udc_pollstall_timer))
  1301. mod_timer(&udc_pollstall_timer, jiffies - 1);
  1302. /* disable DMA */
  1303. tmp = readl(&dev->regs->ctl);
  1304. tmp &= AMD_UNMASK_BIT(UDC_DEVCTL_RDE);
  1305. tmp &= AMD_UNMASK_BIT(UDC_DEVCTL_TDE);
  1306. writel(tmp, &dev->regs->ctl);
  1307. /* enable dynamic CSR programming */
  1308. tmp = readl(&dev->regs->cfg);
  1309. tmp |= AMD_BIT(UDC_DEVCFG_CSR_PRG);
  1310. /* set self powered */
  1311. tmp |= AMD_BIT(UDC_DEVCFG_SP);
  1312. /* set remote wakeupable */
  1313. tmp |= AMD_BIT(UDC_DEVCFG_RWKP);
  1314. writel(tmp, &dev->regs->cfg);
  1315. make_ep_lists(dev);
  1316. dev->data_ep_enabled = 0;
  1317. dev->data_ep_queued = 0;
  1318. }
  1319. /* init registers at driver load time */
  1320. static int startup_registers(struct udc *dev)
  1321. {
  1322. u32 tmp;
  1323. /* init controller by soft reset */
  1324. udc_soft_reset(dev);
  1325. /* mask not needed interrupts */
  1326. udc_mask_unused_interrupts(dev);
  1327. /* put into initial config */
  1328. udc_basic_init(dev);
  1329. /* link up all endpoints */
  1330. udc_setup_endpoints(dev);
  1331. /* program speed */
  1332. tmp = readl(&dev->regs->cfg);
  1333. if (use_fullspeed)
  1334. tmp = AMD_ADDBITS(tmp, UDC_DEVCFG_SPD_FS, UDC_DEVCFG_SPD);
  1335. else
  1336. tmp = AMD_ADDBITS(tmp, UDC_DEVCFG_SPD_HS, UDC_DEVCFG_SPD);
  1337. writel(tmp, &dev->regs->cfg);
  1338. return 0;
  1339. }
  1340. /* Sets initial endpoint parameters */
  1341. static void udc_setup_endpoints(struct udc *dev)
  1342. {
  1343. struct udc_ep *ep;
  1344. u32 tmp;
  1345. u32 reg;
  1346. DBG(dev, "udc_setup_endpoints()\n");
  1347. /* read enum speed */
  1348. tmp = readl(&dev->regs->sts);
  1349. tmp = AMD_GETBITS(tmp, UDC_DEVSTS_ENUM_SPEED);
  1350. if (tmp == UDC_DEVSTS_ENUM_SPEED_HIGH)
  1351. dev->gadget.speed = USB_SPEED_HIGH;
  1352. else if (tmp == UDC_DEVSTS_ENUM_SPEED_FULL)
  1353. dev->gadget.speed = USB_SPEED_FULL;
  1354. /* set basic ep parameters */
  1355. for (tmp = 0; tmp < UDC_EP_NUM; tmp++) {
  1356. ep = &dev->ep[tmp];
  1357. ep->dev = dev;
  1358. ep->ep.name = ep_info[tmp].name;
  1359. ep->ep.caps = ep_info[tmp].caps;
  1360. ep->num = tmp;
  1361. /* txfifo size is calculated at enable time */
  1362. ep->txfifo = dev->txfifo;
  1363. /* fifo size */
  1364. if (tmp < UDC_EPIN_NUM) {
  1365. ep->fifo_depth = UDC_TXFIFO_SIZE;
  1366. ep->in = 1;
  1367. } else {
  1368. ep->fifo_depth = UDC_RXFIFO_SIZE;
  1369. ep->in = 0;
  1370. }
  1371. ep->regs = &dev->ep_regs[tmp];
  1372. /*
  1373. * ep will be reset only if ep was not enabled before to avoid
  1374. * disabling ep interrupts when ENUM interrupt occurs but ep is
  1375. * not enabled by gadget driver
  1376. */
  1377. if (!ep->ep.desc)
  1378. ep_init(dev->regs, ep);
  1379. if (use_dma) {
  1380. /*
  1381. * ep->dma is not really used, just to indicate that
  1382. * DMA is active: remove this
  1383. * dma regs = dev control regs
  1384. */
  1385. ep->dma = &dev->regs->ctl;
  1386. /* nak OUT endpoints until enable - not for ep0 */
  1387. if (tmp != UDC_EP0IN_IX && tmp != UDC_EP0OUT_IX
  1388. && tmp > UDC_EPIN_NUM) {
  1389. /* set NAK */
  1390. reg = readl(&dev->ep[tmp].regs->ctl);
  1391. reg |= AMD_BIT(UDC_EPCTL_SNAK);
  1392. writel(reg, &dev->ep[tmp].regs->ctl);
  1393. dev->ep[tmp].naking = 1;
  1394. }
  1395. }
  1396. }
  1397. /* EP0 max packet */
  1398. if (dev->gadget.speed == USB_SPEED_FULL) {
  1399. usb_ep_set_maxpacket_limit(&dev->ep[UDC_EP0IN_IX].ep,
  1400. UDC_FS_EP0IN_MAX_PKT_SIZE);
  1401. usb_ep_set_maxpacket_limit(&dev->ep[UDC_EP0OUT_IX].ep,
  1402. UDC_FS_EP0OUT_MAX_PKT_SIZE);
  1403. } else if (dev->gadget.speed == USB_SPEED_HIGH) {
  1404. usb_ep_set_maxpacket_limit(&dev->ep[UDC_EP0IN_IX].ep,
  1405. UDC_EP0IN_MAX_PKT_SIZE);
  1406. usb_ep_set_maxpacket_limit(&dev->ep[UDC_EP0OUT_IX].ep,
  1407. UDC_EP0OUT_MAX_PKT_SIZE);
  1408. }
  1409. /*
  1410. * with suspend bug workaround, ep0 params for gadget driver
  1411. * are set at gadget driver bind() call
  1412. */
  1413. dev->gadget.ep0 = &dev->ep[UDC_EP0IN_IX].ep;
  1414. dev->ep[UDC_EP0IN_IX].halted = 0;
  1415. INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
  1416. /* init cfg/alt/int */
  1417. dev->cur_config = 0;
  1418. dev->cur_intf = 0;
  1419. dev->cur_alt = 0;
  1420. }
  1421. /* Bringup after Connect event, initial bringup to be ready for ep0 events */
  1422. static void usb_connect(struct udc *dev)
  1423. {
  1424. dev_info(&dev->pdev->dev, "USB Connect\n");
  1425. dev->connected = 1;
  1426. /* put into initial config */
  1427. udc_basic_init(dev);
  1428. /* enable device setup interrupts */
  1429. udc_enable_dev_setup_interrupts(dev);
  1430. }
  1431. /*
  1432. * Calls gadget with disconnect event and resets the UDC and makes
  1433. * initial bringup to be ready for ep0 events
  1434. */
  1435. static void usb_disconnect(struct udc *dev)
  1436. {
  1437. dev_info(&dev->pdev->dev, "USB Disconnect\n");
  1438. dev->connected = 0;
  1439. /* mask interrupts */
  1440. udc_mask_unused_interrupts(dev);
  1441. /* REVISIT there doesn't seem to be a point to having this
  1442. * talk to a tasklet ... do it directly, we already hold
  1443. * the spinlock needed to process the disconnect.
  1444. */
  1445. tasklet_schedule(&disconnect_tasklet);
  1446. }
  1447. /* Tasklet for disconnect to be outside of interrupt context */
  1448. static void udc_tasklet_disconnect(unsigned long par)
  1449. {
  1450. struct udc *dev = (struct udc *)(*((struct udc **) par));
  1451. u32 tmp;
  1452. DBG(dev, "Tasklet disconnect\n");
  1453. spin_lock_irq(&dev->lock);
  1454. if (dev->driver) {
  1455. spin_unlock(&dev->lock);
  1456. dev->driver->disconnect(&dev->gadget);
  1457. spin_lock(&dev->lock);
  1458. /* empty queues */
  1459. for (tmp = 0; tmp < UDC_EP_NUM; tmp++)
  1460. empty_req_queue(&dev->ep[tmp]);
  1461. }
  1462. /* disable ep0 */
  1463. ep_init(dev->regs,
  1464. &dev->ep[UDC_EP0IN_IX]);
  1465. if (!soft_reset_occured) {
  1466. /* init controller by soft reset */
  1467. udc_soft_reset(dev);
  1468. soft_reset_occured++;
  1469. }
  1470. /* re-enable dev interrupts */
  1471. udc_enable_dev_setup_interrupts(dev);
  1472. /* back to full speed ? */
  1473. if (use_fullspeed) {
  1474. tmp = readl(&dev->regs->cfg);
  1475. tmp = AMD_ADDBITS(tmp, UDC_DEVCFG_SPD_FS, UDC_DEVCFG_SPD);
  1476. writel(tmp, &dev->regs->cfg);
  1477. }
  1478. spin_unlock_irq(&dev->lock);
  1479. }
  1480. /* Reset the UDC core */
  1481. static void udc_soft_reset(struct udc *dev)
  1482. {
  1483. unsigned long flags;
  1484. DBG(dev, "Soft reset\n");
  1485. /*
  1486. * reset possible waiting interrupts, because int.
  1487. * status is lost after soft reset,
  1488. * ep int. status reset
  1489. */
  1490. writel(UDC_EPINT_MSK_DISABLE_ALL, &dev->regs->ep_irqsts);
  1491. /* device int. status reset */
  1492. writel(UDC_DEV_MSK_DISABLE, &dev->regs->irqsts);
  1493. spin_lock_irqsave(&udc_irq_spinlock, flags);
  1494. writel(AMD_BIT(UDC_DEVCFG_SOFTRESET), &dev->regs->cfg);
  1495. readl(&dev->regs->cfg);
  1496. spin_unlock_irqrestore(&udc_irq_spinlock, flags);
  1497. }
  1498. /* RDE timer callback to set RDE bit */
  1499. static void udc_timer_function(unsigned long v)
  1500. {
  1501. u32 tmp;
  1502. spin_lock_irq(&udc_irq_spinlock);
  1503. if (set_rde > 0) {
  1504. /*
  1505. * open the fifo if fifo was filled on last timer call
  1506. * conditionally
  1507. */
  1508. if (set_rde > 1) {
  1509. /* set RDE to receive setup data */
  1510. tmp = readl(&udc->regs->ctl);
  1511. tmp |= AMD_BIT(UDC_DEVCTL_RDE);
  1512. writel(tmp, &udc->regs->ctl);
  1513. set_rde = -1;
  1514. } else if (readl(&udc->regs->sts)
  1515. & AMD_BIT(UDC_DEVSTS_RXFIFO_EMPTY)) {
  1516. /*
  1517. * if fifo empty setup polling, do not just
  1518. * open the fifo
  1519. */
  1520. udc_timer.expires = jiffies + HZ/UDC_RDE_TIMER_DIV;
  1521. if (!stop_timer)
  1522. add_timer(&udc_timer);
  1523. } else {
  1524. /*
  1525. * fifo contains data now, setup timer for opening
  1526. * the fifo when timer expires to be able to receive
  1527. * setup packets, when data packets gets queued by
  1528. * gadget layer then timer will forced to expire with
  1529. * set_rde=0 (RDE is set in udc_queue())
  1530. */
  1531. set_rde++;
  1532. /* debug: lhadmot_timer_start = 221070 */
  1533. udc_timer.expires = jiffies + HZ*UDC_RDE_TIMER_SECONDS;
  1534. if (!stop_timer)
  1535. add_timer(&udc_timer);
  1536. }
  1537. } else
  1538. set_rde = -1; /* RDE was set by udc_queue() */
  1539. spin_unlock_irq(&udc_irq_spinlock);
  1540. if (stop_timer)
  1541. complete(&on_exit);
  1542. }
  1543. /* Handle halt state, used in stall poll timer */
  1544. static void udc_handle_halt_state(struct udc_ep *ep)
  1545. {
  1546. u32 tmp;
  1547. /* set stall as long not halted */
  1548. if (ep->halted == 1) {
  1549. tmp = readl(&ep->regs->ctl);
  1550. /* STALL cleared ? */
  1551. if (!(tmp & AMD_BIT(UDC_EPCTL_S))) {
  1552. /*
  1553. * FIXME: MSC spec requires that stall remains
  1554. * even on receivng of CLEAR_FEATURE HALT. So
  1555. * we would set STALL again here to be compliant.
  1556. * But with current mass storage drivers this does
  1557. * not work (would produce endless host retries).
  1558. * So we clear halt on CLEAR_FEATURE.
  1559. *
  1560. DBG(ep->dev, "ep %d: set STALL again\n", ep->num);
  1561. tmp |= AMD_BIT(UDC_EPCTL_S);
  1562. writel(tmp, &ep->regs->ctl);*/
  1563. /* clear NAK by writing CNAK */
  1564. tmp |= AMD_BIT(UDC_EPCTL_CNAK);
  1565. writel(tmp, &ep->regs->ctl);
  1566. ep->halted = 0;
  1567. UDC_QUEUE_CNAK(ep, ep->num);
  1568. }
  1569. }
  1570. }
  1571. /* Stall timer callback to poll S bit and set it again after */
  1572. static void udc_pollstall_timer_function(unsigned long v)
  1573. {
  1574. struct udc_ep *ep;
  1575. int halted = 0;
  1576. spin_lock_irq(&udc_stall_spinlock);
  1577. /*
  1578. * only one IN and OUT endpoints are handled
  1579. * IN poll stall
  1580. */
  1581. ep = &udc->ep[UDC_EPIN_IX];
  1582. udc_handle_halt_state(ep);
  1583. if (ep->halted)
  1584. halted = 1;
  1585. /* OUT poll stall */
  1586. ep = &udc->ep[UDC_EPOUT_IX];
  1587. udc_handle_halt_state(ep);
  1588. if (ep->halted)
  1589. halted = 1;
  1590. /* setup timer again when still halted */
  1591. if (!stop_pollstall_timer && halted) {
  1592. udc_pollstall_timer.expires = jiffies +
  1593. HZ * UDC_POLLSTALL_TIMER_USECONDS
  1594. / (1000 * 1000);
  1595. add_timer(&udc_pollstall_timer);
  1596. }
  1597. spin_unlock_irq(&udc_stall_spinlock);
  1598. if (stop_pollstall_timer)
  1599. complete(&on_pollstall_exit);
  1600. }
  1601. /* Inits endpoint 0 so that SETUP packets are processed */
  1602. static void activate_control_endpoints(struct udc *dev)
  1603. {
  1604. u32 tmp;
  1605. DBG(dev, "activate_control_endpoints\n");
  1606. /* flush fifo */
  1607. tmp = readl(&dev->ep[UDC_EP0IN_IX].regs->ctl);
  1608. tmp |= AMD_BIT(UDC_EPCTL_F);
  1609. writel(tmp, &dev->ep[UDC_EP0IN_IX].regs->ctl);
  1610. /* set ep0 directions */
  1611. dev->ep[UDC_EP0IN_IX].in = 1;
  1612. dev->ep[UDC_EP0OUT_IX].in = 0;
  1613. /* set buffer size (tx fifo entries) of EP0_IN */
  1614. tmp = readl(&dev->ep[UDC_EP0IN_IX].regs->bufin_framenum);
  1615. if (dev->gadget.speed == USB_SPEED_FULL)
  1616. tmp = AMD_ADDBITS(tmp, UDC_FS_EPIN0_BUFF_SIZE,
  1617. UDC_EPIN_BUFF_SIZE);
  1618. else if (dev->gadget.speed == USB_SPEED_HIGH)
  1619. tmp = AMD_ADDBITS(tmp, UDC_EPIN0_BUFF_SIZE,
  1620. UDC_EPIN_BUFF_SIZE);
  1621. writel(tmp, &dev->ep[UDC_EP0IN_IX].regs->bufin_framenum);
  1622. /* set max packet size of EP0_IN */
  1623. tmp = readl(&dev->ep[UDC_EP0IN_IX].regs->bufout_maxpkt);
  1624. if (dev->gadget.speed == USB_SPEED_FULL)
  1625. tmp = AMD_ADDBITS(tmp, UDC_FS_EP0IN_MAX_PKT_SIZE,
  1626. UDC_EP_MAX_PKT_SIZE);
  1627. else if (dev->gadget.speed == USB_SPEED_HIGH)
  1628. tmp = AMD_ADDBITS(tmp, UDC_EP0IN_MAX_PKT_SIZE,
  1629. UDC_EP_MAX_PKT_SIZE);
  1630. writel(tmp, &dev->ep[UDC_EP0IN_IX].regs->bufout_maxpkt);
  1631. /* set max packet size of EP0_OUT */
  1632. tmp = readl(&dev->ep[UDC_EP0OUT_IX].regs->bufout_maxpkt);
  1633. if (dev->gadget.speed == USB_SPEED_FULL)
  1634. tmp = AMD_ADDBITS(tmp, UDC_FS_EP0OUT_MAX_PKT_SIZE,
  1635. UDC_EP_MAX_PKT_SIZE);
  1636. else if (dev->gadget.speed == USB_SPEED_HIGH)
  1637. tmp = AMD_ADDBITS(tmp, UDC_EP0OUT_MAX_PKT_SIZE,
  1638. UDC_EP_MAX_PKT_SIZE);
  1639. writel(tmp, &dev->ep[UDC_EP0OUT_IX].regs->bufout_maxpkt);
  1640. /* set max packet size of EP0 in UDC CSR */
  1641. tmp = readl(&dev->csr->ne[0]);
  1642. if (dev->gadget.speed == USB_SPEED_FULL)
  1643. tmp = AMD_ADDBITS(tmp, UDC_FS_EP0OUT_MAX_PKT_SIZE,
  1644. UDC_CSR_NE_MAX_PKT);
  1645. else if (dev->gadget.speed == USB_SPEED_HIGH)
  1646. tmp = AMD_ADDBITS(tmp, UDC_EP0OUT_MAX_PKT_SIZE,
  1647. UDC_CSR_NE_MAX_PKT);
  1648. writel(tmp, &dev->csr->ne[0]);
  1649. if (use_dma) {
  1650. dev->ep[UDC_EP0OUT_IX].td->status |=
  1651. AMD_BIT(UDC_DMA_OUT_STS_L);
  1652. /* write dma desc address */
  1653. writel(dev->ep[UDC_EP0OUT_IX].td_stp_dma,
  1654. &dev->ep[UDC_EP0OUT_IX].regs->subptr);
  1655. writel(dev->ep[UDC_EP0OUT_IX].td_phys,
  1656. &dev->ep[UDC_EP0OUT_IX].regs->desptr);
  1657. /* stop RDE timer */
  1658. if (timer_pending(&udc_timer)) {
  1659. set_rde = 0;
  1660. mod_timer(&udc_timer, jiffies - 1);
  1661. }
  1662. /* stop pollstall timer */
  1663. if (timer_pending(&udc_pollstall_timer))
  1664. mod_timer(&udc_pollstall_timer, jiffies - 1);
  1665. /* enable DMA */
  1666. tmp = readl(&dev->regs->ctl);
  1667. tmp |= AMD_BIT(UDC_DEVCTL_MODE)
  1668. | AMD_BIT(UDC_DEVCTL_RDE)
  1669. | AMD_BIT(UDC_DEVCTL_TDE);
  1670. if (use_dma_bufferfill_mode)
  1671. tmp |= AMD_BIT(UDC_DEVCTL_BF);
  1672. else if (use_dma_ppb_du)
  1673. tmp |= AMD_BIT(UDC_DEVCTL_DU);
  1674. writel(tmp, &dev->regs->ctl);
  1675. }
  1676. /* clear NAK by writing CNAK for EP0IN */
  1677. tmp = readl(&dev->ep[UDC_EP0IN_IX].regs->ctl);
  1678. tmp |= AMD_BIT(UDC_EPCTL_CNAK);
  1679. writel(tmp, &dev->ep[UDC_EP0IN_IX].regs->ctl);
  1680. dev->ep[UDC_EP0IN_IX].naking = 0;
  1681. UDC_QUEUE_CNAK(&dev->ep[UDC_EP0IN_IX], UDC_EP0IN_IX);
  1682. /* clear NAK by writing CNAK for EP0OUT */
  1683. tmp = readl(&dev->ep[UDC_EP0OUT_IX].regs->ctl);
  1684. tmp |= AMD_BIT(UDC_EPCTL_CNAK);
  1685. writel(tmp, &dev->ep[UDC_EP0OUT_IX].regs->ctl);
  1686. dev->ep[UDC_EP0OUT_IX].naking = 0;
  1687. UDC_QUEUE_CNAK(&dev->ep[UDC_EP0OUT_IX], UDC_EP0OUT_IX);
  1688. }
  1689. /* Make endpoint 0 ready for control traffic */
  1690. static int setup_ep0(struct udc *dev)
  1691. {
  1692. activate_control_endpoints(dev);
  1693. /* enable ep0 interrupts */
  1694. udc_enable_ep0_interrupts(dev);
  1695. /* enable device setup interrupts */
  1696. udc_enable_dev_setup_interrupts(dev);
  1697. return 0;
  1698. }
  1699. /* Called by gadget driver to register itself */
  1700. static int amd5536_udc_start(struct usb_gadget *g,
  1701. struct usb_gadget_driver *driver)
  1702. {
  1703. struct udc *dev = to_amd5536_udc(g);
  1704. u32 tmp;
  1705. driver->driver.bus = NULL;
  1706. dev->driver = driver;
  1707. /* Some gadget drivers use both ep0 directions.
  1708. * NOTE: to gadget driver, ep0 is just one endpoint...
  1709. */
  1710. dev->ep[UDC_EP0OUT_IX].ep.driver_data =
  1711. dev->ep[UDC_EP0IN_IX].ep.driver_data;
  1712. /* get ready for ep0 traffic */
  1713. setup_ep0(dev);
  1714. /* clear SD */
  1715. tmp = readl(&dev->regs->ctl);
  1716. tmp = tmp & AMD_CLEAR_BIT(UDC_DEVCTL_SD);
  1717. writel(tmp, &dev->regs->ctl);
  1718. usb_connect(dev);
  1719. return 0;
  1720. }
  1721. /* shutdown requests and disconnect from gadget */
  1722. static void
  1723. shutdown(struct udc *dev, struct usb_gadget_driver *driver)
  1724. __releases(dev->lock)
  1725. __acquires(dev->lock)
  1726. {
  1727. int tmp;
  1728. /* empty queues and init hardware */
  1729. udc_basic_init(dev);
  1730. for (tmp = 0; tmp < UDC_EP_NUM; tmp++)
  1731. empty_req_queue(&dev->ep[tmp]);
  1732. udc_setup_endpoints(dev);
  1733. }
  1734. /* Called by gadget driver to unregister itself */
  1735. static int amd5536_udc_stop(struct usb_gadget *g)
  1736. {
  1737. struct udc *dev = to_amd5536_udc(g);
  1738. unsigned long flags;
  1739. u32 tmp;
  1740. spin_lock_irqsave(&dev->lock, flags);
  1741. udc_mask_unused_interrupts(dev);
  1742. shutdown(dev, NULL);
  1743. spin_unlock_irqrestore(&dev->lock, flags);
  1744. dev->driver = NULL;
  1745. /* set SD */
  1746. tmp = readl(&dev->regs->ctl);
  1747. tmp |= AMD_BIT(UDC_DEVCTL_SD);
  1748. writel(tmp, &dev->regs->ctl);
  1749. return 0;
  1750. }
  1751. /* Clear pending NAK bits */
  1752. static void udc_process_cnak_queue(struct udc *dev)
  1753. {
  1754. u32 tmp;
  1755. u32 reg;
  1756. /* check epin's */
  1757. DBG(dev, "CNAK pending queue processing\n");
  1758. for (tmp = 0; tmp < UDC_EPIN_NUM_USED; tmp++) {
  1759. if (cnak_pending & (1 << tmp)) {
  1760. DBG(dev, "CNAK pending for ep%d\n", tmp);
  1761. /* clear NAK by writing CNAK */
  1762. reg = readl(&dev->ep[tmp].regs->ctl);
  1763. reg |= AMD_BIT(UDC_EPCTL_CNAK);
  1764. writel(reg, &dev->ep[tmp].regs->ctl);
  1765. dev->ep[tmp].naking = 0;
  1766. UDC_QUEUE_CNAK(&dev->ep[tmp], dev->ep[tmp].num);
  1767. }
  1768. }
  1769. /* ... and ep0out */
  1770. if (cnak_pending & (1 << UDC_EP0OUT_IX)) {
  1771. DBG(dev, "CNAK pending for ep%d\n", UDC_EP0OUT_IX);
  1772. /* clear NAK by writing CNAK */
  1773. reg = readl(&dev->ep[UDC_EP0OUT_IX].regs->ctl);
  1774. reg |= AMD_BIT(UDC_EPCTL_CNAK);
  1775. writel(reg, &dev->ep[UDC_EP0OUT_IX].regs->ctl);
  1776. dev->ep[UDC_EP0OUT_IX].naking = 0;
  1777. UDC_QUEUE_CNAK(&dev->ep[UDC_EP0OUT_IX],
  1778. dev->ep[UDC_EP0OUT_IX].num);
  1779. }
  1780. }
  1781. /* Enabling RX DMA after setup packet */
  1782. static void udc_ep0_set_rde(struct udc *dev)
  1783. {
  1784. if (use_dma) {
  1785. /*
  1786. * only enable RXDMA when no data endpoint enabled
  1787. * or data is queued
  1788. */
  1789. if (!dev->data_ep_enabled || dev->data_ep_queued) {
  1790. udc_set_rde(dev);
  1791. } else {
  1792. /*
  1793. * setup timer for enabling RDE (to not enable
  1794. * RXFIFO DMA for data endpoints to early)
  1795. */
  1796. if (set_rde != 0 && !timer_pending(&udc_timer)) {
  1797. udc_timer.expires =
  1798. jiffies + HZ/UDC_RDE_TIMER_DIV;
  1799. set_rde = 1;
  1800. if (!stop_timer)
  1801. add_timer(&udc_timer);
  1802. }
  1803. }
  1804. }
  1805. }
  1806. /* Interrupt handler for data OUT traffic */
  1807. static irqreturn_t udc_data_out_isr(struct udc *dev, int ep_ix)
  1808. {
  1809. irqreturn_t ret_val = IRQ_NONE;
  1810. u32 tmp;
  1811. struct udc_ep *ep;
  1812. struct udc_request *req;
  1813. unsigned int count;
  1814. struct udc_data_dma *td = NULL;
  1815. unsigned dma_done;
  1816. VDBG(dev, "ep%d irq\n", ep_ix);
  1817. ep = &dev->ep[ep_ix];
  1818. tmp = readl(&ep->regs->sts);
  1819. if (use_dma) {
  1820. /* BNA event ? */
  1821. if (tmp & AMD_BIT(UDC_EPSTS_BNA)) {
  1822. DBG(dev, "BNA ep%dout occurred - DESPTR = %x\n",
  1823. ep->num, readl(&ep->regs->desptr));
  1824. /* clear BNA */
  1825. writel(tmp | AMD_BIT(UDC_EPSTS_BNA), &ep->regs->sts);
  1826. if (!ep->cancel_transfer)
  1827. ep->bna_occurred = 1;
  1828. else
  1829. ep->cancel_transfer = 0;
  1830. ret_val = IRQ_HANDLED;
  1831. goto finished;
  1832. }
  1833. }
  1834. /* HE event ? */
  1835. if (tmp & AMD_BIT(UDC_EPSTS_HE)) {
  1836. dev_err(&dev->pdev->dev, "HE ep%dout occurred\n", ep->num);
  1837. /* clear HE */
  1838. writel(tmp | AMD_BIT(UDC_EPSTS_HE), &ep->regs->sts);
  1839. ret_val = IRQ_HANDLED;
  1840. goto finished;
  1841. }
  1842. if (!list_empty(&ep->queue)) {
  1843. /* next request */
  1844. req = list_entry(ep->queue.next,
  1845. struct udc_request, queue);
  1846. } else {
  1847. req = NULL;
  1848. udc_rxfifo_pending = 1;
  1849. }
  1850. VDBG(dev, "req = %p\n", req);
  1851. /* fifo mode */
  1852. if (!use_dma) {
  1853. /* read fifo */
  1854. if (req && udc_rxfifo_read(ep, req)) {
  1855. ret_val = IRQ_HANDLED;
  1856. /* finish */
  1857. complete_req(ep, req, 0);
  1858. /* next request */
  1859. if (!list_empty(&ep->queue) && !ep->halted) {
  1860. req = list_entry(ep->queue.next,
  1861. struct udc_request, queue);
  1862. } else
  1863. req = NULL;
  1864. }
  1865. /* DMA */
  1866. } else if (!ep->cancel_transfer && req) {
  1867. ret_val = IRQ_HANDLED;
  1868. /* check for DMA done */
  1869. if (!use_dma_ppb) {
  1870. dma_done = AMD_GETBITS(req->td_data->status,
  1871. UDC_DMA_OUT_STS_BS);
  1872. /* packet per buffer mode - rx bytes */
  1873. } else {
  1874. /*
  1875. * if BNA occurred then recover desc. from
  1876. * BNA dummy desc.
  1877. */
  1878. if (ep->bna_occurred) {
  1879. VDBG(dev, "Recover desc. from BNA dummy\n");
  1880. memcpy(req->td_data, ep->bna_dummy_req->td_data,
  1881. sizeof(struct udc_data_dma));
  1882. ep->bna_occurred = 0;
  1883. udc_init_bna_dummy(ep->req);
  1884. }
  1885. td = udc_get_last_dma_desc(req);
  1886. dma_done = AMD_GETBITS(td->status, UDC_DMA_OUT_STS_BS);
  1887. }
  1888. if (dma_done == UDC_DMA_OUT_STS_BS_DMA_DONE) {
  1889. /* buffer fill mode - rx bytes */
  1890. if (!use_dma_ppb) {
  1891. /* received number bytes */
  1892. count = AMD_GETBITS(req->td_data->status,
  1893. UDC_DMA_OUT_STS_RXBYTES);
  1894. VDBG(dev, "rx bytes=%u\n", count);
  1895. /* packet per buffer mode - rx bytes */
  1896. } else {
  1897. VDBG(dev, "req->td_data=%p\n", req->td_data);
  1898. VDBG(dev, "last desc = %p\n", td);
  1899. /* received number bytes */
  1900. if (use_dma_ppb_du) {
  1901. /* every desc. counts bytes */
  1902. count = udc_get_ppbdu_rxbytes(req);
  1903. } else {
  1904. /* last desc. counts bytes */
  1905. count = AMD_GETBITS(td->status,
  1906. UDC_DMA_OUT_STS_RXBYTES);
  1907. if (!count && req->req.length
  1908. == UDC_DMA_MAXPACKET) {
  1909. /*
  1910. * on 64k packets the RXBYTES
  1911. * field is zero
  1912. */
  1913. count = UDC_DMA_MAXPACKET;
  1914. }
  1915. }
  1916. VDBG(dev, "last desc rx bytes=%u\n", count);
  1917. }
  1918. tmp = req->req.length - req->req.actual;
  1919. if (count > tmp) {
  1920. if ((tmp % ep->ep.maxpacket) != 0) {
  1921. DBG(dev, "%s: rx %db, space=%db\n",
  1922. ep->ep.name, count, tmp);
  1923. req->req.status = -EOVERFLOW;
  1924. }
  1925. count = tmp;
  1926. }
  1927. req->req.actual += count;
  1928. req->dma_going = 0;
  1929. /* complete request */
  1930. complete_req(ep, req, 0);
  1931. /* next request */
  1932. if (!list_empty(&ep->queue) && !ep->halted) {
  1933. req = list_entry(ep->queue.next,
  1934. struct udc_request,
  1935. queue);
  1936. /*
  1937. * DMA may be already started by udc_queue()
  1938. * called by gadget drivers completion
  1939. * routine. This happens when queue
  1940. * holds one request only.
  1941. */
  1942. if (req->dma_going == 0) {
  1943. /* next dma */
  1944. if (prep_dma(ep, req, GFP_ATOMIC) != 0)
  1945. goto finished;
  1946. /* write desc pointer */
  1947. writel(req->td_phys,
  1948. &ep->regs->desptr);
  1949. req->dma_going = 1;
  1950. /* enable DMA */
  1951. udc_set_rde(dev);
  1952. }
  1953. } else {
  1954. /*
  1955. * implant BNA dummy descriptor to allow
  1956. * RXFIFO opening by RDE
  1957. */
  1958. if (ep->bna_dummy_req) {
  1959. /* write desc pointer */
  1960. writel(ep->bna_dummy_req->td_phys,
  1961. &ep->regs->desptr);
  1962. ep->bna_occurred = 0;
  1963. }
  1964. /*
  1965. * schedule timer for setting RDE if queue
  1966. * remains empty to allow ep0 packets pass
  1967. * through
  1968. */
  1969. if (set_rde != 0
  1970. && !timer_pending(&udc_timer)) {
  1971. udc_timer.expires =
  1972. jiffies
  1973. + HZ*UDC_RDE_TIMER_SECONDS;
  1974. set_rde = 1;
  1975. if (!stop_timer)
  1976. add_timer(&udc_timer);
  1977. }
  1978. if (ep->num != UDC_EP0OUT_IX)
  1979. dev->data_ep_queued = 0;
  1980. }
  1981. } else {
  1982. /*
  1983. * RX DMA must be reenabled for each desc in PPBDU mode
  1984. * and must be enabled for PPBNDU mode in case of BNA
  1985. */
  1986. udc_set_rde(dev);
  1987. }
  1988. } else if (ep->cancel_transfer) {
  1989. ret_val = IRQ_HANDLED;
  1990. ep->cancel_transfer = 0;
  1991. }
  1992. /* check pending CNAKS */
  1993. if (cnak_pending) {
  1994. /* CNAk processing when rxfifo empty only */
  1995. if (readl(&dev->regs->sts) & AMD_BIT(UDC_DEVSTS_RXFIFO_EMPTY))
  1996. udc_process_cnak_queue(dev);
  1997. }
  1998. /* clear OUT bits in ep status */
  1999. writel(UDC_EPSTS_OUT_CLEAR, &ep->regs->sts);
  2000. finished:
  2001. return ret_val;
  2002. }
  2003. /* Interrupt handler for data IN traffic */
  2004. static irqreturn_t udc_data_in_isr(struct udc *dev, int ep_ix)
  2005. {
  2006. irqreturn_t ret_val = IRQ_NONE;
  2007. u32 tmp;
  2008. u32 epsts;
  2009. struct udc_ep *ep;
  2010. struct udc_request *req;
  2011. struct udc_data_dma *td;
  2012. unsigned dma_done;
  2013. unsigned len;
  2014. ep = &dev->ep[ep_ix];
  2015. epsts = readl(&ep->regs->sts);
  2016. if (use_dma) {
  2017. /* BNA ? */
  2018. if (epsts & AMD_BIT(UDC_EPSTS_BNA)) {
  2019. dev_err(&dev->pdev->dev,
  2020. "BNA ep%din occurred - DESPTR = %08lx\n",
  2021. ep->num,
  2022. (unsigned long) readl(&ep->regs->desptr));
  2023. /* clear BNA */
  2024. writel(epsts, &ep->regs->sts);
  2025. ret_val = IRQ_HANDLED;
  2026. goto finished;
  2027. }
  2028. }
  2029. /* HE event ? */
  2030. if (epsts & AMD_BIT(UDC_EPSTS_HE)) {
  2031. dev_err(&dev->pdev->dev,
  2032. "HE ep%dn occurred - DESPTR = %08lx\n",
  2033. ep->num, (unsigned long) readl(&ep->regs->desptr));
  2034. /* clear HE */
  2035. writel(epsts | AMD_BIT(UDC_EPSTS_HE), &ep->regs->sts);
  2036. ret_val = IRQ_HANDLED;
  2037. goto finished;
  2038. }
  2039. /* DMA completion */
  2040. if (epsts & AMD_BIT(UDC_EPSTS_TDC)) {
  2041. VDBG(dev, "TDC set- completion\n");
  2042. ret_val = IRQ_HANDLED;
  2043. if (!ep->cancel_transfer && !list_empty(&ep->queue)) {
  2044. req = list_entry(ep->queue.next,
  2045. struct udc_request, queue);
  2046. /*
  2047. * length bytes transferred
  2048. * check dma done of last desc. in PPBDU mode
  2049. */
  2050. if (use_dma_ppb_du) {
  2051. td = udc_get_last_dma_desc(req);
  2052. if (td) {
  2053. dma_done =
  2054. AMD_GETBITS(td->status,
  2055. UDC_DMA_IN_STS_BS);
  2056. /* don't care DMA done */
  2057. req->req.actual = req->req.length;
  2058. }
  2059. } else {
  2060. /* assume all bytes transferred */
  2061. req->req.actual = req->req.length;
  2062. }
  2063. if (req->req.actual == req->req.length) {
  2064. /* complete req */
  2065. complete_req(ep, req, 0);
  2066. req->dma_going = 0;
  2067. /* further request available ? */
  2068. if (list_empty(&ep->queue)) {
  2069. /* disable interrupt */
  2070. tmp = readl(&dev->regs->ep_irqmsk);
  2071. tmp |= AMD_BIT(ep->num);
  2072. writel(tmp, &dev->regs->ep_irqmsk);
  2073. }
  2074. }
  2075. }
  2076. ep->cancel_transfer = 0;
  2077. }
  2078. /*
  2079. * status reg has IN bit set and TDC not set (if TDC was handled,
  2080. * IN must not be handled (UDC defect) ?
  2081. */
  2082. if ((epsts & AMD_BIT(UDC_EPSTS_IN))
  2083. && !(epsts & AMD_BIT(UDC_EPSTS_TDC))) {
  2084. ret_val = IRQ_HANDLED;
  2085. if (!list_empty(&ep->queue)) {
  2086. /* next request */
  2087. req = list_entry(ep->queue.next,
  2088. struct udc_request, queue);
  2089. /* FIFO mode */
  2090. if (!use_dma) {
  2091. /* write fifo */
  2092. udc_txfifo_write(ep, &req->req);
  2093. len = req->req.length - req->req.actual;
  2094. if (len > ep->ep.maxpacket)
  2095. len = ep->ep.maxpacket;
  2096. req->req.actual += len;
  2097. if (req->req.actual == req->req.length
  2098. || (len != ep->ep.maxpacket)) {
  2099. /* complete req */
  2100. complete_req(ep, req, 0);
  2101. }
  2102. /* DMA */
  2103. } else if (req && !req->dma_going) {
  2104. VDBG(dev, "IN DMA : req=%p req->td_data=%p\n",
  2105. req, req->td_data);
  2106. if (req->td_data) {
  2107. req->dma_going = 1;
  2108. /*
  2109. * unset L bit of first desc.
  2110. * for chain
  2111. */
  2112. if (use_dma_ppb && req->req.length >
  2113. ep->ep.maxpacket) {
  2114. req->td_data->status &=
  2115. AMD_CLEAR_BIT(
  2116. UDC_DMA_IN_STS_L);
  2117. }
  2118. /* write desc pointer */
  2119. writel(req->td_phys, &ep->regs->desptr);
  2120. /* set HOST READY */
  2121. req->td_data->status =
  2122. AMD_ADDBITS(
  2123. req->td_data->status,
  2124. UDC_DMA_IN_STS_BS_HOST_READY,
  2125. UDC_DMA_IN_STS_BS);
  2126. /* set poll demand bit */
  2127. tmp = readl(&ep->regs->ctl);
  2128. tmp |= AMD_BIT(UDC_EPCTL_P);
  2129. writel(tmp, &ep->regs->ctl);
  2130. }
  2131. }
  2132. } else if (!use_dma && ep->in) {
  2133. /* disable interrupt */
  2134. tmp = readl(
  2135. &dev->regs->ep_irqmsk);
  2136. tmp |= AMD_BIT(ep->num);
  2137. writel(tmp,
  2138. &dev->regs->ep_irqmsk);
  2139. }
  2140. }
  2141. /* clear status bits */
  2142. writel(epsts, &ep->regs->sts);
  2143. finished:
  2144. return ret_val;
  2145. }
  2146. /* Interrupt handler for Control OUT traffic */
  2147. static irqreturn_t udc_control_out_isr(struct udc *dev)
  2148. __releases(dev->lock)
  2149. __acquires(dev->lock)
  2150. {
  2151. irqreturn_t ret_val = IRQ_NONE;
  2152. u32 tmp;
  2153. int setup_supported;
  2154. u32 count;
  2155. int set = 0;
  2156. struct udc_ep *ep;
  2157. struct udc_ep *ep_tmp;
  2158. ep = &dev->ep[UDC_EP0OUT_IX];
  2159. /* clear irq */
  2160. writel(AMD_BIT(UDC_EPINT_OUT_EP0), &dev->regs->ep_irqsts);
  2161. tmp = readl(&dev->ep[UDC_EP0OUT_IX].regs->sts);
  2162. /* check BNA and clear if set */
  2163. if (tmp & AMD_BIT(UDC_EPSTS_BNA)) {
  2164. VDBG(dev, "ep0: BNA set\n");
  2165. writel(AMD_BIT(UDC_EPSTS_BNA),
  2166. &dev->ep[UDC_EP0OUT_IX].regs->sts);
  2167. ep->bna_occurred = 1;
  2168. ret_val = IRQ_HANDLED;
  2169. goto finished;
  2170. }
  2171. /* type of data: SETUP or DATA 0 bytes */
  2172. tmp = AMD_GETBITS(tmp, UDC_EPSTS_OUT);
  2173. VDBG(dev, "data_typ = %x\n", tmp);
  2174. /* setup data */
  2175. if (tmp == UDC_EPSTS_OUT_SETUP) {
  2176. ret_val = IRQ_HANDLED;
  2177. ep->dev->stall_ep0in = 0;
  2178. dev->waiting_zlp_ack_ep0in = 0;
  2179. /* set NAK for EP0_IN */
  2180. tmp = readl(&dev->ep[UDC_EP0IN_IX].regs->ctl);
  2181. tmp |= AMD_BIT(UDC_EPCTL_SNAK);
  2182. writel(tmp, &dev->ep[UDC_EP0IN_IX].regs->ctl);
  2183. dev->ep[UDC_EP0IN_IX].naking = 1;
  2184. /* get setup data */
  2185. if (use_dma) {
  2186. /* clear OUT bits in ep status */
  2187. writel(UDC_EPSTS_OUT_CLEAR,
  2188. &dev->ep[UDC_EP0OUT_IX].regs->sts);
  2189. setup_data.data[0] =
  2190. dev->ep[UDC_EP0OUT_IX].td_stp->data12;
  2191. setup_data.data[1] =
  2192. dev->ep[UDC_EP0OUT_IX].td_stp->data34;
  2193. /* set HOST READY */
  2194. dev->ep[UDC_EP0OUT_IX].td_stp->status =
  2195. UDC_DMA_STP_STS_BS_HOST_READY;
  2196. } else {
  2197. /* read fifo */
  2198. udc_rxfifo_read_dwords(dev, setup_data.data, 2);
  2199. }
  2200. /* determine direction of control data */
  2201. if ((setup_data.request.bRequestType & USB_DIR_IN) != 0) {
  2202. dev->gadget.ep0 = &dev->ep[UDC_EP0IN_IX].ep;
  2203. /* enable RDE */
  2204. udc_ep0_set_rde(dev);
  2205. set = 0;
  2206. } else {
  2207. dev->gadget.ep0 = &dev->ep[UDC_EP0OUT_IX].ep;
  2208. /*
  2209. * implant BNA dummy descriptor to allow RXFIFO opening
  2210. * by RDE
  2211. */
  2212. if (ep->bna_dummy_req) {
  2213. /* write desc pointer */
  2214. writel(ep->bna_dummy_req->td_phys,
  2215. &dev->ep[UDC_EP0OUT_IX].regs->desptr);
  2216. ep->bna_occurred = 0;
  2217. }
  2218. set = 1;
  2219. dev->ep[UDC_EP0OUT_IX].naking = 1;
  2220. /*
  2221. * setup timer for enabling RDE (to not enable
  2222. * RXFIFO DMA for data to early)
  2223. */
  2224. set_rde = 1;
  2225. if (!timer_pending(&udc_timer)) {
  2226. udc_timer.expires = jiffies +
  2227. HZ/UDC_RDE_TIMER_DIV;
  2228. if (!stop_timer)
  2229. add_timer(&udc_timer);
  2230. }
  2231. }
  2232. /*
  2233. * mass storage reset must be processed here because
  2234. * next packet may be a CLEAR_FEATURE HALT which would not
  2235. * clear the stall bit when no STALL handshake was received
  2236. * before (autostall can cause this)
  2237. */
  2238. if (setup_data.data[0] == UDC_MSCRES_DWORD0
  2239. && setup_data.data[1] == UDC_MSCRES_DWORD1) {
  2240. DBG(dev, "MSC Reset\n");
  2241. /*
  2242. * clear stall bits
  2243. * only one IN and OUT endpoints are handled
  2244. */
  2245. ep_tmp = &udc->ep[UDC_EPIN_IX];
  2246. udc_set_halt(&ep_tmp->ep, 0);
  2247. ep_tmp = &udc->ep[UDC_EPOUT_IX];
  2248. udc_set_halt(&ep_tmp->ep, 0);
  2249. }
  2250. /* call gadget with setup data received */
  2251. spin_unlock(&dev->lock);
  2252. setup_supported = dev->driver->setup(&dev->gadget,
  2253. &setup_data.request);
  2254. spin_lock(&dev->lock);
  2255. tmp = readl(&dev->ep[UDC_EP0IN_IX].regs->ctl);
  2256. /* ep0 in returns data (not zlp) on IN phase */
  2257. if (setup_supported >= 0 && setup_supported <
  2258. UDC_EP0IN_MAXPACKET) {
  2259. /* clear NAK by writing CNAK in EP0_IN */
  2260. tmp |= AMD_BIT(UDC_EPCTL_CNAK);
  2261. writel(tmp, &dev->ep[UDC_EP0IN_IX].regs->ctl);
  2262. dev->ep[UDC_EP0IN_IX].naking = 0;
  2263. UDC_QUEUE_CNAK(&dev->ep[UDC_EP0IN_IX], UDC_EP0IN_IX);
  2264. /* if unsupported request then stall */
  2265. } else if (setup_supported < 0) {
  2266. tmp |= AMD_BIT(UDC_EPCTL_S);
  2267. writel(tmp, &dev->ep[UDC_EP0IN_IX].regs->ctl);
  2268. } else
  2269. dev->waiting_zlp_ack_ep0in = 1;
  2270. /* clear NAK by writing CNAK in EP0_OUT */
  2271. if (!set) {
  2272. tmp = readl(&dev->ep[UDC_EP0OUT_IX].regs->ctl);
  2273. tmp |= AMD_BIT(UDC_EPCTL_CNAK);
  2274. writel(tmp, &dev->ep[UDC_EP0OUT_IX].regs->ctl);
  2275. dev->ep[UDC_EP0OUT_IX].naking = 0;
  2276. UDC_QUEUE_CNAK(&dev->ep[UDC_EP0OUT_IX], UDC_EP0OUT_IX);
  2277. }
  2278. if (!use_dma) {
  2279. /* clear OUT bits in ep status */
  2280. writel(UDC_EPSTS_OUT_CLEAR,
  2281. &dev->ep[UDC_EP0OUT_IX].regs->sts);
  2282. }
  2283. /* data packet 0 bytes */
  2284. } else if (tmp == UDC_EPSTS_OUT_DATA) {
  2285. /* clear OUT bits in ep status */
  2286. writel(UDC_EPSTS_OUT_CLEAR, &dev->ep[UDC_EP0OUT_IX].regs->sts);
  2287. /* get setup data: only 0 packet */
  2288. if (use_dma) {
  2289. /* no req if 0 packet, just reactivate */
  2290. if (list_empty(&dev->ep[UDC_EP0OUT_IX].queue)) {
  2291. VDBG(dev, "ZLP\n");
  2292. /* set HOST READY */
  2293. dev->ep[UDC_EP0OUT_IX].td->status =
  2294. AMD_ADDBITS(
  2295. dev->ep[UDC_EP0OUT_IX].td->status,
  2296. UDC_DMA_OUT_STS_BS_HOST_READY,
  2297. UDC_DMA_OUT_STS_BS);
  2298. /* enable RDE */
  2299. udc_ep0_set_rde(dev);
  2300. ret_val = IRQ_HANDLED;
  2301. } else {
  2302. /* control write */
  2303. ret_val |= udc_data_out_isr(dev, UDC_EP0OUT_IX);
  2304. /* re-program desc. pointer for possible ZLPs */
  2305. writel(dev->ep[UDC_EP0OUT_IX].td_phys,
  2306. &dev->ep[UDC_EP0OUT_IX].regs->desptr);
  2307. /* enable RDE */
  2308. udc_ep0_set_rde(dev);
  2309. }
  2310. } else {
  2311. /* received number bytes */
  2312. count = readl(&dev->ep[UDC_EP0OUT_IX].regs->sts);
  2313. count = AMD_GETBITS(count, UDC_EPSTS_RX_PKT_SIZE);
  2314. /* out data for fifo mode not working */
  2315. count = 0;
  2316. /* 0 packet or real data ? */
  2317. if (count != 0) {
  2318. ret_val |= udc_data_out_isr(dev, UDC_EP0OUT_IX);
  2319. } else {
  2320. /* dummy read confirm */
  2321. readl(&dev->ep[UDC_EP0OUT_IX].regs->confirm);
  2322. ret_val = IRQ_HANDLED;
  2323. }
  2324. }
  2325. }
  2326. /* check pending CNAKS */
  2327. if (cnak_pending) {
  2328. /* CNAk processing when rxfifo empty only */
  2329. if (readl(&dev->regs->sts) & AMD_BIT(UDC_DEVSTS_RXFIFO_EMPTY))
  2330. udc_process_cnak_queue(dev);
  2331. }
  2332. finished:
  2333. return ret_val;
  2334. }
  2335. /* Interrupt handler for Control IN traffic */
  2336. static irqreturn_t udc_control_in_isr(struct udc *dev)
  2337. {
  2338. irqreturn_t ret_val = IRQ_NONE;
  2339. u32 tmp;
  2340. struct udc_ep *ep;
  2341. struct udc_request *req;
  2342. unsigned len;
  2343. ep = &dev->ep[UDC_EP0IN_IX];
  2344. /* clear irq */
  2345. writel(AMD_BIT(UDC_EPINT_IN_EP0), &dev->regs->ep_irqsts);
  2346. tmp = readl(&dev->ep[UDC_EP0IN_IX].regs->sts);
  2347. /* DMA completion */
  2348. if (tmp & AMD_BIT(UDC_EPSTS_TDC)) {
  2349. VDBG(dev, "isr: TDC clear\n");
  2350. ret_val = IRQ_HANDLED;
  2351. /* clear TDC bit */
  2352. writel(AMD_BIT(UDC_EPSTS_TDC),
  2353. &dev->ep[UDC_EP0IN_IX].regs->sts);
  2354. /* status reg has IN bit set ? */
  2355. } else if (tmp & AMD_BIT(UDC_EPSTS_IN)) {
  2356. ret_val = IRQ_HANDLED;
  2357. if (ep->dma) {
  2358. /* clear IN bit */
  2359. writel(AMD_BIT(UDC_EPSTS_IN),
  2360. &dev->ep[UDC_EP0IN_IX].regs->sts);
  2361. }
  2362. if (dev->stall_ep0in) {
  2363. DBG(dev, "stall ep0in\n");
  2364. /* halt ep0in */
  2365. tmp = readl(&ep->regs->ctl);
  2366. tmp |= AMD_BIT(UDC_EPCTL_S);
  2367. writel(tmp, &ep->regs->ctl);
  2368. } else {
  2369. if (!list_empty(&ep->queue)) {
  2370. /* next request */
  2371. req = list_entry(ep->queue.next,
  2372. struct udc_request, queue);
  2373. if (ep->dma) {
  2374. /* write desc pointer */
  2375. writel(req->td_phys, &ep->regs->desptr);
  2376. /* set HOST READY */
  2377. req->td_data->status =
  2378. AMD_ADDBITS(
  2379. req->td_data->status,
  2380. UDC_DMA_STP_STS_BS_HOST_READY,
  2381. UDC_DMA_STP_STS_BS);
  2382. /* set poll demand bit */
  2383. tmp =
  2384. readl(&dev->ep[UDC_EP0IN_IX].regs->ctl);
  2385. tmp |= AMD_BIT(UDC_EPCTL_P);
  2386. writel(tmp,
  2387. &dev->ep[UDC_EP0IN_IX].regs->ctl);
  2388. /* all bytes will be transferred */
  2389. req->req.actual = req->req.length;
  2390. /* complete req */
  2391. complete_req(ep, req, 0);
  2392. } else {
  2393. /* write fifo */
  2394. udc_txfifo_write(ep, &req->req);
  2395. /* lengh bytes transferred */
  2396. len = req->req.length - req->req.actual;
  2397. if (len > ep->ep.maxpacket)
  2398. len = ep->ep.maxpacket;
  2399. req->req.actual += len;
  2400. if (req->req.actual == req->req.length
  2401. || (len != ep->ep.maxpacket)) {
  2402. /* complete req */
  2403. complete_req(ep, req, 0);
  2404. }
  2405. }
  2406. }
  2407. }
  2408. ep->halted = 0;
  2409. dev->stall_ep0in = 0;
  2410. if (!ep->dma) {
  2411. /* clear IN bit */
  2412. writel(AMD_BIT(UDC_EPSTS_IN),
  2413. &dev->ep[UDC_EP0IN_IX].regs->sts);
  2414. }
  2415. }
  2416. return ret_val;
  2417. }
  2418. /* Interrupt handler for global device events */
  2419. static irqreturn_t udc_dev_isr(struct udc *dev, u32 dev_irq)
  2420. __releases(dev->lock)
  2421. __acquires(dev->lock)
  2422. {
  2423. irqreturn_t ret_val = IRQ_NONE;
  2424. u32 tmp;
  2425. u32 cfg;
  2426. struct udc_ep *ep;
  2427. u16 i;
  2428. u8 udc_csr_epix;
  2429. /* SET_CONFIG irq ? */
  2430. if (dev_irq & AMD_BIT(UDC_DEVINT_SC)) {
  2431. ret_val = IRQ_HANDLED;
  2432. /* read config value */
  2433. tmp = readl(&dev->regs->sts);
  2434. cfg = AMD_GETBITS(tmp, UDC_DEVSTS_CFG);
  2435. DBG(dev, "SET_CONFIG interrupt: config=%d\n", cfg);
  2436. dev->cur_config = cfg;
  2437. dev->set_cfg_not_acked = 1;
  2438. /* make usb request for gadget driver */
  2439. memset(&setup_data, 0 , sizeof(union udc_setup_data));
  2440. setup_data.request.bRequest = USB_REQ_SET_CONFIGURATION;
  2441. setup_data.request.wValue = cpu_to_le16(dev->cur_config);
  2442. /* programm the NE registers */
  2443. for (i = 0; i < UDC_EP_NUM; i++) {
  2444. ep = &dev->ep[i];
  2445. if (ep->in) {
  2446. /* ep ix in UDC CSR register space */
  2447. udc_csr_epix = ep->num;
  2448. /* OUT ep */
  2449. } else {
  2450. /* ep ix in UDC CSR register space */
  2451. udc_csr_epix = ep->num - UDC_CSR_EP_OUT_IX_OFS;
  2452. }
  2453. tmp = readl(&dev->csr->ne[udc_csr_epix]);
  2454. /* ep cfg */
  2455. tmp = AMD_ADDBITS(tmp, ep->dev->cur_config,
  2456. UDC_CSR_NE_CFG);
  2457. /* write reg */
  2458. writel(tmp, &dev->csr->ne[udc_csr_epix]);
  2459. /* clear stall bits */
  2460. ep->halted = 0;
  2461. tmp = readl(&ep->regs->ctl);
  2462. tmp = tmp & AMD_CLEAR_BIT(UDC_EPCTL_S);
  2463. writel(tmp, &ep->regs->ctl);
  2464. }
  2465. /* call gadget zero with setup data received */
  2466. spin_unlock(&dev->lock);
  2467. tmp = dev->driver->setup(&dev->gadget, &setup_data.request);
  2468. spin_lock(&dev->lock);
  2469. } /* SET_INTERFACE ? */
  2470. if (dev_irq & AMD_BIT(UDC_DEVINT_SI)) {
  2471. ret_val = IRQ_HANDLED;
  2472. dev->set_cfg_not_acked = 1;
  2473. /* read interface and alt setting values */
  2474. tmp = readl(&dev->regs->sts);
  2475. dev->cur_alt = AMD_GETBITS(tmp, UDC_DEVSTS_ALT);
  2476. dev->cur_intf = AMD_GETBITS(tmp, UDC_DEVSTS_INTF);
  2477. /* make usb request for gadget driver */
  2478. memset(&setup_data, 0 , sizeof(union udc_setup_data));
  2479. setup_data.request.bRequest = USB_REQ_SET_INTERFACE;
  2480. setup_data.request.bRequestType = USB_RECIP_INTERFACE;
  2481. setup_data.request.wValue = cpu_to_le16(dev->cur_alt);
  2482. setup_data.request.wIndex = cpu_to_le16(dev->cur_intf);
  2483. DBG(dev, "SET_INTERFACE interrupt: alt=%d intf=%d\n",
  2484. dev->cur_alt, dev->cur_intf);
  2485. /* programm the NE registers */
  2486. for (i = 0; i < UDC_EP_NUM; i++) {
  2487. ep = &dev->ep[i];
  2488. if (ep->in) {
  2489. /* ep ix in UDC CSR register space */
  2490. udc_csr_epix = ep->num;
  2491. /* OUT ep */
  2492. } else {
  2493. /* ep ix in UDC CSR register space */
  2494. udc_csr_epix = ep->num - UDC_CSR_EP_OUT_IX_OFS;
  2495. }
  2496. /* UDC CSR reg */
  2497. /* set ep values */
  2498. tmp = readl(&dev->csr->ne[udc_csr_epix]);
  2499. /* ep interface */
  2500. tmp = AMD_ADDBITS(tmp, ep->dev->cur_intf,
  2501. UDC_CSR_NE_INTF);
  2502. /* tmp = AMD_ADDBITS(tmp, 2, UDC_CSR_NE_INTF); */
  2503. /* ep alt */
  2504. tmp = AMD_ADDBITS(tmp, ep->dev->cur_alt,
  2505. UDC_CSR_NE_ALT);
  2506. /* write reg */
  2507. writel(tmp, &dev->csr->ne[udc_csr_epix]);
  2508. /* clear stall bits */
  2509. ep->halted = 0;
  2510. tmp = readl(&ep->regs->ctl);
  2511. tmp = tmp & AMD_CLEAR_BIT(UDC_EPCTL_S);
  2512. writel(tmp, &ep->regs->ctl);
  2513. }
  2514. /* call gadget zero with setup data received */
  2515. spin_unlock(&dev->lock);
  2516. tmp = dev->driver->setup(&dev->gadget, &setup_data.request);
  2517. spin_lock(&dev->lock);
  2518. } /* USB reset */
  2519. if (dev_irq & AMD_BIT(UDC_DEVINT_UR)) {
  2520. DBG(dev, "USB Reset interrupt\n");
  2521. ret_val = IRQ_HANDLED;
  2522. /* allow soft reset when suspend occurs */
  2523. soft_reset_occured = 0;
  2524. dev->waiting_zlp_ack_ep0in = 0;
  2525. dev->set_cfg_not_acked = 0;
  2526. /* mask not needed interrupts */
  2527. udc_mask_unused_interrupts(dev);
  2528. /* call gadget to resume and reset configs etc. */
  2529. spin_unlock(&dev->lock);
  2530. if (dev->sys_suspended && dev->driver->resume) {
  2531. dev->driver->resume(&dev->gadget);
  2532. dev->sys_suspended = 0;
  2533. }
  2534. usb_gadget_udc_reset(&dev->gadget, dev->driver);
  2535. spin_lock(&dev->lock);
  2536. /* disable ep0 to empty req queue */
  2537. empty_req_queue(&dev->ep[UDC_EP0IN_IX]);
  2538. ep_init(dev->regs, &dev->ep[UDC_EP0IN_IX]);
  2539. /* soft reset when rxfifo not empty */
  2540. tmp = readl(&dev->regs->sts);
  2541. if (!(tmp & AMD_BIT(UDC_DEVSTS_RXFIFO_EMPTY))
  2542. && !soft_reset_after_usbreset_occured) {
  2543. udc_soft_reset(dev);
  2544. soft_reset_after_usbreset_occured++;
  2545. }
  2546. /*
  2547. * DMA reset to kill potential old DMA hw hang,
  2548. * POLL bit is already reset by ep_init() through
  2549. * disconnect()
  2550. */
  2551. DBG(dev, "DMA machine reset\n");
  2552. tmp = readl(&dev->regs->cfg);
  2553. writel(tmp | AMD_BIT(UDC_DEVCFG_DMARST), &dev->regs->cfg);
  2554. writel(tmp, &dev->regs->cfg);
  2555. /* put into initial config */
  2556. udc_basic_init(dev);
  2557. /* enable device setup interrupts */
  2558. udc_enable_dev_setup_interrupts(dev);
  2559. /* enable suspend interrupt */
  2560. tmp = readl(&dev->regs->irqmsk);
  2561. tmp &= AMD_UNMASK_BIT(UDC_DEVINT_US);
  2562. writel(tmp, &dev->regs->irqmsk);
  2563. } /* USB suspend */
  2564. if (dev_irq & AMD_BIT(UDC_DEVINT_US)) {
  2565. DBG(dev, "USB Suspend interrupt\n");
  2566. ret_val = IRQ_HANDLED;
  2567. if (dev->driver->suspend) {
  2568. spin_unlock(&dev->lock);
  2569. dev->sys_suspended = 1;
  2570. dev->driver->suspend(&dev->gadget);
  2571. spin_lock(&dev->lock);
  2572. }
  2573. } /* new speed ? */
  2574. if (dev_irq & AMD_BIT(UDC_DEVINT_ENUM)) {
  2575. DBG(dev, "ENUM interrupt\n");
  2576. ret_val = IRQ_HANDLED;
  2577. soft_reset_after_usbreset_occured = 0;
  2578. /* disable ep0 to empty req queue */
  2579. empty_req_queue(&dev->ep[UDC_EP0IN_IX]);
  2580. ep_init(dev->regs, &dev->ep[UDC_EP0IN_IX]);
  2581. /* link up all endpoints */
  2582. udc_setup_endpoints(dev);
  2583. dev_info(&dev->pdev->dev, "Connect: %s\n",
  2584. usb_speed_string(dev->gadget.speed));
  2585. /* init ep 0 */
  2586. activate_control_endpoints(dev);
  2587. /* enable ep0 interrupts */
  2588. udc_enable_ep0_interrupts(dev);
  2589. }
  2590. /* session valid change interrupt */
  2591. if (dev_irq & AMD_BIT(UDC_DEVINT_SVC)) {
  2592. DBG(dev, "USB SVC interrupt\n");
  2593. ret_val = IRQ_HANDLED;
  2594. /* check that session is not valid to detect disconnect */
  2595. tmp = readl(&dev->regs->sts);
  2596. if (!(tmp & AMD_BIT(UDC_DEVSTS_SESSVLD))) {
  2597. /* disable suspend interrupt */
  2598. tmp = readl(&dev->regs->irqmsk);
  2599. tmp |= AMD_BIT(UDC_DEVINT_US);
  2600. writel(tmp, &dev->regs->irqmsk);
  2601. DBG(dev, "USB Disconnect (session valid low)\n");
  2602. /* cleanup on disconnect */
  2603. usb_disconnect(udc);
  2604. }
  2605. }
  2606. return ret_val;
  2607. }
  2608. /* Interrupt Service Routine, see Linux Kernel Doc for parameters */
  2609. static irqreturn_t udc_irq(int irq, void *pdev)
  2610. {
  2611. struct udc *dev = pdev;
  2612. u32 reg;
  2613. u16 i;
  2614. u32 ep_irq;
  2615. irqreturn_t ret_val = IRQ_NONE;
  2616. spin_lock(&dev->lock);
  2617. /* check for ep irq */
  2618. reg = readl(&dev->regs->ep_irqsts);
  2619. if (reg) {
  2620. if (reg & AMD_BIT(UDC_EPINT_OUT_EP0))
  2621. ret_val |= udc_control_out_isr(dev);
  2622. if (reg & AMD_BIT(UDC_EPINT_IN_EP0))
  2623. ret_val |= udc_control_in_isr(dev);
  2624. /*
  2625. * data endpoint
  2626. * iterate ep's
  2627. */
  2628. for (i = 1; i < UDC_EP_NUM; i++) {
  2629. ep_irq = 1 << i;
  2630. if (!(reg & ep_irq) || i == UDC_EPINT_OUT_EP0)
  2631. continue;
  2632. /* clear irq status */
  2633. writel(ep_irq, &dev->regs->ep_irqsts);
  2634. /* irq for out ep ? */
  2635. if (i > UDC_EPIN_NUM)
  2636. ret_val |= udc_data_out_isr(dev, i);
  2637. else
  2638. ret_val |= udc_data_in_isr(dev, i);
  2639. }
  2640. }
  2641. /* check for dev irq */
  2642. reg = readl(&dev->regs->irqsts);
  2643. if (reg) {
  2644. /* clear irq */
  2645. writel(reg, &dev->regs->irqsts);
  2646. ret_val |= udc_dev_isr(dev, reg);
  2647. }
  2648. spin_unlock(&dev->lock);
  2649. return ret_val;
  2650. }
  2651. /* Tears down device */
  2652. static void gadget_release(struct device *pdev)
  2653. {
  2654. struct amd5536udc *dev = dev_get_drvdata(pdev);
  2655. kfree(dev);
  2656. }
  2657. /* Cleanup on device remove */
  2658. static void udc_remove(struct udc *dev)
  2659. {
  2660. /* remove timer */
  2661. stop_timer++;
  2662. if (timer_pending(&udc_timer))
  2663. wait_for_completion(&on_exit);
  2664. if (udc_timer.data)
  2665. del_timer_sync(&udc_timer);
  2666. /* remove pollstall timer */
  2667. stop_pollstall_timer++;
  2668. if (timer_pending(&udc_pollstall_timer))
  2669. wait_for_completion(&on_pollstall_exit);
  2670. if (udc_pollstall_timer.data)
  2671. del_timer_sync(&udc_pollstall_timer);
  2672. udc = NULL;
  2673. }
  2674. /* free all the dma pools */
  2675. static void free_dma_pools(struct udc *dev)
  2676. {
  2677. dma_pool_free(dev->stp_requests, dev->ep[UDC_EP0OUT_IX].td,
  2678. dev->ep[UDC_EP0OUT_IX].td_phys);
  2679. dma_pool_free(dev->stp_requests, dev->ep[UDC_EP0OUT_IX].td_stp,
  2680. dev->ep[UDC_EP0OUT_IX].td_stp_dma);
  2681. dma_pool_destroy(dev->stp_requests);
  2682. dma_pool_destroy(dev->data_requests);
  2683. }
  2684. /* Reset all pci context */
  2685. static void udc_pci_remove(struct pci_dev *pdev)
  2686. {
  2687. struct udc *dev;
  2688. dev = pci_get_drvdata(pdev);
  2689. usb_del_gadget_udc(&udc->gadget);
  2690. /* gadget driver must not be registered */
  2691. if (WARN_ON(dev->driver))
  2692. return;
  2693. /* dma pool cleanup */
  2694. free_dma_pools(dev);
  2695. /* reset controller */
  2696. writel(AMD_BIT(UDC_DEVCFG_SOFTRESET), &dev->regs->cfg);
  2697. free_irq(pdev->irq, dev);
  2698. iounmap(dev->virt_addr);
  2699. release_mem_region(pci_resource_start(pdev, 0),
  2700. pci_resource_len(pdev, 0));
  2701. pci_disable_device(pdev);
  2702. udc_remove(dev);
  2703. }
  2704. /* create dma pools on init */
  2705. static int init_dma_pools(struct udc *dev)
  2706. {
  2707. struct udc_stp_dma *td_stp;
  2708. struct udc_data_dma *td_data;
  2709. int retval;
  2710. /* consistent DMA mode setting ? */
  2711. if (use_dma_ppb) {
  2712. use_dma_bufferfill_mode = 0;
  2713. } else {
  2714. use_dma_ppb_du = 0;
  2715. use_dma_bufferfill_mode = 1;
  2716. }
  2717. /* DMA setup */
  2718. dev->data_requests = dma_pool_create("data_requests", NULL,
  2719. sizeof(struct udc_data_dma), 0, 0);
  2720. if (!dev->data_requests) {
  2721. DBG(dev, "can't get request data pool\n");
  2722. return -ENOMEM;
  2723. }
  2724. /* EP0 in dma regs = dev control regs */
  2725. dev->ep[UDC_EP0IN_IX].dma = &dev->regs->ctl;
  2726. /* dma desc for setup data */
  2727. dev->stp_requests = dma_pool_create("setup requests", NULL,
  2728. sizeof(struct udc_stp_dma), 0, 0);
  2729. if (!dev->stp_requests) {
  2730. DBG(dev, "can't get stp request pool\n");
  2731. retval = -ENOMEM;
  2732. goto err_create_dma_pool;
  2733. }
  2734. /* setup */
  2735. td_stp = dma_pool_alloc(dev->stp_requests, GFP_KERNEL,
  2736. &dev->ep[UDC_EP0OUT_IX].td_stp_dma);
  2737. if (!td_stp) {
  2738. retval = -ENOMEM;
  2739. goto err_alloc_dma;
  2740. }
  2741. dev->ep[UDC_EP0OUT_IX].td_stp = td_stp;
  2742. /* data: 0 packets !? */
  2743. td_data = dma_pool_alloc(dev->stp_requests, GFP_KERNEL,
  2744. &dev->ep[UDC_EP0OUT_IX].td_phys);
  2745. if (!td_data) {
  2746. retval = -ENOMEM;
  2747. goto err_alloc_phys;
  2748. }
  2749. dev->ep[UDC_EP0OUT_IX].td = td_data;
  2750. return 0;
  2751. err_alloc_phys:
  2752. dma_pool_free(dev->stp_requests, dev->ep[UDC_EP0OUT_IX].td_stp,
  2753. dev->ep[UDC_EP0OUT_IX].td_stp_dma);
  2754. err_alloc_dma:
  2755. dma_pool_destroy(dev->stp_requests);
  2756. dev->stp_requests = NULL;
  2757. err_create_dma_pool:
  2758. dma_pool_destroy(dev->data_requests);
  2759. dev->data_requests = NULL;
  2760. return retval;
  2761. }
  2762. /* general probe */
  2763. static int udc_probe(struct udc *dev)
  2764. {
  2765. char tmp[128];
  2766. u32 reg;
  2767. int retval;
  2768. /* mark timer as not initialized */
  2769. udc_timer.data = 0;
  2770. udc_pollstall_timer.data = 0;
  2771. /* device struct setup */
  2772. dev->gadget.ops = &udc_ops;
  2773. dev_set_name(&dev->gadget.dev, "gadget");
  2774. dev->gadget.name = name;
  2775. dev->gadget.max_speed = USB_SPEED_HIGH;
  2776. /* init registers, interrupts, ... */
  2777. startup_registers(dev);
  2778. dev_info(&dev->pdev->dev, "%s\n", mod_desc);
  2779. snprintf(tmp, sizeof(tmp), "%d", dev->irq);
  2780. dev_info(&dev->pdev->dev,
  2781. "irq %s, pci mem %08lx, chip rev %02x(Geode5536 %s)\n",
  2782. tmp, dev->phys_addr, dev->chiprev,
  2783. (dev->chiprev == UDC_HSA0_REV) ? "A0" : "B1");
  2784. strcpy(tmp, UDC_DRIVER_VERSION_STRING);
  2785. if (dev->chiprev == UDC_HSA0_REV) {
  2786. dev_err(&dev->pdev->dev, "chip revision is A0; too old\n");
  2787. retval = -ENODEV;
  2788. goto finished;
  2789. }
  2790. dev_info(&dev->pdev->dev,
  2791. "driver version: %s(for Geode5536 B1)\n", tmp);
  2792. udc = dev;
  2793. retval = usb_add_gadget_udc_release(&udc->pdev->dev, &dev->gadget,
  2794. gadget_release);
  2795. if (retval)
  2796. goto finished;
  2797. /* timer init */
  2798. init_timer(&udc_timer);
  2799. udc_timer.function = udc_timer_function;
  2800. udc_timer.data = 1;
  2801. /* timer pollstall init */
  2802. init_timer(&udc_pollstall_timer);
  2803. udc_pollstall_timer.function = udc_pollstall_timer_function;
  2804. udc_pollstall_timer.data = 1;
  2805. /* set SD */
  2806. reg = readl(&dev->regs->ctl);
  2807. reg |= AMD_BIT(UDC_DEVCTL_SD);
  2808. writel(reg, &dev->regs->ctl);
  2809. /* print dev register info */
  2810. print_regs(dev);
  2811. return 0;
  2812. finished:
  2813. return retval;
  2814. }
  2815. /* Called by pci bus driver to init pci context */
  2816. static int udc_pci_probe(
  2817. struct pci_dev *pdev,
  2818. const struct pci_device_id *id
  2819. )
  2820. {
  2821. struct udc *dev;
  2822. unsigned long resource;
  2823. unsigned long len;
  2824. int retval = 0;
  2825. /* one udc only */
  2826. if (udc) {
  2827. dev_dbg(&pdev->dev, "already probed\n");
  2828. return -EBUSY;
  2829. }
  2830. /* init */
  2831. dev = kzalloc(sizeof(struct udc), GFP_KERNEL);
  2832. if (!dev)
  2833. return -ENOMEM;
  2834. /* pci setup */
  2835. if (pci_enable_device(pdev) < 0) {
  2836. retval = -ENODEV;
  2837. goto err_pcidev;
  2838. }
  2839. /* PCI resource allocation */
  2840. resource = pci_resource_start(pdev, 0);
  2841. len = pci_resource_len(pdev, 0);
  2842. if (!request_mem_region(resource, len, name)) {
  2843. dev_dbg(&pdev->dev, "pci device used already\n");
  2844. retval = -EBUSY;
  2845. goto err_memreg;
  2846. }
  2847. dev->virt_addr = ioremap_nocache(resource, len);
  2848. if (!dev->virt_addr) {
  2849. dev_dbg(&pdev->dev, "start address cannot be mapped\n");
  2850. retval = -EFAULT;
  2851. goto err_ioremap;
  2852. }
  2853. if (!pdev->irq) {
  2854. dev_err(&pdev->dev, "irq not set\n");
  2855. retval = -ENODEV;
  2856. goto err_irq;
  2857. }
  2858. spin_lock_init(&dev->lock);
  2859. /* udc csr registers base */
  2860. dev->csr = dev->virt_addr + UDC_CSR_ADDR;
  2861. /* dev registers base */
  2862. dev->regs = dev->virt_addr + UDC_DEVCFG_ADDR;
  2863. /* ep registers base */
  2864. dev->ep_regs = dev->virt_addr + UDC_EPREGS_ADDR;
  2865. /* fifo's base */
  2866. dev->rxfifo = (u32 __iomem *)(dev->virt_addr + UDC_RXFIFO_ADDR);
  2867. dev->txfifo = (u32 __iomem *)(dev->virt_addr + UDC_TXFIFO_ADDR);
  2868. if (request_irq(pdev->irq, udc_irq, IRQF_SHARED, name, dev) != 0) {
  2869. dev_dbg(&pdev->dev, "request_irq(%d) fail\n", pdev->irq);
  2870. retval = -EBUSY;
  2871. goto err_irq;
  2872. }
  2873. pci_set_drvdata(pdev, dev);
  2874. /* chip revision for Hs AMD5536 */
  2875. dev->chiprev = pdev->revision;
  2876. pci_set_master(pdev);
  2877. pci_try_set_mwi(pdev);
  2878. /* init dma pools */
  2879. if (use_dma) {
  2880. retval = init_dma_pools(dev);
  2881. if (retval != 0)
  2882. goto err_dma;
  2883. }
  2884. dev->phys_addr = resource;
  2885. dev->irq = pdev->irq;
  2886. dev->pdev = pdev;
  2887. /* general probing */
  2888. if (udc_probe(dev)) {
  2889. retval = -ENODEV;
  2890. goto err_probe;
  2891. }
  2892. return 0;
  2893. err_probe:
  2894. if (use_dma)
  2895. free_dma_pools(dev);
  2896. err_dma:
  2897. free_irq(pdev->irq, dev);
  2898. err_irq:
  2899. iounmap(dev->virt_addr);
  2900. err_ioremap:
  2901. release_mem_region(resource, len);
  2902. err_memreg:
  2903. pci_disable_device(pdev);
  2904. err_pcidev:
  2905. kfree(dev);
  2906. return retval;
  2907. }
  2908. /* PCI device parameters */
  2909. static const struct pci_device_id pci_id[] = {
  2910. {
  2911. PCI_DEVICE(PCI_VENDOR_ID_AMD, 0x2096),
  2912. .class = PCI_CLASS_SERIAL_USB_DEVICE,
  2913. .class_mask = 0xffffffff,
  2914. },
  2915. {},
  2916. };
  2917. MODULE_DEVICE_TABLE(pci, pci_id);
  2918. /* PCI functions */
  2919. static struct pci_driver udc_pci_driver = {
  2920. .name = (char *) name,
  2921. .id_table = pci_id,
  2922. .probe = udc_pci_probe,
  2923. .remove = udc_pci_remove,
  2924. };
  2925. module_pci_driver(udc_pci_driver);
  2926. MODULE_DESCRIPTION(UDC_MOD_DESCRIPTION);
  2927. MODULE_AUTHOR("Thomas Dahlmann");
  2928. MODULE_LICENSE("GPL");