s3c-hsotg.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720
  1. /**
  2. * linux/drivers/usb/gadget/s3c-hsotg.c
  3. *
  4. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com
  6. *
  7. * Copyright 2008 Openmoko, Inc.
  8. * Copyright 2008 Simtec Electronics
  9. * Ben Dooks <ben@simtec.co.uk>
  10. * http://armlinux.simtec.co.uk/
  11. *
  12. * S3C USB2.0 High-speed / OtG driver
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/delay.h>
  27. #include <linux/io.h>
  28. #include <linux/slab.h>
  29. #include <linux/clk.h>
  30. #include <linux/regulator/consumer.h>
  31. #include <linux/usb/ch9.h>
  32. #include <linux/usb/gadget.h>
  33. #include <linux/platform_data/s3c-hsotg.h>
  34. #include <mach/map.h>
  35. #include "s3c-hsotg.h"
  36. #define DMA_ADDR_INVALID (~((dma_addr_t)0))
  37. static const char * const s3c_hsotg_supply_names[] = {
  38. "vusb_d", /* digital USB supply, 1.2V */
  39. "vusb_a", /* analog USB supply, 1.1V */
  40. };
  41. /*
  42. * EP0_MPS_LIMIT
  43. *
  44. * Unfortunately there seems to be a limit of the amount of data that can
  45. * be transferred by IN transactions on EP0. This is either 127 bytes or 3
  46. * packets (which practically means 1 packet and 63 bytes of data) when the
  47. * MPS is set to 64.
  48. *
  49. * This means if we are wanting to move >127 bytes of data, we need to
  50. * split the transactions up, but just doing one packet at a time does
  51. * not work (this may be an implicit DATA0 PID on first packet of the
  52. * transaction) and doing 2 packets is outside the controller's limits.
  53. *
  54. * If we try to lower the MPS size for EP0, then no transfers work properly
  55. * for EP0, and the system will fail basic enumeration. As no cause for this
  56. * has currently been found, we cannot support any large IN transfers for
  57. * EP0.
  58. */
  59. #define EP0_MPS_LIMIT 64
  60. struct s3c_hsotg;
  61. struct s3c_hsotg_req;
  62. /**
  63. * struct s3c_hsotg_ep - driver endpoint definition.
  64. * @ep: The gadget layer representation of the endpoint.
  65. * @name: The driver generated name for the endpoint.
  66. * @queue: Queue of requests for this endpoint.
  67. * @parent: Reference back to the parent device structure.
  68. * @req: The current request that the endpoint is processing. This is
  69. * used to indicate an request has been loaded onto the endpoint
  70. * and has yet to be completed (maybe due to data move, or simply
  71. * awaiting an ack from the core all the data has been completed).
  72. * @debugfs: File entry for debugfs file for this endpoint.
  73. * @lock: State lock to protect contents of endpoint.
  74. * @dir_in: Set to true if this endpoint is of the IN direction, which
  75. * means that it is sending data to the Host.
  76. * @index: The index for the endpoint registers.
  77. * @name: The name array passed to the USB core.
  78. * @halted: Set if the endpoint has been halted.
  79. * @periodic: Set if this is a periodic ep, such as Interrupt
  80. * @sent_zlp: Set if we've sent a zero-length packet.
  81. * @total_data: The total number of data bytes done.
  82. * @fifo_size: The size of the FIFO (for periodic IN endpoints)
  83. * @fifo_load: The amount of data loaded into the FIFO (periodic IN)
  84. * @last_load: The offset of data for the last start of request.
  85. * @size_loaded: The last loaded size for DxEPTSIZE for periodic IN
  86. *
  87. * This is the driver's state for each registered enpoint, allowing it
  88. * to keep track of transactions that need doing. Each endpoint has a
  89. * lock to protect the state, to try and avoid using an overall lock
  90. * for the host controller as much as possible.
  91. *
  92. * For periodic IN endpoints, we have fifo_size and fifo_load to try
  93. * and keep track of the amount of data in the periodic FIFO for each
  94. * of these as we don't have a status register that tells us how much
  95. * is in each of them. (note, this may actually be useless information
  96. * as in shared-fifo mode periodic in acts like a single-frame packet
  97. * buffer than a fifo)
  98. */
  99. struct s3c_hsotg_ep {
  100. struct usb_ep ep;
  101. struct list_head queue;
  102. struct s3c_hsotg *parent;
  103. struct s3c_hsotg_req *req;
  104. struct dentry *debugfs;
  105. unsigned long total_data;
  106. unsigned int size_loaded;
  107. unsigned int last_load;
  108. unsigned int fifo_load;
  109. unsigned short fifo_size;
  110. unsigned char dir_in;
  111. unsigned char index;
  112. unsigned int halted:1;
  113. unsigned int periodic:1;
  114. unsigned int sent_zlp:1;
  115. char name[10];
  116. };
  117. /**
  118. * struct s3c_hsotg - driver state.
  119. * @dev: The parent device supplied to the probe function
  120. * @driver: USB gadget driver
  121. * @plat: The platform specific configuration data.
  122. * @regs: The memory area mapped for accessing registers.
  123. * @irq: The IRQ number we are using
  124. * @supplies: Definition of USB power supplies
  125. * @dedicated_fifos: Set if the hardware has dedicated IN-EP fifos.
  126. * @num_of_eps: Number of available EPs (excluding EP0)
  127. * @debug_root: root directrory for debugfs.
  128. * @debug_file: main status file for debugfs.
  129. * @debug_fifo: FIFO status file for debugfs.
  130. * @ep0_reply: Request used for ep0 reply.
  131. * @ep0_buff: Buffer for EP0 reply data, if needed.
  132. * @ctrl_buff: Buffer for EP0 control requests.
  133. * @ctrl_req: Request for EP0 control packets.
  134. * @setup: NAK management for EP0 SETUP
  135. * @last_rst: Time of last reset
  136. * @eps: The endpoints being supplied to the gadget framework
  137. */
  138. struct s3c_hsotg {
  139. struct device *dev;
  140. struct usb_gadget_driver *driver;
  141. struct s3c_hsotg_plat *plat;
  142. spinlock_t lock;
  143. void __iomem *regs;
  144. int irq;
  145. struct clk *clk;
  146. struct regulator_bulk_data supplies[ARRAY_SIZE(s3c_hsotg_supply_names)];
  147. unsigned int dedicated_fifos:1;
  148. unsigned char num_of_eps;
  149. struct dentry *debug_root;
  150. struct dentry *debug_file;
  151. struct dentry *debug_fifo;
  152. struct usb_request *ep0_reply;
  153. struct usb_request *ctrl_req;
  154. u8 ep0_buff[8];
  155. u8 ctrl_buff[8];
  156. struct usb_gadget gadget;
  157. unsigned int setup;
  158. unsigned long last_rst;
  159. struct s3c_hsotg_ep *eps;
  160. };
  161. /**
  162. * struct s3c_hsotg_req - data transfer request
  163. * @req: The USB gadget request
  164. * @queue: The list of requests for the endpoint this is queued for.
  165. * @in_progress: Has already had size/packets written to core
  166. * @mapped: DMA buffer for this request has been mapped via dma_map_single().
  167. */
  168. struct s3c_hsotg_req {
  169. struct usb_request req;
  170. struct list_head queue;
  171. unsigned char in_progress;
  172. unsigned char mapped;
  173. };
  174. /* conversion functions */
  175. static inline struct s3c_hsotg_req *our_req(struct usb_request *req)
  176. {
  177. return container_of(req, struct s3c_hsotg_req, req);
  178. }
  179. static inline struct s3c_hsotg_ep *our_ep(struct usb_ep *ep)
  180. {
  181. return container_of(ep, struct s3c_hsotg_ep, ep);
  182. }
  183. static inline struct s3c_hsotg *to_hsotg(struct usb_gadget *gadget)
  184. {
  185. return container_of(gadget, struct s3c_hsotg, gadget);
  186. }
  187. static inline void __orr32(void __iomem *ptr, u32 val)
  188. {
  189. writel(readl(ptr) | val, ptr);
  190. }
  191. static inline void __bic32(void __iomem *ptr, u32 val)
  192. {
  193. writel(readl(ptr) & ~val, ptr);
  194. }
  195. /* forward decleration of functions */
  196. static void s3c_hsotg_dump(struct s3c_hsotg *hsotg);
  197. /**
  198. * using_dma - return the DMA status of the driver.
  199. * @hsotg: The driver state.
  200. *
  201. * Return true if we're using DMA.
  202. *
  203. * Currently, we have the DMA support code worked into everywhere
  204. * that needs it, but the AMBA DMA implementation in the hardware can
  205. * only DMA from 32bit aligned addresses. This means that gadgets such
  206. * as the CDC Ethernet cannot work as they often pass packets which are
  207. * not 32bit aligned.
  208. *
  209. * Unfortunately the choice to use DMA or not is global to the controller
  210. * and seems to be only settable when the controller is being put through
  211. * a core reset. This means we either need to fix the gadgets to take
  212. * account of DMA alignment, or add bounce buffers (yuerk).
  213. *
  214. * Until this issue is sorted out, we always return 'false'.
  215. */
  216. static inline bool using_dma(struct s3c_hsotg *hsotg)
  217. {
  218. return false; /* support is not complete */
  219. }
  220. /**
  221. * s3c_hsotg_en_gsint - enable one or more of the general interrupt
  222. * @hsotg: The device state
  223. * @ints: A bitmask of the interrupts to enable
  224. */
  225. static void s3c_hsotg_en_gsint(struct s3c_hsotg *hsotg, u32 ints)
  226. {
  227. u32 gsintmsk = readl(hsotg->regs + GINTMSK);
  228. u32 new_gsintmsk;
  229. new_gsintmsk = gsintmsk | ints;
  230. if (new_gsintmsk != gsintmsk) {
  231. dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
  232. writel(new_gsintmsk, hsotg->regs + GINTMSK);
  233. }
  234. }
  235. /**
  236. * s3c_hsotg_disable_gsint - disable one or more of the general interrupt
  237. * @hsotg: The device state
  238. * @ints: A bitmask of the interrupts to enable
  239. */
  240. static void s3c_hsotg_disable_gsint(struct s3c_hsotg *hsotg, u32 ints)
  241. {
  242. u32 gsintmsk = readl(hsotg->regs + GINTMSK);
  243. u32 new_gsintmsk;
  244. new_gsintmsk = gsintmsk & ~ints;
  245. if (new_gsintmsk != gsintmsk)
  246. writel(new_gsintmsk, hsotg->regs + GINTMSK);
  247. }
  248. /**
  249. * s3c_hsotg_ctrl_epint - enable/disable an endpoint irq
  250. * @hsotg: The device state
  251. * @ep: The endpoint index
  252. * @dir_in: True if direction is in.
  253. * @en: The enable value, true to enable
  254. *
  255. * Set or clear the mask for an individual endpoint's interrupt
  256. * request.
  257. */
  258. static void s3c_hsotg_ctrl_epint(struct s3c_hsotg *hsotg,
  259. unsigned int ep, unsigned int dir_in,
  260. unsigned int en)
  261. {
  262. unsigned long flags;
  263. u32 bit = 1 << ep;
  264. u32 daint;
  265. if (!dir_in)
  266. bit <<= 16;
  267. local_irq_save(flags);
  268. daint = readl(hsotg->regs + DAINTMSK);
  269. if (en)
  270. daint |= bit;
  271. else
  272. daint &= ~bit;
  273. writel(daint, hsotg->regs + DAINTMSK);
  274. local_irq_restore(flags);
  275. }
  276. /**
  277. * s3c_hsotg_init_fifo - initialise non-periodic FIFOs
  278. * @hsotg: The device instance.
  279. */
  280. static void s3c_hsotg_init_fifo(struct s3c_hsotg *hsotg)
  281. {
  282. unsigned int ep;
  283. unsigned int addr;
  284. unsigned int size;
  285. int timeout;
  286. u32 val;
  287. /* set FIFO sizes to 2048/1024 */
  288. writel(2048, hsotg->regs + GRXFSIZ);
  289. writel(GNPTXFSIZ_NPTxFStAddr(2048) |
  290. GNPTXFSIZ_NPTxFDep(1024),
  291. hsotg->regs + GNPTXFSIZ);
  292. /*
  293. * arange all the rest of the TX FIFOs, as some versions of this
  294. * block have overlapping default addresses. This also ensures
  295. * that if the settings have been changed, then they are set to
  296. * known values.
  297. */
  298. /* start at the end of the GNPTXFSIZ, rounded up */
  299. addr = 2048 + 1024;
  300. size = 768;
  301. /*
  302. * currently we allocate TX FIFOs for all possible endpoints,
  303. * and assume that they are all the same size.
  304. */
  305. for (ep = 1; ep <= 15; ep++) {
  306. val = addr;
  307. val |= size << DPTXFSIZn_DPTxFSize_SHIFT;
  308. addr += size;
  309. writel(val, hsotg->regs + DPTXFSIZn(ep));
  310. }
  311. /*
  312. * according to p428 of the design guide, we need to ensure that
  313. * all fifos are flushed before continuing
  314. */
  315. writel(GRSTCTL_TxFNum(0x10) | GRSTCTL_TxFFlsh |
  316. GRSTCTL_RxFFlsh, hsotg->regs + GRSTCTL);
  317. /* wait until the fifos are both flushed */
  318. timeout = 100;
  319. while (1) {
  320. val = readl(hsotg->regs + GRSTCTL);
  321. if ((val & (GRSTCTL_TxFFlsh | GRSTCTL_RxFFlsh)) == 0)
  322. break;
  323. if (--timeout == 0) {
  324. dev_err(hsotg->dev,
  325. "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
  326. __func__, val);
  327. }
  328. udelay(1);
  329. }
  330. dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
  331. }
  332. /**
  333. * @ep: USB endpoint to allocate request for.
  334. * @flags: Allocation flags
  335. *
  336. * Allocate a new USB request structure appropriate for the specified endpoint
  337. */
  338. static struct usb_request *s3c_hsotg_ep_alloc_request(struct usb_ep *ep,
  339. gfp_t flags)
  340. {
  341. struct s3c_hsotg_req *req;
  342. req = kzalloc(sizeof(struct s3c_hsotg_req), flags);
  343. if (!req)
  344. return NULL;
  345. INIT_LIST_HEAD(&req->queue);
  346. req->req.dma = DMA_ADDR_INVALID;
  347. return &req->req;
  348. }
  349. /**
  350. * is_ep_periodic - return true if the endpoint is in periodic mode.
  351. * @hs_ep: The endpoint to query.
  352. *
  353. * Returns true if the endpoint is in periodic mode, meaning it is being
  354. * used for an Interrupt or ISO transfer.
  355. */
  356. static inline int is_ep_periodic(struct s3c_hsotg_ep *hs_ep)
  357. {
  358. return hs_ep->periodic;
  359. }
  360. /**
  361. * s3c_hsotg_unmap_dma - unmap the DMA memory being used for the request
  362. * @hsotg: The device state.
  363. * @hs_ep: The endpoint for the request
  364. * @hs_req: The request being processed.
  365. *
  366. * This is the reverse of s3c_hsotg_map_dma(), called for the completion
  367. * of a request to ensure the buffer is ready for access by the caller.
  368. */
  369. static void s3c_hsotg_unmap_dma(struct s3c_hsotg *hsotg,
  370. struct s3c_hsotg_ep *hs_ep,
  371. struct s3c_hsotg_req *hs_req)
  372. {
  373. struct usb_request *req = &hs_req->req;
  374. enum dma_data_direction dir;
  375. dir = hs_ep->dir_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
  376. /* ignore this if we're not moving any data */
  377. if (hs_req->req.length == 0)
  378. return;
  379. if (hs_req->mapped) {
  380. /* we mapped this, so unmap and remove the dma */
  381. dma_unmap_single(hsotg->dev, req->dma, req->length, dir);
  382. req->dma = DMA_ADDR_INVALID;
  383. hs_req->mapped = 0;
  384. } else {
  385. dma_sync_single_for_cpu(hsotg->dev, req->dma, req->length, dir);
  386. }
  387. }
  388. /**
  389. * s3c_hsotg_write_fifo - write packet Data to the TxFIFO
  390. * @hsotg: The controller state.
  391. * @hs_ep: The endpoint we're going to write for.
  392. * @hs_req: The request to write data for.
  393. *
  394. * This is called when the TxFIFO has some space in it to hold a new
  395. * transmission and we have something to give it. The actual setup of
  396. * the data size is done elsewhere, so all we have to do is to actually
  397. * write the data.
  398. *
  399. * The return value is zero if there is more space (or nothing was done)
  400. * otherwise -ENOSPC is returned if the FIFO space was used up.
  401. *
  402. * This routine is only needed for PIO
  403. */
  404. static int s3c_hsotg_write_fifo(struct s3c_hsotg *hsotg,
  405. struct s3c_hsotg_ep *hs_ep,
  406. struct s3c_hsotg_req *hs_req)
  407. {
  408. bool periodic = is_ep_periodic(hs_ep);
  409. u32 gnptxsts = readl(hsotg->regs + GNPTXSTS);
  410. int buf_pos = hs_req->req.actual;
  411. int to_write = hs_ep->size_loaded;
  412. void *data;
  413. int can_write;
  414. int pkt_round;
  415. to_write -= (buf_pos - hs_ep->last_load);
  416. /* if there's nothing to write, get out early */
  417. if (to_write == 0)
  418. return 0;
  419. if (periodic && !hsotg->dedicated_fifos) {
  420. u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
  421. int size_left;
  422. int size_done;
  423. /*
  424. * work out how much data was loaded so we can calculate
  425. * how much data is left in the fifo.
  426. */
  427. size_left = DxEPTSIZ_XferSize_GET(epsize);
  428. /*
  429. * if shared fifo, we cannot write anything until the
  430. * previous data has been completely sent.
  431. */
  432. if (hs_ep->fifo_load != 0) {
  433. s3c_hsotg_en_gsint(hsotg, GINTSTS_PTxFEmp);
  434. return -ENOSPC;
  435. }
  436. dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
  437. __func__, size_left,
  438. hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
  439. /* how much of the data has moved */
  440. size_done = hs_ep->size_loaded - size_left;
  441. /* how much data is left in the fifo */
  442. can_write = hs_ep->fifo_load - size_done;
  443. dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
  444. __func__, can_write);
  445. can_write = hs_ep->fifo_size - can_write;
  446. dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
  447. __func__, can_write);
  448. if (can_write <= 0) {
  449. s3c_hsotg_en_gsint(hsotg, GINTSTS_PTxFEmp);
  450. return -ENOSPC;
  451. }
  452. } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
  453. can_write = readl(hsotg->regs + DTXFSTS(hs_ep->index));
  454. can_write &= 0xffff;
  455. can_write *= 4;
  456. } else {
  457. if (GNPTXSTS_NPTxQSpcAvail_GET(gnptxsts) == 0) {
  458. dev_dbg(hsotg->dev,
  459. "%s: no queue slots available (0x%08x)\n",
  460. __func__, gnptxsts);
  461. s3c_hsotg_en_gsint(hsotg, GINTSTS_NPTxFEmp);
  462. return -ENOSPC;
  463. }
  464. can_write = GNPTXSTS_NPTxFSpcAvail_GET(gnptxsts);
  465. can_write *= 4; /* fifo size is in 32bit quantities. */
  466. }
  467. dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, mps %d\n",
  468. __func__, gnptxsts, can_write, to_write, hs_ep->ep.maxpacket);
  469. /*
  470. * limit to 512 bytes of data, it seems at least on the non-periodic
  471. * FIFO, requests of >512 cause the endpoint to get stuck with a
  472. * fragment of the end of the transfer in it.
  473. */
  474. if (can_write > 512)
  475. can_write = 512;
  476. /*
  477. * limit the write to one max-packet size worth of data, but allow
  478. * the transfer to return that it did not run out of fifo space
  479. * doing it.
  480. */
  481. if (to_write > hs_ep->ep.maxpacket) {
  482. to_write = hs_ep->ep.maxpacket;
  483. s3c_hsotg_en_gsint(hsotg,
  484. periodic ? GINTSTS_PTxFEmp :
  485. GINTSTS_NPTxFEmp);
  486. }
  487. /* see if we can write data */
  488. if (to_write > can_write) {
  489. to_write = can_write;
  490. pkt_round = to_write % hs_ep->ep.maxpacket;
  491. /*
  492. * Round the write down to an
  493. * exact number of packets.
  494. *
  495. * Note, we do not currently check to see if we can ever
  496. * write a full packet or not to the FIFO.
  497. */
  498. if (pkt_round)
  499. to_write -= pkt_round;
  500. /*
  501. * enable correct FIFO interrupt to alert us when there
  502. * is more room left.
  503. */
  504. s3c_hsotg_en_gsint(hsotg,
  505. periodic ? GINTSTS_PTxFEmp :
  506. GINTSTS_NPTxFEmp);
  507. }
  508. dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
  509. to_write, hs_req->req.length, can_write, buf_pos);
  510. if (to_write <= 0)
  511. return -ENOSPC;
  512. hs_req->req.actual = buf_pos + to_write;
  513. hs_ep->total_data += to_write;
  514. if (periodic)
  515. hs_ep->fifo_load += to_write;
  516. to_write = DIV_ROUND_UP(to_write, 4);
  517. data = hs_req->req.buf + buf_pos;
  518. writesl(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
  519. return (to_write >= can_write) ? -ENOSPC : 0;
  520. }
  521. /**
  522. * get_ep_limit - get the maximum data legnth for this endpoint
  523. * @hs_ep: The endpoint
  524. *
  525. * Return the maximum data that can be queued in one go on a given endpoint
  526. * so that transfers that are too long can be split.
  527. */
  528. static unsigned get_ep_limit(struct s3c_hsotg_ep *hs_ep)
  529. {
  530. int index = hs_ep->index;
  531. unsigned maxsize;
  532. unsigned maxpkt;
  533. if (index != 0) {
  534. maxsize = DxEPTSIZ_XferSize_LIMIT + 1;
  535. maxpkt = DxEPTSIZ_PktCnt_LIMIT + 1;
  536. } else {
  537. maxsize = 64+64;
  538. if (hs_ep->dir_in)
  539. maxpkt = DIEPTSIZ0_PktCnt_LIMIT + 1;
  540. else
  541. maxpkt = 2;
  542. }
  543. /* we made the constant loading easier above by using +1 */
  544. maxpkt--;
  545. maxsize--;
  546. /*
  547. * constrain by packet count if maxpkts*pktsize is greater
  548. * than the length register size.
  549. */
  550. if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
  551. maxsize = maxpkt * hs_ep->ep.maxpacket;
  552. return maxsize;
  553. }
  554. /**
  555. * s3c_hsotg_start_req - start a USB request from an endpoint's queue
  556. * @hsotg: The controller state.
  557. * @hs_ep: The endpoint to process a request for
  558. * @hs_req: The request to start.
  559. * @continuing: True if we are doing more for the current request.
  560. *
  561. * Start the given request running by setting the endpoint registers
  562. * appropriately, and writing any data to the FIFOs.
  563. */
  564. static void s3c_hsotg_start_req(struct s3c_hsotg *hsotg,
  565. struct s3c_hsotg_ep *hs_ep,
  566. struct s3c_hsotg_req *hs_req,
  567. bool continuing)
  568. {
  569. struct usb_request *ureq = &hs_req->req;
  570. int index = hs_ep->index;
  571. int dir_in = hs_ep->dir_in;
  572. u32 epctrl_reg;
  573. u32 epsize_reg;
  574. u32 epsize;
  575. u32 ctrl;
  576. unsigned length;
  577. unsigned packets;
  578. unsigned maxreq;
  579. if (index != 0) {
  580. if (hs_ep->req && !continuing) {
  581. dev_err(hsotg->dev, "%s: active request\n", __func__);
  582. WARN_ON(1);
  583. return;
  584. } else if (hs_ep->req != hs_req && continuing) {
  585. dev_err(hsotg->dev,
  586. "%s: continue different req\n", __func__);
  587. WARN_ON(1);
  588. return;
  589. }
  590. }
  591. epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
  592. epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
  593. dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
  594. __func__, readl(hsotg->regs + epctrl_reg), index,
  595. hs_ep->dir_in ? "in" : "out");
  596. /* If endpoint is stalled, we will restart request later */
  597. ctrl = readl(hsotg->regs + epctrl_reg);
  598. if (ctrl & DxEPCTL_Stall) {
  599. dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
  600. return;
  601. }
  602. length = ureq->length - ureq->actual;
  603. dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
  604. ureq->length, ureq->actual);
  605. if (0)
  606. dev_dbg(hsotg->dev,
  607. "REQ buf %p len %d dma 0x%08x noi=%d zp=%d snok=%d\n",
  608. ureq->buf, length, ureq->dma,
  609. ureq->no_interrupt, ureq->zero, ureq->short_not_ok);
  610. maxreq = get_ep_limit(hs_ep);
  611. if (length > maxreq) {
  612. int round = maxreq % hs_ep->ep.maxpacket;
  613. dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
  614. __func__, length, maxreq, round);
  615. /* round down to multiple of packets */
  616. if (round)
  617. maxreq -= round;
  618. length = maxreq;
  619. }
  620. if (length)
  621. packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
  622. else
  623. packets = 1; /* send one packet if length is zero. */
  624. if (dir_in && index != 0)
  625. epsize = DxEPTSIZ_MC(1);
  626. else
  627. epsize = 0;
  628. if (index != 0 && ureq->zero) {
  629. /*
  630. * test for the packets being exactly right for the
  631. * transfer
  632. */
  633. if (length == (packets * hs_ep->ep.maxpacket))
  634. packets++;
  635. }
  636. epsize |= DxEPTSIZ_PktCnt(packets);
  637. epsize |= DxEPTSIZ_XferSize(length);
  638. dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
  639. __func__, packets, length, ureq->length, epsize, epsize_reg);
  640. /* store the request as the current one we're doing */
  641. hs_ep->req = hs_req;
  642. /* write size / packets */
  643. writel(epsize, hsotg->regs + epsize_reg);
  644. if (using_dma(hsotg) && !continuing) {
  645. unsigned int dma_reg;
  646. /*
  647. * write DMA address to control register, buffer already
  648. * synced by s3c_hsotg_ep_queue().
  649. */
  650. dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
  651. writel(ureq->dma, hsotg->regs + dma_reg);
  652. dev_dbg(hsotg->dev, "%s: 0x%08x => 0x%08x\n",
  653. __func__, ureq->dma, dma_reg);
  654. }
  655. ctrl |= DxEPCTL_EPEna; /* ensure ep enabled */
  656. ctrl |= DxEPCTL_USBActEp;
  657. dev_dbg(hsotg->dev, "setup req:%d\n", hsotg->setup);
  658. /* For Setup request do not clear NAK */
  659. if (hsotg->setup && index == 0)
  660. hsotg->setup = 0;
  661. else
  662. ctrl |= DxEPCTL_CNAK; /* clear NAK set by core */
  663. dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
  664. writel(ctrl, hsotg->regs + epctrl_reg);
  665. /*
  666. * set these, it seems that DMA support increments past the end
  667. * of the packet buffer so we need to calculate the length from
  668. * this information.
  669. */
  670. hs_ep->size_loaded = length;
  671. hs_ep->last_load = ureq->actual;
  672. if (dir_in && !using_dma(hsotg)) {
  673. /* set these anyway, we may need them for non-periodic in */
  674. hs_ep->fifo_load = 0;
  675. s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
  676. }
  677. /*
  678. * clear the INTknTXFEmpMsk when we start request, more as a aide
  679. * to debugging to see what is going on.
  680. */
  681. if (dir_in)
  682. writel(DIEPMSK_INTknTXFEmpMsk,
  683. hsotg->regs + DIEPINT(index));
  684. /*
  685. * Note, trying to clear the NAK here causes problems with transmit
  686. * on the S3C6400 ending up with the TXFIFO becoming full.
  687. */
  688. /* check ep is enabled */
  689. if (!(readl(hsotg->regs + epctrl_reg) & DxEPCTL_EPEna))
  690. dev_warn(hsotg->dev,
  691. "ep%d: failed to become enabled (DxEPCTL=0x%08x)?\n",
  692. index, readl(hsotg->regs + epctrl_reg));
  693. dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n",
  694. __func__, readl(hsotg->regs + epctrl_reg));
  695. }
  696. /**
  697. * s3c_hsotg_map_dma - map the DMA memory being used for the request
  698. * @hsotg: The device state.
  699. * @hs_ep: The endpoint the request is on.
  700. * @req: The request being processed.
  701. *
  702. * We've been asked to queue a request, so ensure that the memory buffer
  703. * is correctly setup for DMA. If we've been passed an extant DMA address
  704. * then ensure the buffer has been synced to memory. If our buffer has no
  705. * DMA memory, then we map the memory and mark our request to allow us to
  706. * cleanup on completion.
  707. */
  708. static int s3c_hsotg_map_dma(struct s3c_hsotg *hsotg,
  709. struct s3c_hsotg_ep *hs_ep,
  710. struct usb_request *req)
  711. {
  712. enum dma_data_direction dir;
  713. struct s3c_hsotg_req *hs_req = our_req(req);
  714. dir = hs_ep->dir_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
  715. /* if the length is zero, ignore the DMA data */
  716. if (hs_req->req.length == 0)
  717. return 0;
  718. if (req->dma == DMA_ADDR_INVALID) {
  719. dma_addr_t dma;
  720. dma = dma_map_single(hsotg->dev, req->buf, req->length, dir);
  721. if (unlikely(dma_mapping_error(hsotg->dev, dma)))
  722. goto dma_error;
  723. if (dma & 3) {
  724. dev_err(hsotg->dev, "%s: unaligned dma buffer\n",
  725. __func__);
  726. dma_unmap_single(hsotg->dev, dma, req->length, dir);
  727. return -EINVAL;
  728. }
  729. hs_req->mapped = 1;
  730. req->dma = dma;
  731. } else {
  732. dma_sync_single_for_cpu(hsotg->dev, req->dma, req->length, dir);
  733. hs_req->mapped = 0;
  734. }
  735. return 0;
  736. dma_error:
  737. dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
  738. __func__, req->buf, req->length);
  739. return -EIO;
  740. }
  741. static int s3c_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
  742. gfp_t gfp_flags)
  743. {
  744. struct s3c_hsotg_req *hs_req = our_req(req);
  745. struct s3c_hsotg_ep *hs_ep = our_ep(ep);
  746. struct s3c_hsotg *hs = hs_ep->parent;
  747. bool first;
  748. dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
  749. ep->name, req, req->length, req->buf, req->no_interrupt,
  750. req->zero, req->short_not_ok);
  751. /* initialise status of the request */
  752. INIT_LIST_HEAD(&hs_req->queue);
  753. req->actual = 0;
  754. req->status = -EINPROGRESS;
  755. /* if we're using DMA, sync the buffers as necessary */
  756. if (using_dma(hs)) {
  757. int ret = s3c_hsotg_map_dma(hs, hs_ep, req);
  758. if (ret)
  759. return ret;
  760. }
  761. first = list_empty(&hs_ep->queue);
  762. list_add_tail(&hs_req->queue, &hs_ep->queue);
  763. if (first)
  764. s3c_hsotg_start_req(hs, hs_ep, hs_req, false);
  765. return 0;
  766. }
  767. static int s3c_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
  768. gfp_t gfp_flags)
  769. {
  770. struct s3c_hsotg_ep *hs_ep = our_ep(ep);
  771. struct s3c_hsotg *hs = hs_ep->parent;
  772. unsigned long flags = 0;
  773. int ret = 0;
  774. spin_lock_irqsave(&hs->lock, flags);
  775. ret = s3c_hsotg_ep_queue(ep, req, gfp_flags);
  776. spin_unlock_irqrestore(&hs->lock, flags);
  777. return ret;
  778. }
  779. static void s3c_hsotg_ep_free_request(struct usb_ep *ep,
  780. struct usb_request *req)
  781. {
  782. struct s3c_hsotg_req *hs_req = our_req(req);
  783. kfree(hs_req);
  784. }
  785. /**
  786. * s3c_hsotg_complete_oursetup - setup completion callback
  787. * @ep: The endpoint the request was on.
  788. * @req: The request completed.
  789. *
  790. * Called on completion of any requests the driver itself
  791. * submitted that need cleaning up.
  792. */
  793. static void s3c_hsotg_complete_oursetup(struct usb_ep *ep,
  794. struct usb_request *req)
  795. {
  796. struct s3c_hsotg_ep *hs_ep = our_ep(ep);
  797. struct s3c_hsotg *hsotg = hs_ep->parent;
  798. dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
  799. s3c_hsotg_ep_free_request(ep, req);
  800. }
  801. /**
  802. * ep_from_windex - convert control wIndex value to endpoint
  803. * @hsotg: The driver state.
  804. * @windex: The control request wIndex field (in host order).
  805. *
  806. * Convert the given wIndex into a pointer to an driver endpoint
  807. * structure, or return NULL if it is not a valid endpoint.
  808. */
  809. static struct s3c_hsotg_ep *ep_from_windex(struct s3c_hsotg *hsotg,
  810. u32 windex)
  811. {
  812. struct s3c_hsotg_ep *ep = &hsotg->eps[windex & 0x7F];
  813. int dir = (windex & USB_DIR_IN) ? 1 : 0;
  814. int idx = windex & 0x7F;
  815. if (windex >= 0x100)
  816. return NULL;
  817. if (idx > hsotg->num_of_eps)
  818. return NULL;
  819. if (idx && ep->dir_in != dir)
  820. return NULL;
  821. return ep;
  822. }
  823. /**
  824. * s3c_hsotg_send_reply - send reply to control request
  825. * @hsotg: The device state
  826. * @ep: Endpoint 0
  827. * @buff: Buffer for request
  828. * @length: Length of reply.
  829. *
  830. * Create a request and queue it on the given endpoint. This is useful as
  831. * an internal method of sending replies to certain control requests, etc.
  832. */
  833. static int s3c_hsotg_send_reply(struct s3c_hsotg *hsotg,
  834. struct s3c_hsotg_ep *ep,
  835. void *buff,
  836. int length)
  837. {
  838. struct usb_request *req;
  839. int ret;
  840. dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
  841. req = s3c_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
  842. hsotg->ep0_reply = req;
  843. if (!req) {
  844. dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
  845. return -ENOMEM;
  846. }
  847. req->buf = hsotg->ep0_buff;
  848. req->length = length;
  849. req->zero = 1; /* always do zero-length final transfer */
  850. req->complete = s3c_hsotg_complete_oursetup;
  851. if (length)
  852. memcpy(req->buf, buff, length);
  853. else
  854. ep->sent_zlp = 1;
  855. ret = s3c_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
  856. if (ret) {
  857. dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
  858. return ret;
  859. }
  860. return 0;
  861. }
  862. /**
  863. * s3c_hsotg_process_req_status - process request GET_STATUS
  864. * @hsotg: The device state
  865. * @ctrl: USB control request
  866. */
  867. static int s3c_hsotg_process_req_status(struct s3c_hsotg *hsotg,
  868. struct usb_ctrlrequest *ctrl)
  869. {
  870. struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
  871. struct s3c_hsotg_ep *ep;
  872. __le16 reply;
  873. int ret;
  874. dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
  875. if (!ep0->dir_in) {
  876. dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
  877. return -EINVAL;
  878. }
  879. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  880. case USB_RECIP_DEVICE:
  881. reply = cpu_to_le16(0); /* bit 0 => self powered,
  882. * bit 1 => remote wakeup */
  883. break;
  884. case USB_RECIP_INTERFACE:
  885. /* currently, the data result should be zero */
  886. reply = cpu_to_le16(0);
  887. break;
  888. case USB_RECIP_ENDPOINT:
  889. ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
  890. if (!ep)
  891. return -ENOENT;
  892. reply = cpu_to_le16(ep->halted ? 1 : 0);
  893. break;
  894. default:
  895. return 0;
  896. }
  897. if (le16_to_cpu(ctrl->wLength) != 2)
  898. return -EINVAL;
  899. ret = s3c_hsotg_send_reply(hsotg, ep0, &reply, 2);
  900. if (ret) {
  901. dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
  902. return ret;
  903. }
  904. return 1;
  905. }
  906. static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value);
  907. /**
  908. * get_ep_head - return the first request on the endpoint
  909. * @hs_ep: The controller endpoint to get
  910. *
  911. * Get the first request on the endpoint.
  912. */
  913. static struct s3c_hsotg_req *get_ep_head(struct s3c_hsotg_ep *hs_ep)
  914. {
  915. if (list_empty(&hs_ep->queue))
  916. return NULL;
  917. return list_first_entry(&hs_ep->queue, struct s3c_hsotg_req, queue);
  918. }
  919. /**
  920. * s3c_hsotg_process_req_featire - process request {SET,CLEAR}_FEATURE
  921. * @hsotg: The device state
  922. * @ctrl: USB control request
  923. */
  924. static int s3c_hsotg_process_req_feature(struct s3c_hsotg *hsotg,
  925. struct usb_ctrlrequest *ctrl)
  926. {
  927. struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
  928. struct s3c_hsotg_req *hs_req;
  929. bool restart;
  930. bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
  931. struct s3c_hsotg_ep *ep;
  932. int ret;
  933. dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
  934. __func__, set ? "SET" : "CLEAR");
  935. if (ctrl->bRequestType == USB_RECIP_ENDPOINT) {
  936. ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
  937. if (!ep) {
  938. dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
  939. __func__, le16_to_cpu(ctrl->wIndex));
  940. return -ENOENT;
  941. }
  942. switch (le16_to_cpu(ctrl->wValue)) {
  943. case USB_ENDPOINT_HALT:
  944. s3c_hsotg_ep_sethalt(&ep->ep, set);
  945. ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
  946. if (ret) {
  947. dev_err(hsotg->dev,
  948. "%s: failed to send reply\n", __func__);
  949. return ret;
  950. }
  951. if (!set) {
  952. /*
  953. * If we have request in progress,
  954. * then complete it
  955. */
  956. if (ep->req) {
  957. hs_req = ep->req;
  958. ep->req = NULL;
  959. list_del_init(&hs_req->queue);
  960. hs_req->req.complete(&ep->ep,
  961. &hs_req->req);
  962. }
  963. /* If we have pending request, then start it */
  964. restart = !list_empty(&ep->queue);
  965. if (restart) {
  966. hs_req = get_ep_head(ep);
  967. s3c_hsotg_start_req(hsotg, ep,
  968. hs_req, false);
  969. }
  970. }
  971. break;
  972. default:
  973. return -ENOENT;
  974. }
  975. } else
  976. return -ENOENT; /* currently only deal with endpoint */
  977. return 1;
  978. }
  979. /**
  980. * s3c_hsotg_process_control - process a control request
  981. * @hsotg: The device state
  982. * @ctrl: The control request received
  983. *
  984. * The controller has received the SETUP phase of a control request, and
  985. * needs to work out what to do next (and whether to pass it on to the
  986. * gadget driver).
  987. */
  988. static void s3c_hsotg_process_control(struct s3c_hsotg *hsotg,
  989. struct usb_ctrlrequest *ctrl)
  990. {
  991. struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
  992. int ret = 0;
  993. u32 dcfg;
  994. ep0->sent_zlp = 0;
  995. dev_dbg(hsotg->dev, "ctrl Req=%02x, Type=%02x, V=%04x, L=%04x\n",
  996. ctrl->bRequest, ctrl->bRequestType,
  997. ctrl->wValue, ctrl->wLength);
  998. /*
  999. * record the direction of the request, for later use when enquing
  1000. * packets onto EP0.
  1001. */
  1002. ep0->dir_in = (ctrl->bRequestType & USB_DIR_IN) ? 1 : 0;
  1003. dev_dbg(hsotg->dev, "ctrl: dir_in=%d\n", ep0->dir_in);
  1004. /*
  1005. * if we've no data with this request, then the last part of the
  1006. * transaction is going to implicitly be IN.
  1007. */
  1008. if (ctrl->wLength == 0)
  1009. ep0->dir_in = 1;
  1010. if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
  1011. switch (ctrl->bRequest) {
  1012. case USB_REQ_SET_ADDRESS:
  1013. dcfg = readl(hsotg->regs + DCFG);
  1014. dcfg &= ~DCFG_DevAddr_MASK;
  1015. dcfg |= ctrl->wValue << DCFG_DevAddr_SHIFT;
  1016. writel(dcfg, hsotg->regs + DCFG);
  1017. dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
  1018. ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
  1019. return;
  1020. case USB_REQ_GET_STATUS:
  1021. ret = s3c_hsotg_process_req_status(hsotg, ctrl);
  1022. break;
  1023. case USB_REQ_CLEAR_FEATURE:
  1024. case USB_REQ_SET_FEATURE:
  1025. ret = s3c_hsotg_process_req_feature(hsotg, ctrl);
  1026. break;
  1027. }
  1028. }
  1029. /* as a fallback, try delivering it to the driver to deal with */
  1030. if (ret == 0 && hsotg->driver) {
  1031. ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
  1032. if (ret < 0)
  1033. dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
  1034. }
  1035. /*
  1036. * the request is either unhandlable, or is not formatted correctly
  1037. * so respond with a STALL for the status stage to indicate failure.
  1038. */
  1039. if (ret < 0) {
  1040. u32 reg;
  1041. u32 ctrl;
  1042. dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
  1043. reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
  1044. /*
  1045. * DxEPCTL_Stall will be cleared by EP once it has
  1046. * taken effect, so no need to clear later.
  1047. */
  1048. ctrl = readl(hsotg->regs + reg);
  1049. ctrl |= DxEPCTL_Stall;
  1050. ctrl |= DxEPCTL_CNAK;
  1051. writel(ctrl, hsotg->regs + reg);
  1052. dev_dbg(hsotg->dev,
  1053. "written DxEPCTL=0x%08x to %08x (DxEPCTL=0x%08x)\n",
  1054. ctrl, reg, readl(hsotg->regs + reg));
  1055. /*
  1056. * don't believe we need to anything more to get the EP
  1057. * to reply with a STALL packet
  1058. */
  1059. }
  1060. }
  1061. static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg);
  1062. /**
  1063. * s3c_hsotg_complete_setup - completion of a setup transfer
  1064. * @ep: The endpoint the request was on.
  1065. * @req: The request completed.
  1066. *
  1067. * Called on completion of any requests the driver itself submitted for
  1068. * EP0 setup packets
  1069. */
  1070. static void s3c_hsotg_complete_setup(struct usb_ep *ep,
  1071. struct usb_request *req)
  1072. {
  1073. struct s3c_hsotg_ep *hs_ep = our_ep(ep);
  1074. struct s3c_hsotg *hsotg = hs_ep->parent;
  1075. if (req->status < 0) {
  1076. dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
  1077. return;
  1078. }
  1079. if (req->actual == 0)
  1080. s3c_hsotg_enqueue_setup(hsotg);
  1081. else
  1082. s3c_hsotg_process_control(hsotg, req->buf);
  1083. }
  1084. /**
  1085. * s3c_hsotg_enqueue_setup - start a request for EP0 packets
  1086. * @hsotg: The device state.
  1087. *
  1088. * Enqueue a request on EP0 if necessary to received any SETUP packets
  1089. * received from the host.
  1090. */
  1091. static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg)
  1092. {
  1093. struct usb_request *req = hsotg->ctrl_req;
  1094. struct s3c_hsotg_req *hs_req = our_req(req);
  1095. int ret;
  1096. dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
  1097. req->zero = 0;
  1098. req->length = 8;
  1099. req->buf = hsotg->ctrl_buff;
  1100. req->complete = s3c_hsotg_complete_setup;
  1101. if (!list_empty(&hs_req->queue)) {
  1102. dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
  1103. return;
  1104. }
  1105. hsotg->eps[0].dir_in = 0;
  1106. ret = s3c_hsotg_ep_queue(&hsotg->eps[0].ep, req, GFP_ATOMIC);
  1107. if (ret < 0) {
  1108. dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
  1109. /*
  1110. * Don't think there's much we can do other than watch the
  1111. * driver fail.
  1112. */
  1113. }
  1114. }
  1115. /**
  1116. * s3c_hsotg_complete_request - complete a request given to us
  1117. * @hsotg: The device state.
  1118. * @hs_ep: The endpoint the request was on.
  1119. * @hs_req: The request to complete.
  1120. * @result: The result code (0 => Ok, otherwise errno)
  1121. *
  1122. * The given request has finished, so call the necessary completion
  1123. * if it has one and then look to see if we can start a new request
  1124. * on the endpoint.
  1125. *
  1126. * Note, expects the ep to already be locked as appropriate.
  1127. */
  1128. static void s3c_hsotg_complete_request(struct s3c_hsotg *hsotg,
  1129. struct s3c_hsotg_ep *hs_ep,
  1130. struct s3c_hsotg_req *hs_req,
  1131. int result)
  1132. {
  1133. bool restart;
  1134. if (!hs_req) {
  1135. dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
  1136. return;
  1137. }
  1138. dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
  1139. hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
  1140. /*
  1141. * only replace the status if we've not already set an error
  1142. * from a previous transaction
  1143. */
  1144. if (hs_req->req.status == -EINPROGRESS)
  1145. hs_req->req.status = result;
  1146. hs_ep->req = NULL;
  1147. list_del_init(&hs_req->queue);
  1148. if (using_dma(hsotg))
  1149. s3c_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
  1150. /*
  1151. * call the complete request with the locks off, just in case the
  1152. * request tries to queue more work for this endpoint.
  1153. */
  1154. if (hs_req->req.complete) {
  1155. spin_unlock(&hsotg->lock);
  1156. hs_req->req.complete(&hs_ep->ep, &hs_req->req);
  1157. spin_lock(&hsotg->lock);
  1158. }
  1159. /*
  1160. * Look to see if there is anything else to do. Note, the completion
  1161. * of the previous request may have caused a new request to be started
  1162. * so be careful when doing this.
  1163. */
  1164. if (!hs_ep->req && result >= 0) {
  1165. restart = !list_empty(&hs_ep->queue);
  1166. if (restart) {
  1167. hs_req = get_ep_head(hs_ep);
  1168. s3c_hsotg_start_req(hsotg, hs_ep, hs_req, false);
  1169. }
  1170. }
  1171. }
  1172. /**
  1173. * s3c_hsotg_rx_data - receive data from the FIFO for an endpoint
  1174. * @hsotg: The device state.
  1175. * @ep_idx: The endpoint index for the data
  1176. * @size: The size of data in the fifo, in bytes
  1177. *
  1178. * The FIFO status shows there is data to read from the FIFO for a given
  1179. * endpoint, so sort out whether we need to read the data into a request
  1180. * that has been made for that endpoint.
  1181. */
  1182. static void s3c_hsotg_rx_data(struct s3c_hsotg *hsotg, int ep_idx, int size)
  1183. {
  1184. struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep_idx];
  1185. struct s3c_hsotg_req *hs_req = hs_ep->req;
  1186. void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
  1187. int to_read;
  1188. int max_req;
  1189. int read_ptr;
  1190. if (!hs_req) {
  1191. u32 epctl = readl(hsotg->regs + DOEPCTL(ep_idx));
  1192. int ptr;
  1193. dev_warn(hsotg->dev,
  1194. "%s: FIFO %d bytes on ep%d but no req (DxEPCTl=0x%08x)\n",
  1195. __func__, size, ep_idx, epctl);
  1196. /* dump the data from the FIFO, we've nothing we can do */
  1197. for (ptr = 0; ptr < size; ptr += 4)
  1198. (void)readl(fifo);
  1199. return;
  1200. }
  1201. to_read = size;
  1202. read_ptr = hs_req->req.actual;
  1203. max_req = hs_req->req.length - read_ptr;
  1204. dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
  1205. __func__, to_read, max_req, read_ptr, hs_req->req.length);
  1206. if (to_read > max_req) {
  1207. /*
  1208. * more data appeared than we where willing
  1209. * to deal with in this request.
  1210. */
  1211. /* currently we don't deal this */
  1212. WARN_ON_ONCE(1);
  1213. }
  1214. hs_ep->total_data += to_read;
  1215. hs_req->req.actual += to_read;
  1216. to_read = DIV_ROUND_UP(to_read, 4);
  1217. /*
  1218. * note, we might over-write the buffer end by 3 bytes depending on
  1219. * alignment of the data.
  1220. */
  1221. readsl(fifo, hs_req->req.buf + read_ptr, to_read);
  1222. }
  1223. /**
  1224. * s3c_hsotg_send_zlp - send zero-length packet on control endpoint
  1225. * @hsotg: The device instance
  1226. * @req: The request currently on this endpoint
  1227. *
  1228. * Generate a zero-length IN packet request for terminating a SETUP
  1229. * transaction.
  1230. *
  1231. * Note, since we don't write any data to the TxFIFO, then it is
  1232. * currently believed that we do not need to wait for any space in
  1233. * the TxFIFO.
  1234. */
  1235. static void s3c_hsotg_send_zlp(struct s3c_hsotg *hsotg,
  1236. struct s3c_hsotg_req *req)
  1237. {
  1238. u32 ctrl;
  1239. if (!req) {
  1240. dev_warn(hsotg->dev, "%s: no request?\n", __func__);
  1241. return;
  1242. }
  1243. if (req->req.length == 0) {
  1244. hsotg->eps[0].sent_zlp = 1;
  1245. s3c_hsotg_enqueue_setup(hsotg);
  1246. return;
  1247. }
  1248. hsotg->eps[0].dir_in = 1;
  1249. hsotg->eps[0].sent_zlp = 1;
  1250. dev_dbg(hsotg->dev, "sending zero-length packet\n");
  1251. /* issue a zero-sized packet to terminate this */
  1252. writel(DxEPTSIZ_MC(1) | DxEPTSIZ_PktCnt(1) |
  1253. DxEPTSIZ_XferSize(0), hsotg->regs + DIEPTSIZ(0));
  1254. ctrl = readl(hsotg->regs + DIEPCTL0);
  1255. ctrl |= DxEPCTL_CNAK; /* clear NAK set by core */
  1256. ctrl |= DxEPCTL_EPEna; /* ensure ep enabled */
  1257. ctrl |= DxEPCTL_USBActEp;
  1258. writel(ctrl, hsotg->regs + DIEPCTL0);
  1259. }
  1260. /**
  1261. * s3c_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
  1262. * @hsotg: The device instance
  1263. * @epnum: The endpoint received from
  1264. * @was_setup: Set if processing a SetupDone event.
  1265. *
  1266. * The RXFIFO has delivered an OutDone event, which means that the data
  1267. * transfer for an OUT endpoint has been completed, either by a short
  1268. * packet or by the finish of a transfer.
  1269. */
  1270. static void s3c_hsotg_handle_outdone(struct s3c_hsotg *hsotg,
  1271. int epnum, bool was_setup)
  1272. {
  1273. u32 epsize = readl(hsotg->regs + DOEPTSIZ(epnum));
  1274. struct s3c_hsotg_ep *hs_ep = &hsotg->eps[epnum];
  1275. struct s3c_hsotg_req *hs_req = hs_ep->req;
  1276. struct usb_request *req = &hs_req->req;
  1277. unsigned size_left = DxEPTSIZ_XferSize_GET(epsize);
  1278. int result = 0;
  1279. if (!hs_req) {
  1280. dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
  1281. return;
  1282. }
  1283. if (using_dma(hsotg)) {
  1284. unsigned size_done;
  1285. /*
  1286. * Calculate the size of the transfer by checking how much
  1287. * is left in the endpoint size register and then working it
  1288. * out from the amount we loaded for the transfer.
  1289. *
  1290. * We need to do this as DMA pointers are always 32bit aligned
  1291. * so may overshoot/undershoot the transfer.
  1292. */
  1293. size_done = hs_ep->size_loaded - size_left;
  1294. size_done += hs_ep->last_load;
  1295. req->actual = size_done;
  1296. }
  1297. /* if there is more request to do, schedule new transfer */
  1298. if (req->actual < req->length && size_left == 0) {
  1299. s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
  1300. return;
  1301. } else if (epnum == 0) {
  1302. /*
  1303. * After was_setup = 1 =>
  1304. * set CNAK for non Setup requests
  1305. */
  1306. hsotg->setup = was_setup ? 0 : 1;
  1307. }
  1308. if (req->actual < req->length && req->short_not_ok) {
  1309. dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
  1310. __func__, req->actual, req->length);
  1311. /*
  1312. * todo - what should we return here? there's no one else
  1313. * even bothering to check the status.
  1314. */
  1315. }
  1316. if (epnum == 0) {
  1317. /*
  1318. * Condition req->complete != s3c_hsotg_complete_setup says:
  1319. * send ZLP when we have an asynchronous request from gadget
  1320. */
  1321. if (!was_setup && req->complete != s3c_hsotg_complete_setup)
  1322. s3c_hsotg_send_zlp(hsotg, hs_req);
  1323. }
  1324. s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
  1325. }
  1326. /**
  1327. * s3c_hsotg_read_frameno - read current frame number
  1328. * @hsotg: The device instance
  1329. *
  1330. * Return the current frame number
  1331. */
  1332. static u32 s3c_hsotg_read_frameno(struct s3c_hsotg *hsotg)
  1333. {
  1334. u32 dsts;
  1335. dsts = readl(hsotg->regs + DSTS);
  1336. dsts &= DSTS_SOFFN_MASK;
  1337. dsts >>= DSTS_SOFFN_SHIFT;
  1338. return dsts;
  1339. }
  1340. /**
  1341. * s3c_hsotg_handle_rx - RX FIFO has data
  1342. * @hsotg: The device instance
  1343. *
  1344. * The IRQ handler has detected that the RX FIFO has some data in it
  1345. * that requires processing, so find out what is in there and do the
  1346. * appropriate read.
  1347. *
  1348. * The RXFIFO is a true FIFO, the packets coming out are still in packet
  1349. * chunks, so if you have x packets received on an endpoint you'll get x
  1350. * FIFO events delivered, each with a packet's worth of data in it.
  1351. *
  1352. * When using DMA, we should not be processing events from the RXFIFO
  1353. * as the actual data should be sent to the memory directly and we turn
  1354. * on the completion interrupts to get notifications of transfer completion.
  1355. */
  1356. static void s3c_hsotg_handle_rx(struct s3c_hsotg *hsotg)
  1357. {
  1358. u32 grxstsr = readl(hsotg->regs + GRXSTSP);
  1359. u32 epnum, status, size;
  1360. WARN_ON(using_dma(hsotg));
  1361. epnum = grxstsr & GRXSTS_EPNum_MASK;
  1362. status = grxstsr & GRXSTS_PktSts_MASK;
  1363. size = grxstsr & GRXSTS_ByteCnt_MASK;
  1364. size >>= GRXSTS_ByteCnt_SHIFT;
  1365. if (1)
  1366. dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
  1367. __func__, grxstsr, size, epnum);
  1368. #define __status(x) ((x) >> GRXSTS_PktSts_SHIFT)
  1369. switch (status >> GRXSTS_PktSts_SHIFT) {
  1370. case __status(GRXSTS_PktSts_GlobalOutNAK):
  1371. dev_dbg(hsotg->dev, "GlobalOutNAK\n");
  1372. break;
  1373. case __status(GRXSTS_PktSts_OutDone):
  1374. dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
  1375. s3c_hsotg_read_frameno(hsotg));
  1376. if (!using_dma(hsotg))
  1377. s3c_hsotg_handle_outdone(hsotg, epnum, false);
  1378. break;
  1379. case __status(GRXSTS_PktSts_SetupDone):
  1380. dev_dbg(hsotg->dev,
  1381. "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
  1382. s3c_hsotg_read_frameno(hsotg),
  1383. readl(hsotg->regs + DOEPCTL(0)));
  1384. s3c_hsotg_handle_outdone(hsotg, epnum, true);
  1385. break;
  1386. case __status(GRXSTS_PktSts_OutRX):
  1387. s3c_hsotg_rx_data(hsotg, epnum, size);
  1388. break;
  1389. case __status(GRXSTS_PktSts_SetupRX):
  1390. dev_dbg(hsotg->dev,
  1391. "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
  1392. s3c_hsotg_read_frameno(hsotg),
  1393. readl(hsotg->regs + DOEPCTL(0)));
  1394. s3c_hsotg_rx_data(hsotg, epnum, size);
  1395. break;
  1396. default:
  1397. dev_warn(hsotg->dev, "%s: unknown status %08x\n",
  1398. __func__, grxstsr);
  1399. s3c_hsotg_dump(hsotg);
  1400. break;
  1401. }
  1402. }
  1403. /**
  1404. * s3c_hsotg_ep0_mps - turn max packet size into register setting
  1405. * @mps: The maximum packet size in bytes.
  1406. */
  1407. static u32 s3c_hsotg_ep0_mps(unsigned int mps)
  1408. {
  1409. switch (mps) {
  1410. case 64:
  1411. return D0EPCTL_MPS_64;
  1412. case 32:
  1413. return D0EPCTL_MPS_32;
  1414. case 16:
  1415. return D0EPCTL_MPS_16;
  1416. case 8:
  1417. return D0EPCTL_MPS_8;
  1418. }
  1419. /* bad max packet size, warn and return invalid result */
  1420. WARN_ON(1);
  1421. return (u32)-1;
  1422. }
  1423. /**
  1424. * s3c_hsotg_set_ep_maxpacket - set endpoint's max-packet field
  1425. * @hsotg: The driver state.
  1426. * @ep: The index number of the endpoint
  1427. * @mps: The maximum packet size in bytes
  1428. *
  1429. * Configure the maximum packet size for the given endpoint, updating
  1430. * the hardware control registers to reflect this.
  1431. */
  1432. static void s3c_hsotg_set_ep_maxpacket(struct s3c_hsotg *hsotg,
  1433. unsigned int ep, unsigned int mps)
  1434. {
  1435. struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep];
  1436. void __iomem *regs = hsotg->regs;
  1437. u32 mpsval;
  1438. u32 reg;
  1439. if (ep == 0) {
  1440. /* EP0 is a special case */
  1441. mpsval = s3c_hsotg_ep0_mps(mps);
  1442. if (mpsval > 3)
  1443. goto bad_mps;
  1444. } else {
  1445. if (mps >= DxEPCTL_MPS_LIMIT+1)
  1446. goto bad_mps;
  1447. mpsval = mps;
  1448. }
  1449. hs_ep->ep.maxpacket = mps;
  1450. /*
  1451. * update both the in and out endpoint controldir_ registers, even
  1452. * if one of the directions may not be in use.
  1453. */
  1454. reg = readl(regs + DIEPCTL(ep));
  1455. reg &= ~DxEPCTL_MPS_MASK;
  1456. reg |= mpsval;
  1457. writel(reg, regs + DIEPCTL(ep));
  1458. if (ep) {
  1459. reg = readl(regs + DOEPCTL(ep));
  1460. reg &= ~DxEPCTL_MPS_MASK;
  1461. reg |= mpsval;
  1462. writel(reg, regs + DOEPCTL(ep));
  1463. }
  1464. return;
  1465. bad_mps:
  1466. dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
  1467. }
  1468. /**
  1469. * s3c_hsotg_txfifo_flush - flush Tx FIFO
  1470. * @hsotg: The driver state
  1471. * @idx: The index for the endpoint (0..15)
  1472. */
  1473. static void s3c_hsotg_txfifo_flush(struct s3c_hsotg *hsotg, unsigned int idx)
  1474. {
  1475. int timeout;
  1476. int val;
  1477. writel(GRSTCTL_TxFNum(idx) | GRSTCTL_TxFFlsh,
  1478. hsotg->regs + GRSTCTL);
  1479. /* wait until the fifo is flushed */
  1480. timeout = 100;
  1481. while (1) {
  1482. val = readl(hsotg->regs + GRSTCTL);
  1483. if ((val & (GRSTCTL_TxFFlsh)) == 0)
  1484. break;
  1485. if (--timeout == 0) {
  1486. dev_err(hsotg->dev,
  1487. "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
  1488. __func__, val);
  1489. }
  1490. udelay(1);
  1491. }
  1492. }
  1493. /**
  1494. * s3c_hsotg_trytx - check to see if anything needs transmitting
  1495. * @hsotg: The driver state
  1496. * @hs_ep: The driver endpoint to check.
  1497. *
  1498. * Check to see if there is a request that has data to send, and if so
  1499. * make an attempt to write data into the FIFO.
  1500. */
  1501. static int s3c_hsotg_trytx(struct s3c_hsotg *hsotg,
  1502. struct s3c_hsotg_ep *hs_ep)
  1503. {
  1504. struct s3c_hsotg_req *hs_req = hs_ep->req;
  1505. if (!hs_ep->dir_in || !hs_req)
  1506. return 0;
  1507. if (hs_req->req.actual < hs_req->req.length) {
  1508. dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
  1509. hs_ep->index);
  1510. return s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
  1511. }
  1512. return 0;
  1513. }
  1514. /**
  1515. * s3c_hsotg_complete_in - complete IN transfer
  1516. * @hsotg: The device state.
  1517. * @hs_ep: The endpoint that has just completed.
  1518. *
  1519. * An IN transfer has been completed, update the transfer's state and then
  1520. * call the relevant completion routines.
  1521. */
  1522. static void s3c_hsotg_complete_in(struct s3c_hsotg *hsotg,
  1523. struct s3c_hsotg_ep *hs_ep)
  1524. {
  1525. struct s3c_hsotg_req *hs_req = hs_ep->req;
  1526. u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
  1527. int size_left, size_done;
  1528. if (!hs_req) {
  1529. dev_dbg(hsotg->dev, "XferCompl but no req\n");
  1530. return;
  1531. }
  1532. /* Finish ZLP handling for IN EP0 transactions */
  1533. if (hsotg->eps[0].sent_zlp) {
  1534. dev_dbg(hsotg->dev, "zlp packet received\n");
  1535. s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
  1536. return;
  1537. }
  1538. /*
  1539. * Calculate the size of the transfer by checking how much is left
  1540. * in the endpoint size register and then working it out from
  1541. * the amount we loaded for the transfer.
  1542. *
  1543. * We do this even for DMA, as the transfer may have incremented
  1544. * past the end of the buffer (DMA transfers are always 32bit
  1545. * aligned).
  1546. */
  1547. size_left = DxEPTSIZ_XferSize_GET(epsize);
  1548. size_done = hs_ep->size_loaded - size_left;
  1549. size_done += hs_ep->last_load;
  1550. if (hs_req->req.actual != size_done)
  1551. dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
  1552. __func__, hs_req->req.actual, size_done);
  1553. hs_req->req.actual = size_done;
  1554. dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
  1555. hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
  1556. /*
  1557. * Check if dealing with Maximum Packet Size(MPS) IN transfer at EP0
  1558. * When sent data is a multiple MPS size (e.g. 64B ,128B ,192B
  1559. * ,256B ... ), after last MPS sized packet send IN ZLP packet to
  1560. * inform the host that no more data is available.
  1561. * The state of req.zero member is checked to be sure that the value to
  1562. * send is smaller than wValue expected from host.
  1563. * Check req.length to NOT send another ZLP when the current one is
  1564. * under completion (the one for which this completion has been called).
  1565. */
  1566. if (hs_req->req.length && hs_ep->index == 0 && hs_req->req.zero &&
  1567. hs_req->req.length == hs_req->req.actual &&
  1568. !(hs_req->req.length % hs_ep->ep.maxpacket)) {
  1569. dev_dbg(hsotg->dev, "ep0 zlp IN packet sent\n");
  1570. s3c_hsotg_send_zlp(hsotg, hs_req);
  1571. return;
  1572. }
  1573. if (!size_left && hs_req->req.actual < hs_req->req.length) {
  1574. dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
  1575. s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
  1576. } else
  1577. s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
  1578. }
  1579. /**
  1580. * s3c_hsotg_epint - handle an in/out endpoint interrupt
  1581. * @hsotg: The driver state
  1582. * @idx: The index for the endpoint (0..15)
  1583. * @dir_in: Set if this is an IN endpoint
  1584. *
  1585. * Process and clear any interrupt pending for an individual endpoint
  1586. */
  1587. static void s3c_hsotg_epint(struct s3c_hsotg *hsotg, unsigned int idx,
  1588. int dir_in)
  1589. {
  1590. struct s3c_hsotg_ep *hs_ep = &hsotg->eps[idx];
  1591. u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
  1592. u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
  1593. u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
  1594. u32 ints;
  1595. ints = readl(hsotg->regs + epint_reg);
  1596. /* Clear endpoint interrupts */
  1597. writel(ints, hsotg->regs + epint_reg);
  1598. dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
  1599. __func__, idx, dir_in ? "in" : "out", ints);
  1600. if (ints & DxEPINT_XferCompl) {
  1601. dev_dbg(hsotg->dev,
  1602. "%s: XferCompl: DxEPCTL=0x%08x, DxEPTSIZ=%08x\n",
  1603. __func__, readl(hsotg->regs + epctl_reg),
  1604. readl(hsotg->regs + epsiz_reg));
  1605. /*
  1606. * we get OutDone from the FIFO, so we only need to look
  1607. * at completing IN requests here
  1608. */
  1609. if (dir_in) {
  1610. s3c_hsotg_complete_in(hsotg, hs_ep);
  1611. if (idx == 0 && !hs_ep->req)
  1612. s3c_hsotg_enqueue_setup(hsotg);
  1613. } else if (using_dma(hsotg)) {
  1614. /*
  1615. * We're using DMA, we need to fire an OutDone here
  1616. * as we ignore the RXFIFO.
  1617. */
  1618. s3c_hsotg_handle_outdone(hsotg, idx, false);
  1619. }
  1620. }
  1621. if (ints & DxEPINT_EPDisbld) {
  1622. dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
  1623. if (dir_in) {
  1624. int epctl = readl(hsotg->regs + epctl_reg);
  1625. s3c_hsotg_txfifo_flush(hsotg, idx);
  1626. if ((epctl & DxEPCTL_Stall) &&
  1627. (epctl & DxEPCTL_EPType_Bulk)) {
  1628. int dctl = readl(hsotg->regs + DCTL);
  1629. dctl |= DCTL_CGNPInNAK;
  1630. writel(dctl, hsotg->regs + DCTL);
  1631. }
  1632. }
  1633. }
  1634. if (ints & DxEPINT_AHBErr)
  1635. dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
  1636. if (ints & DxEPINT_Setup) { /* Setup or Timeout */
  1637. dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
  1638. if (using_dma(hsotg) && idx == 0) {
  1639. /*
  1640. * this is the notification we've received a
  1641. * setup packet. In non-DMA mode we'd get this
  1642. * from the RXFIFO, instead we need to process
  1643. * the setup here.
  1644. */
  1645. if (dir_in)
  1646. WARN_ON_ONCE(1);
  1647. else
  1648. s3c_hsotg_handle_outdone(hsotg, 0, true);
  1649. }
  1650. }
  1651. if (ints & DxEPINT_Back2BackSetup)
  1652. dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
  1653. if (dir_in) {
  1654. /* not sure if this is important, but we'll clear it anyway */
  1655. if (ints & DIEPMSK_INTknTXFEmpMsk) {
  1656. dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
  1657. __func__, idx);
  1658. }
  1659. /* this probably means something bad is happening */
  1660. if (ints & DIEPMSK_INTknEPMisMsk) {
  1661. dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
  1662. __func__, idx);
  1663. }
  1664. /* FIFO has space or is empty (see GAHBCFG) */
  1665. if (hsotg->dedicated_fifos &&
  1666. ints & DIEPMSK_TxFIFOEmpty) {
  1667. dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
  1668. __func__, idx);
  1669. if (!using_dma(hsotg))
  1670. s3c_hsotg_trytx(hsotg, hs_ep);
  1671. }
  1672. }
  1673. }
  1674. /**
  1675. * s3c_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
  1676. * @hsotg: The device state.
  1677. *
  1678. * Handle updating the device settings after the enumeration phase has
  1679. * been completed.
  1680. */
  1681. static void s3c_hsotg_irq_enumdone(struct s3c_hsotg *hsotg)
  1682. {
  1683. u32 dsts = readl(hsotg->regs + DSTS);
  1684. int ep0_mps = 0, ep_mps;
  1685. /*
  1686. * This should signal the finish of the enumeration phase
  1687. * of the USB handshaking, so we should now know what rate
  1688. * we connected at.
  1689. */
  1690. dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
  1691. /*
  1692. * note, since we're limited by the size of transfer on EP0, and
  1693. * it seems IN transfers must be a even number of packets we do
  1694. * not advertise a 64byte MPS on EP0.
  1695. */
  1696. /* catch both EnumSpd_FS and EnumSpd_FS48 */
  1697. switch (dsts & DSTS_EnumSpd_MASK) {
  1698. case DSTS_EnumSpd_FS:
  1699. case DSTS_EnumSpd_FS48:
  1700. hsotg->gadget.speed = USB_SPEED_FULL;
  1701. ep0_mps = EP0_MPS_LIMIT;
  1702. ep_mps = 64;
  1703. break;
  1704. case DSTS_EnumSpd_HS:
  1705. hsotg->gadget.speed = USB_SPEED_HIGH;
  1706. ep0_mps = EP0_MPS_LIMIT;
  1707. ep_mps = 512;
  1708. break;
  1709. case DSTS_EnumSpd_LS:
  1710. hsotg->gadget.speed = USB_SPEED_LOW;
  1711. /*
  1712. * note, we don't actually support LS in this driver at the
  1713. * moment, and the documentation seems to imply that it isn't
  1714. * supported by the PHYs on some of the devices.
  1715. */
  1716. break;
  1717. }
  1718. dev_info(hsotg->dev, "new device is %s\n",
  1719. usb_speed_string(hsotg->gadget.speed));
  1720. /*
  1721. * we should now know the maximum packet size for an
  1722. * endpoint, so set the endpoints to a default value.
  1723. */
  1724. if (ep0_mps) {
  1725. int i;
  1726. s3c_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps);
  1727. for (i = 1; i < hsotg->num_of_eps; i++)
  1728. s3c_hsotg_set_ep_maxpacket(hsotg, i, ep_mps);
  1729. }
  1730. /* ensure after enumeration our EP0 is active */
  1731. s3c_hsotg_enqueue_setup(hsotg);
  1732. dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
  1733. readl(hsotg->regs + DIEPCTL0),
  1734. readl(hsotg->regs + DOEPCTL0));
  1735. }
  1736. /**
  1737. * kill_all_requests - remove all requests from the endpoint's queue
  1738. * @hsotg: The device state.
  1739. * @ep: The endpoint the requests may be on.
  1740. * @result: The result code to use.
  1741. * @force: Force removal of any current requests
  1742. *
  1743. * Go through the requests on the given endpoint and mark them
  1744. * completed with the given result code.
  1745. */
  1746. static void kill_all_requests(struct s3c_hsotg *hsotg,
  1747. struct s3c_hsotg_ep *ep,
  1748. int result, bool force)
  1749. {
  1750. struct s3c_hsotg_req *req, *treq;
  1751. list_for_each_entry_safe(req, treq, &ep->queue, queue) {
  1752. /*
  1753. * currently, we can't do much about an already
  1754. * running request on an in endpoint
  1755. */
  1756. if (ep->req == req && ep->dir_in && !force)
  1757. continue;
  1758. s3c_hsotg_complete_request(hsotg, ep, req,
  1759. result);
  1760. }
  1761. }
  1762. #define call_gadget(_hs, _entry) \
  1763. if ((_hs)->gadget.speed != USB_SPEED_UNKNOWN && \
  1764. (_hs)->driver && (_hs)->driver->_entry) { \
  1765. spin_unlock(&_hs->lock); \
  1766. (_hs)->driver->_entry(&(_hs)->gadget); \
  1767. spin_lock(&_hs->lock); \
  1768. }
  1769. /**
  1770. * s3c_hsotg_disconnect - disconnect service
  1771. * @hsotg: The device state.
  1772. *
  1773. * The device has been disconnected. Remove all current
  1774. * transactions and signal the gadget driver that this
  1775. * has happened.
  1776. */
  1777. static void s3c_hsotg_disconnect(struct s3c_hsotg *hsotg)
  1778. {
  1779. unsigned ep;
  1780. for (ep = 0; ep < hsotg->num_of_eps; ep++)
  1781. kill_all_requests(hsotg, &hsotg->eps[ep], -ESHUTDOWN, true);
  1782. call_gadget(hsotg, disconnect);
  1783. }
  1784. /**
  1785. * s3c_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
  1786. * @hsotg: The device state:
  1787. * @periodic: True if this is a periodic FIFO interrupt
  1788. */
  1789. static void s3c_hsotg_irq_fifoempty(struct s3c_hsotg *hsotg, bool periodic)
  1790. {
  1791. struct s3c_hsotg_ep *ep;
  1792. int epno, ret;
  1793. /* look through for any more data to transmit */
  1794. for (epno = 0; epno < hsotg->num_of_eps; epno++) {
  1795. ep = &hsotg->eps[epno];
  1796. if (!ep->dir_in)
  1797. continue;
  1798. if ((periodic && !ep->periodic) ||
  1799. (!periodic && ep->periodic))
  1800. continue;
  1801. ret = s3c_hsotg_trytx(hsotg, ep);
  1802. if (ret < 0)
  1803. break;
  1804. }
  1805. }
  1806. /* IRQ flags which will trigger a retry around the IRQ loop */
  1807. #define IRQ_RETRY_MASK (GINTSTS_NPTxFEmp | \
  1808. GINTSTS_PTxFEmp | \
  1809. GINTSTS_RxFLvl)
  1810. /**
  1811. * s3c_hsotg_corereset - issue softreset to the core
  1812. * @hsotg: The device state
  1813. *
  1814. * Issue a soft reset to the core, and await the core finishing it.
  1815. */
  1816. static int s3c_hsotg_corereset(struct s3c_hsotg *hsotg)
  1817. {
  1818. int timeout;
  1819. u32 grstctl;
  1820. dev_dbg(hsotg->dev, "resetting core\n");
  1821. /* issue soft reset */
  1822. writel(GRSTCTL_CSftRst, hsotg->regs + GRSTCTL);
  1823. timeout = 1000;
  1824. do {
  1825. grstctl = readl(hsotg->regs + GRSTCTL);
  1826. } while ((grstctl & GRSTCTL_CSftRst) && timeout-- > 0);
  1827. if (grstctl & GRSTCTL_CSftRst) {
  1828. dev_err(hsotg->dev, "Failed to get CSftRst asserted\n");
  1829. return -EINVAL;
  1830. }
  1831. timeout = 1000;
  1832. while (1) {
  1833. u32 grstctl = readl(hsotg->regs + GRSTCTL);
  1834. if (timeout-- < 0) {
  1835. dev_info(hsotg->dev,
  1836. "%s: reset failed, GRSTCTL=%08x\n",
  1837. __func__, grstctl);
  1838. return -ETIMEDOUT;
  1839. }
  1840. if (!(grstctl & GRSTCTL_AHBIdle))
  1841. continue;
  1842. break; /* reset done */
  1843. }
  1844. dev_dbg(hsotg->dev, "reset successful\n");
  1845. return 0;
  1846. }
  1847. /**
  1848. * s3c_hsotg_core_init - issue softreset to the core
  1849. * @hsotg: The device state
  1850. *
  1851. * Issue a soft reset to the core, and await the core finishing it.
  1852. */
  1853. static void s3c_hsotg_core_init(struct s3c_hsotg *hsotg)
  1854. {
  1855. s3c_hsotg_corereset(hsotg);
  1856. /*
  1857. * we must now enable ep0 ready for host detection and then
  1858. * set configuration.
  1859. */
  1860. /* set the PLL on, remove the HNP/SRP and set the PHY */
  1861. writel(GUSBCFG_PHYIf16 | GUSBCFG_TOutCal(7) |
  1862. (0x5 << 10), hsotg->regs + GUSBCFG);
  1863. s3c_hsotg_init_fifo(hsotg);
  1864. __orr32(hsotg->regs + DCTL, DCTL_SftDiscon);
  1865. writel(1 << 18 | DCFG_DevSpd_HS, hsotg->regs + DCFG);
  1866. /* Clear any pending OTG interrupts */
  1867. writel(0xffffffff, hsotg->regs + GOTGINT);
  1868. /* Clear any pending interrupts */
  1869. writel(0xffffffff, hsotg->regs + GINTSTS);
  1870. writel(GINTSTS_ErlySusp | GINTSTS_SessReqInt |
  1871. GINTSTS_GOUTNakEff | GINTSTS_GINNakEff |
  1872. GINTSTS_ConIDStsChng | GINTSTS_USBRst |
  1873. GINTSTS_EnumDone | GINTSTS_OTGInt |
  1874. GINTSTS_USBSusp | GINTSTS_WkUpInt,
  1875. hsotg->regs + GINTMSK);
  1876. if (using_dma(hsotg))
  1877. writel(GAHBCFG_GlblIntrEn | GAHBCFG_DMAEn |
  1878. GAHBCFG_HBstLen_Incr4,
  1879. hsotg->regs + GAHBCFG);
  1880. else
  1881. writel(GAHBCFG_GlblIntrEn, hsotg->regs + GAHBCFG);
  1882. /*
  1883. * Enabling INTknTXFEmpMsk here seems to be a big mistake, we end
  1884. * up being flooded with interrupts if the host is polling the
  1885. * endpoint to try and read data.
  1886. */
  1887. writel(((hsotg->dedicated_fifos) ? DIEPMSK_TxFIFOEmpty : 0) |
  1888. DIEPMSK_EPDisbldMsk | DIEPMSK_XferComplMsk |
  1889. DIEPMSK_TimeOUTMsk | DIEPMSK_AHBErrMsk |
  1890. DIEPMSK_INTknEPMisMsk,
  1891. hsotg->regs + DIEPMSK);
  1892. /*
  1893. * don't need XferCompl, we get that from RXFIFO in slave mode. In
  1894. * DMA mode we may need this.
  1895. */
  1896. writel((using_dma(hsotg) ? (DIEPMSK_XferComplMsk |
  1897. DIEPMSK_TimeOUTMsk) : 0) |
  1898. DOEPMSK_EPDisbldMsk | DOEPMSK_AHBErrMsk |
  1899. DOEPMSK_SetupMsk,
  1900. hsotg->regs + DOEPMSK);
  1901. writel(0, hsotg->regs + DAINTMSK);
  1902. dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
  1903. readl(hsotg->regs + DIEPCTL0),
  1904. readl(hsotg->regs + DOEPCTL0));
  1905. /* enable in and out endpoint interrupts */
  1906. s3c_hsotg_en_gsint(hsotg, GINTSTS_OEPInt | GINTSTS_IEPInt);
  1907. /*
  1908. * Enable the RXFIFO when in slave mode, as this is how we collect
  1909. * the data. In DMA mode, we get events from the FIFO but also
  1910. * things we cannot process, so do not use it.
  1911. */
  1912. if (!using_dma(hsotg))
  1913. s3c_hsotg_en_gsint(hsotg, GINTSTS_RxFLvl);
  1914. /* Enable interrupts for EP0 in and out */
  1915. s3c_hsotg_ctrl_epint(hsotg, 0, 0, 1);
  1916. s3c_hsotg_ctrl_epint(hsotg, 0, 1, 1);
  1917. __orr32(hsotg->regs + DCTL, DCTL_PWROnPrgDone);
  1918. udelay(10); /* see openiboot */
  1919. __bic32(hsotg->regs + DCTL, DCTL_PWROnPrgDone);
  1920. dev_dbg(hsotg->dev, "DCTL=0x%08x\n", readl(hsotg->regs + DCTL));
  1921. /*
  1922. * DxEPCTL_USBActEp says RO in manual, but seems to be set by
  1923. * writing to the EPCTL register..
  1924. */
  1925. /* set to read 1 8byte packet */
  1926. writel(DxEPTSIZ_MC(1) | DxEPTSIZ_PktCnt(1) |
  1927. DxEPTSIZ_XferSize(8), hsotg->regs + DOEPTSIZ0);
  1928. writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
  1929. DxEPCTL_CNAK | DxEPCTL_EPEna |
  1930. DxEPCTL_USBActEp,
  1931. hsotg->regs + DOEPCTL0);
  1932. /* enable, but don't activate EP0in */
  1933. writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
  1934. DxEPCTL_USBActEp, hsotg->regs + DIEPCTL0);
  1935. s3c_hsotg_enqueue_setup(hsotg);
  1936. dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
  1937. readl(hsotg->regs + DIEPCTL0),
  1938. readl(hsotg->regs + DOEPCTL0));
  1939. /* clear global NAKs */
  1940. writel(DCTL_CGOUTNak | DCTL_CGNPInNAK,
  1941. hsotg->regs + DCTL);
  1942. /* must be at-least 3ms to allow bus to see disconnect */
  1943. mdelay(3);
  1944. /* remove the soft-disconnect and let's go */
  1945. __bic32(hsotg->regs + DCTL, DCTL_SftDiscon);
  1946. }
  1947. /**
  1948. * s3c_hsotg_irq - handle device interrupt
  1949. * @irq: The IRQ number triggered
  1950. * @pw: The pw value when registered the handler.
  1951. */
  1952. static irqreturn_t s3c_hsotg_irq(int irq, void *pw)
  1953. {
  1954. struct s3c_hsotg *hsotg = pw;
  1955. int retry_count = 8;
  1956. u32 gintsts;
  1957. u32 gintmsk;
  1958. spin_lock(&hsotg->lock);
  1959. irq_retry:
  1960. gintsts = readl(hsotg->regs + GINTSTS);
  1961. gintmsk = readl(hsotg->regs + GINTMSK);
  1962. dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
  1963. __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
  1964. gintsts &= gintmsk;
  1965. if (gintsts & GINTSTS_OTGInt) {
  1966. u32 otgint = readl(hsotg->regs + GOTGINT);
  1967. dev_info(hsotg->dev, "OTGInt: %08x\n", otgint);
  1968. writel(otgint, hsotg->regs + GOTGINT);
  1969. }
  1970. if (gintsts & GINTSTS_SessReqInt) {
  1971. dev_dbg(hsotg->dev, "%s: SessReqInt\n", __func__);
  1972. writel(GINTSTS_SessReqInt, hsotg->regs + GINTSTS);
  1973. }
  1974. if (gintsts & GINTSTS_EnumDone) {
  1975. writel(GINTSTS_EnumDone, hsotg->regs + GINTSTS);
  1976. s3c_hsotg_irq_enumdone(hsotg);
  1977. }
  1978. if (gintsts & GINTSTS_ConIDStsChng) {
  1979. dev_dbg(hsotg->dev, "ConIDStsChg (DSTS=0x%08x, GOTCTL=%08x)\n",
  1980. readl(hsotg->regs + DSTS),
  1981. readl(hsotg->regs + GOTGCTL));
  1982. writel(GINTSTS_ConIDStsChng, hsotg->regs + GINTSTS);
  1983. }
  1984. if (gintsts & (GINTSTS_OEPInt | GINTSTS_IEPInt)) {
  1985. u32 daint = readl(hsotg->regs + DAINT);
  1986. u32 daint_out = daint >> DAINT_OutEP_SHIFT;
  1987. u32 daint_in = daint & ~(daint_out << DAINT_OutEP_SHIFT);
  1988. int ep;
  1989. dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
  1990. for (ep = 0; ep < 15 && daint_out; ep++, daint_out >>= 1) {
  1991. if (daint_out & 1)
  1992. s3c_hsotg_epint(hsotg, ep, 0);
  1993. }
  1994. for (ep = 0; ep < 15 && daint_in; ep++, daint_in >>= 1) {
  1995. if (daint_in & 1)
  1996. s3c_hsotg_epint(hsotg, ep, 1);
  1997. }
  1998. }
  1999. if (gintsts & GINTSTS_USBRst) {
  2000. u32 usb_status = readl(hsotg->regs + GOTGCTL);
  2001. dev_info(hsotg->dev, "%s: USBRst\n", __func__);
  2002. dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
  2003. readl(hsotg->regs + GNPTXSTS));
  2004. writel(GINTSTS_USBRst, hsotg->regs + GINTSTS);
  2005. if (usb_status & GOTGCTL_BSESVLD) {
  2006. if (time_after(jiffies, hsotg->last_rst +
  2007. msecs_to_jiffies(200))) {
  2008. kill_all_requests(hsotg, &hsotg->eps[0],
  2009. -ECONNRESET, true);
  2010. s3c_hsotg_core_init(hsotg);
  2011. hsotg->last_rst = jiffies;
  2012. }
  2013. }
  2014. }
  2015. /* check both FIFOs */
  2016. if (gintsts & GINTSTS_NPTxFEmp) {
  2017. dev_dbg(hsotg->dev, "NPTxFEmp\n");
  2018. /*
  2019. * Disable the interrupt to stop it happening again
  2020. * unless one of these endpoint routines decides that
  2021. * it needs re-enabling
  2022. */
  2023. s3c_hsotg_disable_gsint(hsotg, GINTSTS_NPTxFEmp);
  2024. s3c_hsotg_irq_fifoempty(hsotg, false);
  2025. }
  2026. if (gintsts & GINTSTS_PTxFEmp) {
  2027. dev_dbg(hsotg->dev, "PTxFEmp\n");
  2028. /* See note in GINTSTS_NPTxFEmp */
  2029. s3c_hsotg_disable_gsint(hsotg, GINTSTS_PTxFEmp);
  2030. s3c_hsotg_irq_fifoempty(hsotg, true);
  2031. }
  2032. if (gintsts & GINTSTS_RxFLvl) {
  2033. /*
  2034. * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
  2035. * we need to retry s3c_hsotg_handle_rx if this is still
  2036. * set.
  2037. */
  2038. s3c_hsotg_handle_rx(hsotg);
  2039. }
  2040. if (gintsts & GINTSTS_ModeMis) {
  2041. dev_warn(hsotg->dev, "warning, mode mismatch triggered\n");
  2042. writel(GINTSTS_ModeMis, hsotg->regs + GINTSTS);
  2043. }
  2044. if (gintsts & GINTSTS_USBSusp) {
  2045. dev_info(hsotg->dev, "GINTSTS_USBSusp\n");
  2046. writel(GINTSTS_USBSusp, hsotg->regs + GINTSTS);
  2047. call_gadget(hsotg, suspend);
  2048. s3c_hsotg_disconnect(hsotg);
  2049. }
  2050. if (gintsts & GINTSTS_WkUpInt) {
  2051. dev_info(hsotg->dev, "GINTSTS_WkUpIn\n");
  2052. writel(GINTSTS_WkUpInt, hsotg->regs + GINTSTS);
  2053. call_gadget(hsotg, resume);
  2054. }
  2055. if (gintsts & GINTSTS_ErlySusp) {
  2056. dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
  2057. writel(GINTSTS_ErlySusp, hsotg->regs + GINTSTS);
  2058. s3c_hsotg_disconnect(hsotg);
  2059. }
  2060. /*
  2061. * these next two seem to crop-up occasionally causing the core
  2062. * to shutdown the USB transfer, so try clearing them and logging
  2063. * the occurrence.
  2064. */
  2065. if (gintsts & GINTSTS_GOUTNakEff) {
  2066. dev_info(hsotg->dev, "GOUTNakEff triggered\n");
  2067. writel(DCTL_CGOUTNak, hsotg->regs + DCTL);
  2068. s3c_hsotg_dump(hsotg);
  2069. }
  2070. if (gintsts & GINTSTS_GINNakEff) {
  2071. dev_info(hsotg->dev, "GINNakEff triggered\n");
  2072. writel(DCTL_CGNPInNAK, hsotg->regs + DCTL);
  2073. s3c_hsotg_dump(hsotg);
  2074. }
  2075. /*
  2076. * if we've had fifo events, we should try and go around the
  2077. * loop again to see if there's any point in returning yet.
  2078. */
  2079. if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
  2080. goto irq_retry;
  2081. spin_unlock(&hsotg->lock);
  2082. return IRQ_HANDLED;
  2083. }
  2084. /**
  2085. * s3c_hsotg_ep_enable - enable the given endpoint
  2086. * @ep: The USB endpint to configure
  2087. * @desc: The USB endpoint descriptor to configure with.
  2088. *
  2089. * This is called from the USB gadget code's usb_ep_enable().
  2090. */
  2091. static int s3c_hsotg_ep_enable(struct usb_ep *ep,
  2092. const struct usb_endpoint_descriptor *desc)
  2093. {
  2094. struct s3c_hsotg_ep *hs_ep = our_ep(ep);
  2095. struct s3c_hsotg *hsotg = hs_ep->parent;
  2096. unsigned long flags;
  2097. int index = hs_ep->index;
  2098. u32 epctrl_reg;
  2099. u32 epctrl;
  2100. u32 mps;
  2101. int dir_in;
  2102. int ret = 0;
  2103. dev_dbg(hsotg->dev,
  2104. "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
  2105. __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
  2106. desc->wMaxPacketSize, desc->bInterval);
  2107. /* not to be called for EP0 */
  2108. WARN_ON(index == 0);
  2109. dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
  2110. if (dir_in != hs_ep->dir_in) {
  2111. dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
  2112. return -EINVAL;
  2113. }
  2114. mps = usb_endpoint_maxp(desc);
  2115. /* note, we handle this here instead of s3c_hsotg_set_ep_maxpacket */
  2116. epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
  2117. epctrl = readl(hsotg->regs + epctrl_reg);
  2118. dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
  2119. __func__, epctrl, epctrl_reg);
  2120. spin_lock_irqsave(&hsotg->lock, flags);
  2121. epctrl &= ~(DxEPCTL_EPType_MASK | DxEPCTL_MPS_MASK);
  2122. epctrl |= DxEPCTL_MPS(mps);
  2123. /*
  2124. * mark the endpoint as active, otherwise the core may ignore
  2125. * transactions entirely for this endpoint
  2126. */
  2127. epctrl |= DxEPCTL_USBActEp;
  2128. /*
  2129. * set the NAK status on the endpoint, otherwise we might try and
  2130. * do something with data that we've yet got a request to process
  2131. * since the RXFIFO will take data for an endpoint even if the
  2132. * size register hasn't been set.
  2133. */
  2134. epctrl |= DxEPCTL_SNAK;
  2135. /* update the endpoint state */
  2136. hs_ep->ep.maxpacket = mps;
  2137. /* default, set to non-periodic */
  2138. hs_ep->periodic = 0;
  2139. switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
  2140. case USB_ENDPOINT_XFER_ISOC:
  2141. dev_err(hsotg->dev, "no current ISOC support\n");
  2142. ret = -EINVAL;
  2143. goto out;
  2144. case USB_ENDPOINT_XFER_BULK:
  2145. epctrl |= DxEPCTL_EPType_Bulk;
  2146. break;
  2147. case USB_ENDPOINT_XFER_INT:
  2148. if (dir_in) {
  2149. /*
  2150. * Allocate our TxFNum by simply using the index
  2151. * of the endpoint for the moment. We could do
  2152. * something better if the host indicates how
  2153. * many FIFOs we are expecting to use.
  2154. */
  2155. hs_ep->periodic = 1;
  2156. epctrl |= DxEPCTL_TxFNum(index);
  2157. }
  2158. epctrl |= DxEPCTL_EPType_Intterupt;
  2159. break;
  2160. case USB_ENDPOINT_XFER_CONTROL:
  2161. epctrl |= DxEPCTL_EPType_Control;
  2162. break;
  2163. }
  2164. /*
  2165. * if the hardware has dedicated fifos, we must give each IN EP
  2166. * a unique tx-fifo even if it is non-periodic.
  2167. */
  2168. if (dir_in && hsotg->dedicated_fifos)
  2169. epctrl |= DxEPCTL_TxFNum(index);
  2170. /* for non control endpoints, set PID to D0 */
  2171. if (index)
  2172. epctrl |= DxEPCTL_SetD0PID;
  2173. dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
  2174. __func__, epctrl);
  2175. writel(epctrl, hsotg->regs + epctrl_reg);
  2176. dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
  2177. __func__, readl(hsotg->regs + epctrl_reg));
  2178. /* enable the endpoint interrupt */
  2179. s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
  2180. out:
  2181. spin_unlock_irqrestore(&hsotg->lock, flags);
  2182. return ret;
  2183. }
  2184. /**
  2185. * s3c_hsotg_ep_disable - disable given endpoint
  2186. * @ep: The endpoint to disable.
  2187. */
  2188. static int s3c_hsotg_ep_disable(struct usb_ep *ep)
  2189. {
  2190. struct s3c_hsotg_ep *hs_ep = our_ep(ep);
  2191. struct s3c_hsotg *hsotg = hs_ep->parent;
  2192. int dir_in = hs_ep->dir_in;
  2193. int index = hs_ep->index;
  2194. unsigned long flags;
  2195. u32 epctrl_reg;
  2196. u32 ctrl;
  2197. dev_info(hsotg->dev, "%s(ep %p)\n", __func__, ep);
  2198. if (ep == &hsotg->eps[0].ep) {
  2199. dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
  2200. return -EINVAL;
  2201. }
  2202. epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
  2203. spin_lock_irqsave(&hsotg->lock, flags);
  2204. /* terminate all requests with shutdown */
  2205. kill_all_requests(hsotg, hs_ep, -ESHUTDOWN, false);
  2206. ctrl = readl(hsotg->regs + epctrl_reg);
  2207. ctrl &= ~DxEPCTL_EPEna;
  2208. ctrl &= ~DxEPCTL_USBActEp;
  2209. ctrl |= DxEPCTL_SNAK;
  2210. dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
  2211. writel(ctrl, hsotg->regs + epctrl_reg);
  2212. /* disable endpoint interrupts */
  2213. s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
  2214. spin_unlock_irqrestore(&hsotg->lock, flags);
  2215. return 0;
  2216. }
  2217. /**
  2218. * on_list - check request is on the given endpoint
  2219. * @ep: The endpoint to check.
  2220. * @test: The request to test if it is on the endpoint.
  2221. */
  2222. static bool on_list(struct s3c_hsotg_ep *ep, struct s3c_hsotg_req *test)
  2223. {
  2224. struct s3c_hsotg_req *req, *treq;
  2225. list_for_each_entry_safe(req, treq, &ep->queue, queue) {
  2226. if (req == test)
  2227. return true;
  2228. }
  2229. return false;
  2230. }
  2231. /**
  2232. * s3c_hsotg_ep_dequeue - dequeue given endpoint
  2233. * @ep: The endpoint to dequeue.
  2234. * @req: The request to be removed from a queue.
  2235. */
  2236. static int s3c_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
  2237. {
  2238. struct s3c_hsotg_req *hs_req = our_req(req);
  2239. struct s3c_hsotg_ep *hs_ep = our_ep(ep);
  2240. struct s3c_hsotg *hs = hs_ep->parent;
  2241. unsigned long flags;
  2242. dev_info(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
  2243. spin_lock_irqsave(&hs->lock, flags);
  2244. if (!on_list(hs_ep, hs_req)) {
  2245. spin_unlock_irqrestore(&hs->lock, flags);
  2246. return -EINVAL;
  2247. }
  2248. s3c_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
  2249. spin_unlock_irqrestore(&hs->lock, flags);
  2250. return 0;
  2251. }
  2252. /**
  2253. * s3c_hsotg_ep_sethalt - set halt on a given endpoint
  2254. * @ep: The endpoint to set halt.
  2255. * @value: Set or unset the halt.
  2256. */
  2257. static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value)
  2258. {
  2259. struct s3c_hsotg_ep *hs_ep = our_ep(ep);
  2260. struct s3c_hsotg *hs = hs_ep->parent;
  2261. int index = hs_ep->index;
  2262. u32 epreg;
  2263. u32 epctl;
  2264. u32 xfertype;
  2265. dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
  2266. /* write both IN and OUT control registers */
  2267. epreg = DIEPCTL(index);
  2268. epctl = readl(hs->regs + epreg);
  2269. if (value) {
  2270. epctl |= DxEPCTL_Stall + DxEPCTL_SNAK;
  2271. if (epctl & DxEPCTL_EPEna)
  2272. epctl |= DxEPCTL_EPDis;
  2273. } else {
  2274. epctl &= ~DxEPCTL_Stall;
  2275. xfertype = epctl & DxEPCTL_EPType_MASK;
  2276. if (xfertype == DxEPCTL_EPType_Bulk ||
  2277. xfertype == DxEPCTL_EPType_Intterupt)
  2278. epctl |= DxEPCTL_SetD0PID;
  2279. }
  2280. writel(epctl, hs->regs + epreg);
  2281. epreg = DOEPCTL(index);
  2282. epctl = readl(hs->regs + epreg);
  2283. if (value)
  2284. epctl |= DxEPCTL_Stall;
  2285. else {
  2286. epctl &= ~DxEPCTL_Stall;
  2287. xfertype = epctl & DxEPCTL_EPType_MASK;
  2288. if (xfertype == DxEPCTL_EPType_Bulk ||
  2289. xfertype == DxEPCTL_EPType_Intterupt)
  2290. epctl |= DxEPCTL_SetD0PID;
  2291. }
  2292. writel(epctl, hs->regs + epreg);
  2293. return 0;
  2294. }
  2295. /**
  2296. * s3c_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
  2297. * @ep: The endpoint to set halt.
  2298. * @value: Set or unset the halt.
  2299. */
  2300. static int s3c_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
  2301. {
  2302. struct s3c_hsotg_ep *hs_ep = our_ep(ep);
  2303. struct s3c_hsotg *hs = hs_ep->parent;
  2304. unsigned long flags = 0;
  2305. int ret = 0;
  2306. spin_lock_irqsave(&hs->lock, flags);
  2307. ret = s3c_hsotg_ep_sethalt(ep, value);
  2308. spin_unlock_irqrestore(&hs->lock, flags);
  2309. return ret;
  2310. }
  2311. static struct usb_ep_ops s3c_hsotg_ep_ops = {
  2312. .enable = s3c_hsotg_ep_enable,
  2313. .disable = s3c_hsotg_ep_disable,
  2314. .alloc_request = s3c_hsotg_ep_alloc_request,
  2315. .free_request = s3c_hsotg_ep_free_request,
  2316. .queue = s3c_hsotg_ep_queue_lock,
  2317. .dequeue = s3c_hsotg_ep_dequeue,
  2318. .set_halt = s3c_hsotg_ep_sethalt_lock,
  2319. /* note, don't believe we have any call for the fifo routines */
  2320. };
  2321. /**
  2322. * s3c_hsotg_phy_enable - enable platform phy dev
  2323. * @hsotg: The driver state
  2324. *
  2325. * A wrapper for platform code responsible for controlling
  2326. * low-level USB code
  2327. */
  2328. static void s3c_hsotg_phy_enable(struct s3c_hsotg *hsotg)
  2329. {
  2330. struct platform_device *pdev = to_platform_device(hsotg->dev);
  2331. dev_dbg(hsotg->dev, "pdev 0x%p\n", pdev);
  2332. if (hsotg->plat->phy_init)
  2333. hsotg->plat->phy_init(pdev, hsotg->plat->phy_type);
  2334. }
  2335. /**
  2336. * s3c_hsotg_phy_disable - disable platform phy dev
  2337. * @hsotg: The driver state
  2338. *
  2339. * A wrapper for platform code responsible for controlling
  2340. * low-level USB code
  2341. */
  2342. static void s3c_hsotg_phy_disable(struct s3c_hsotg *hsotg)
  2343. {
  2344. struct platform_device *pdev = to_platform_device(hsotg->dev);
  2345. if (hsotg->plat->phy_exit)
  2346. hsotg->plat->phy_exit(pdev, hsotg->plat->phy_type);
  2347. }
  2348. /**
  2349. * s3c_hsotg_init - initalize the usb core
  2350. * @hsotg: The driver state
  2351. */
  2352. static void s3c_hsotg_init(struct s3c_hsotg *hsotg)
  2353. {
  2354. /* unmask subset of endpoint interrupts */
  2355. writel(DIEPMSK_TimeOUTMsk | DIEPMSK_AHBErrMsk |
  2356. DIEPMSK_EPDisbldMsk | DIEPMSK_XferComplMsk,
  2357. hsotg->regs + DIEPMSK);
  2358. writel(DOEPMSK_SetupMsk | DOEPMSK_AHBErrMsk |
  2359. DOEPMSK_EPDisbldMsk | DOEPMSK_XferComplMsk,
  2360. hsotg->regs + DOEPMSK);
  2361. writel(0, hsotg->regs + DAINTMSK);
  2362. /* Be in disconnected state until gadget is registered */
  2363. __orr32(hsotg->regs + DCTL, DCTL_SftDiscon);
  2364. if (0) {
  2365. /* post global nak until we're ready */
  2366. writel(DCTL_SGNPInNAK | DCTL_SGOUTNak,
  2367. hsotg->regs + DCTL);
  2368. }
  2369. /* setup fifos */
  2370. dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
  2371. readl(hsotg->regs + GRXFSIZ),
  2372. readl(hsotg->regs + GNPTXFSIZ));
  2373. s3c_hsotg_init_fifo(hsotg);
  2374. /* set the PLL on, remove the HNP/SRP and set the PHY */
  2375. writel(GUSBCFG_PHYIf16 | GUSBCFG_TOutCal(7) | (0x5 << 10),
  2376. hsotg->regs + GUSBCFG);
  2377. writel(using_dma(hsotg) ? GAHBCFG_DMAEn : 0x0,
  2378. hsotg->regs + GAHBCFG);
  2379. }
  2380. /**
  2381. * s3c_hsotg_udc_start - prepare the udc for work
  2382. * @gadget: The usb gadget state
  2383. * @driver: The usb gadget driver
  2384. *
  2385. * Perform initialization to prepare udc device and driver
  2386. * to work.
  2387. */
  2388. static int s3c_hsotg_udc_start(struct usb_gadget *gadget,
  2389. struct usb_gadget_driver *driver)
  2390. {
  2391. struct s3c_hsotg *hsotg = to_hsotg(gadget);
  2392. int ret;
  2393. if (!hsotg) {
  2394. printk(KERN_ERR "%s: called with no device\n", __func__);
  2395. return -ENODEV;
  2396. }
  2397. if (!driver) {
  2398. dev_err(hsotg->dev, "%s: no driver\n", __func__);
  2399. return -EINVAL;
  2400. }
  2401. if (driver->max_speed < USB_SPEED_FULL)
  2402. dev_err(hsotg->dev, "%s: bad speed\n", __func__);
  2403. if (!driver->setup) {
  2404. dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
  2405. return -EINVAL;
  2406. }
  2407. WARN_ON(hsotg->driver);
  2408. driver->driver.bus = NULL;
  2409. hsotg->driver = driver;
  2410. hsotg->gadget.dev.driver = &driver->driver;
  2411. hsotg->gadget.dev.of_node = hsotg->dev->of_node;
  2412. hsotg->gadget.dev.dma_mask = hsotg->dev->dma_mask;
  2413. hsotg->gadget.speed = USB_SPEED_UNKNOWN;
  2414. ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
  2415. hsotg->supplies);
  2416. if (ret) {
  2417. dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
  2418. goto err;
  2419. }
  2420. hsotg->last_rst = jiffies;
  2421. dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
  2422. return 0;
  2423. err:
  2424. hsotg->driver = NULL;
  2425. hsotg->gadget.dev.driver = NULL;
  2426. return ret;
  2427. }
  2428. /**
  2429. * s3c_hsotg_udc_stop - stop the udc
  2430. * @gadget: The usb gadget state
  2431. * @driver: The usb gadget driver
  2432. *
  2433. * Stop udc hw block and stay tunned for future transmissions
  2434. */
  2435. static int s3c_hsotg_udc_stop(struct usb_gadget *gadget,
  2436. struct usb_gadget_driver *driver)
  2437. {
  2438. struct s3c_hsotg *hsotg = to_hsotg(gadget);
  2439. unsigned long flags = 0;
  2440. int ep;
  2441. if (!hsotg)
  2442. return -ENODEV;
  2443. if (!driver || driver != hsotg->driver || !driver->unbind)
  2444. return -EINVAL;
  2445. /* all endpoints should be shutdown */
  2446. for (ep = 0; ep < hsotg->num_of_eps; ep++)
  2447. s3c_hsotg_ep_disable(&hsotg->eps[ep].ep);
  2448. spin_lock_irqsave(&hsotg->lock, flags);
  2449. s3c_hsotg_phy_disable(hsotg);
  2450. regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
  2451. hsotg->driver = NULL;
  2452. hsotg->gadget.speed = USB_SPEED_UNKNOWN;
  2453. hsotg->gadget.dev.driver = NULL;
  2454. spin_unlock_irqrestore(&hsotg->lock, flags);
  2455. dev_info(hsotg->dev, "unregistered gadget driver '%s'\n",
  2456. driver->driver.name);
  2457. return 0;
  2458. }
  2459. /**
  2460. * s3c_hsotg_gadget_getframe - read the frame number
  2461. * @gadget: The usb gadget state
  2462. *
  2463. * Read the {micro} frame number
  2464. */
  2465. static int s3c_hsotg_gadget_getframe(struct usb_gadget *gadget)
  2466. {
  2467. return s3c_hsotg_read_frameno(to_hsotg(gadget));
  2468. }
  2469. /**
  2470. * s3c_hsotg_pullup - connect/disconnect the USB PHY
  2471. * @gadget: The usb gadget state
  2472. * @is_on: Current state of the USB PHY
  2473. *
  2474. * Connect/Disconnect the USB PHY pullup
  2475. */
  2476. static int s3c_hsotg_pullup(struct usb_gadget *gadget, int is_on)
  2477. {
  2478. struct s3c_hsotg *hsotg = to_hsotg(gadget);
  2479. unsigned long flags = 0;
  2480. dev_dbg(hsotg->dev, "%s: is_in: %d\n", __func__, is_on);
  2481. spin_lock_irqsave(&hsotg->lock, flags);
  2482. if (is_on) {
  2483. s3c_hsotg_phy_enable(hsotg);
  2484. s3c_hsotg_core_init(hsotg);
  2485. } else {
  2486. s3c_hsotg_disconnect(hsotg);
  2487. s3c_hsotg_phy_disable(hsotg);
  2488. }
  2489. hsotg->gadget.speed = USB_SPEED_UNKNOWN;
  2490. spin_unlock_irqrestore(&hsotg->lock, flags);
  2491. return 0;
  2492. }
  2493. static struct usb_gadget_ops s3c_hsotg_gadget_ops = {
  2494. .get_frame = s3c_hsotg_gadget_getframe,
  2495. .udc_start = s3c_hsotg_udc_start,
  2496. .udc_stop = s3c_hsotg_udc_stop,
  2497. .pullup = s3c_hsotg_pullup,
  2498. };
  2499. /**
  2500. * s3c_hsotg_initep - initialise a single endpoint
  2501. * @hsotg: The device state.
  2502. * @hs_ep: The endpoint to be initialised.
  2503. * @epnum: The endpoint number
  2504. *
  2505. * Initialise the given endpoint (as part of the probe and device state
  2506. * creation) to give to the gadget driver. Setup the endpoint name, any
  2507. * direction information and other state that may be required.
  2508. */
  2509. static void __devinit s3c_hsotg_initep(struct s3c_hsotg *hsotg,
  2510. struct s3c_hsotg_ep *hs_ep,
  2511. int epnum)
  2512. {
  2513. u32 ptxfifo;
  2514. char *dir;
  2515. if (epnum == 0)
  2516. dir = "";
  2517. else if ((epnum % 2) == 0) {
  2518. dir = "out";
  2519. } else {
  2520. dir = "in";
  2521. hs_ep->dir_in = 1;
  2522. }
  2523. hs_ep->index = epnum;
  2524. snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
  2525. INIT_LIST_HEAD(&hs_ep->queue);
  2526. INIT_LIST_HEAD(&hs_ep->ep.ep_list);
  2527. /* add to the list of endpoints known by the gadget driver */
  2528. if (epnum)
  2529. list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
  2530. hs_ep->parent = hsotg;
  2531. hs_ep->ep.name = hs_ep->name;
  2532. hs_ep->ep.maxpacket = epnum ? 512 : EP0_MPS_LIMIT;
  2533. hs_ep->ep.ops = &s3c_hsotg_ep_ops;
  2534. /*
  2535. * Read the FIFO size for the Periodic TX FIFO, even if we're
  2536. * an OUT endpoint, we may as well do this if in future the
  2537. * code is changed to make each endpoint's direction changeable.
  2538. */
  2539. ptxfifo = readl(hsotg->regs + DPTXFSIZn(epnum));
  2540. hs_ep->fifo_size = DPTXFSIZn_DPTxFSize_GET(ptxfifo) * 4;
  2541. /*
  2542. * if we're using dma, we need to set the next-endpoint pointer
  2543. * to be something valid.
  2544. */
  2545. if (using_dma(hsotg)) {
  2546. u32 next = DxEPCTL_NextEp((epnum + 1) % 15);
  2547. writel(next, hsotg->regs + DIEPCTL(epnum));
  2548. writel(next, hsotg->regs + DOEPCTL(epnum));
  2549. }
  2550. }
  2551. /**
  2552. * s3c_hsotg_hw_cfg - read HW configuration registers
  2553. * @param: The device state
  2554. *
  2555. * Read the USB core HW configuration registers
  2556. */
  2557. static void s3c_hsotg_hw_cfg(struct s3c_hsotg *hsotg)
  2558. {
  2559. u32 cfg2, cfg4;
  2560. /* check hardware configuration */
  2561. cfg2 = readl(hsotg->regs + 0x48);
  2562. hsotg->num_of_eps = (cfg2 >> 10) & 0xF;
  2563. dev_info(hsotg->dev, "EPs:%d\n", hsotg->num_of_eps);
  2564. cfg4 = readl(hsotg->regs + 0x50);
  2565. hsotg->dedicated_fifos = (cfg4 >> 25) & 1;
  2566. dev_info(hsotg->dev, "%s fifos\n",
  2567. hsotg->dedicated_fifos ? "dedicated" : "shared");
  2568. }
  2569. /**
  2570. * s3c_hsotg_dump - dump state of the udc
  2571. * @param: The device state
  2572. */
  2573. static void s3c_hsotg_dump(struct s3c_hsotg *hsotg)
  2574. {
  2575. #ifdef DEBUG
  2576. struct device *dev = hsotg->dev;
  2577. void __iomem *regs = hsotg->regs;
  2578. u32 val;
  2579. int idx;
  2580. dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
  2581. readl(regs + DCFG), readl(regs + DCTL),
  2582. readl(regs + DIEPMSK));
  2583. dev_info(dev, "GAHBCFG=0x%08x, 0x44=0x%08x\n",
  2584. readl(regs + GAHBCFG), readl(regs + 0x44));
  2585. dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
  2586. readl(regs + GRXFSIZ), readl(regs + GNPTXFSIZ));
  2587. /* show periodic fifo settings */
  2588. for (idx = 1; idx <= 15; idx++) {
  2589. val = readl(regs + DPTXFSIZn(idx));
  2590. dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
  2591. val >> DPTXFSIZn_DPTxFSize_SHIFT,
  2592. val & DPTXFSIZn_DPTxFStAddr_MASK);
  2593. }
  2594. for (idx = 0; idx < 15; idx++) {
  2595. dev_info(dev,
  2596. "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
  2597. readl(regs + DIEPCTL(idx)),
  2598. readl(regs + DIEPTSIZ(idx)),
  2599. readl(regs + DIEPDMA(idx)));
  2600. val = readl(regs + DOEPCTL(idx));
  2601. dev_info(dev,
  2602. "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
  2603. idx, readl(regs + DOEPCTL(idx)),
  2604. readl(regs + DOEPTSIZ(idx)),
  2605. readl(regs + DOEPDMA(idx)));
  2606. }
  2607. dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
  2608. readl(regs + DVBUSDIS), readl(regs + DVBUSPULSE));
  2609. #endif
  2610. }
  2611. /**
  2612. * state_show - debugfs: show overall driver and device state.
  2613. * @seq: The seq file to write to.
  2614. * @v: Unused parameter.
  2615. *
  2616. * This debugfs entry shows the overall state of the hardware and
  2617. * some general information about each of the endpoints available
  2618. * to the system.
  2619. */
  2620. static int state_show(struct seq_file *seq, void *v)
  2621. {
  2622. struct s3c_hsotg *hsotg = seq->private;
  2623. void __iomem *regs = hsotg->regs;
  2624. int idx;
  2625. seq_printf(seq, "DCFG=0x%08x, DCTL=0x%08x, DSTS=0x%08x\n",
  2626. readl(regs + DCFG),
  2627. readl(regs + DCTL),
  2628. readl(regs + DSTS));
  2629. seq_printf(seq, "DIEPMSK=0x%08x, DOEPMASK=0x%08x\n",
  2630. readl(regs + DIEPMSK), readl(regs + DOEPMSK));
  2631. seq_printf(seq, "GINTMSK=0x%08x, GINTSTS=0x%08x\n",
  2632. readl(regs + GINTMSK),
  2633. readl(regs + GINTSTS));
  2634. seq_printf(seq, "DAINTMSK=0x%08x, DAINT=0x%08x\n",
  2635. readl(regs + DAINTMSK),
  2636. readl(regs + DAINT));
  2637. seq_printf(seq, "GNPTXSTS=0x%08x, GRXSTSR=%08x\n",
  2638. readl(regs + GNPTXSTS),
  2639. readl(regs + GRXSTSR));
  2640. seq_printf(seq, "\nEndpoint status:\n");
  2641. for (idx = 0; idx < 15; idx++) {
  2642. u32 in, out;
  2643. in = readl(regs + DIEPCTL(idx));
  2644. out = readl(regs + DOEPCTL(idx));
  2645. seq_printf(seq, "ep%d: DIEPCTL=0x%08x, DOEPCTL=0x%08x",
  2646. idx, in, out);
  2647. in = readl(regs + DIEPTSIZ(idx));
  2648. out = readl(regs + DOEPTSIZ(idx));
  2649. seq_printf(seq, ", DIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x",
  2650. in, out);
  2651. seq_printf(seq, "\n");
  2652. }
  2653. return 0;
  2654. }
  2655. static int state_open(struct inode *inode, struct file *file)
  2656. {
  2657. return single_open(file, state_show, inode->i_private);
  2658. }
  2659. static const struct file_operations state_fops = {
  2660. .owner = THIS_MODULE,
  2661. .open = state_open,
  2662. .read = seq_read,
  2663. .llseek = seq_lseek,
  2664. .release = single_release,
  2665. };
  2666. /**
  2667. * fifo_show - debugfs: show the fifo information
  2668. * @seq: The seq_file to write data to.
  2669. * @v: Unused parameter.
  2670. *
  2671. * Show the FIFO information for the overall fifo and all the
  2672. * periodic transmission FIFOs.
  2673. */
  2674. static int fifo_show(struct seq_file *seq, void *v)
  2675. {
  2676. struct s3c_hsotg *hsotg = seq->private;
  2677. void __iomem *regs = hsotg->regs;
  2678. u32 val;
  2679. int idx;
  2680. seq_printf(seq, "Non-periodic FIFOs:\n");
  2681. seq_printf(seq, "RXFIFO: Size %d\n", readl(regs + GRXFSIZ));
  2682. val = readl(regs + GNPTXFSIZ);
  2683. seq_printf(seq, "NPTXFIFO: Size %d, Start 0x%08x\n",
  2684. val >> GNPTXFSIZ_NPTxFDep_SHIFT,
  2685. val & GNPTXFSIZ_NPTxFStAddr_MASK);
  2686. seq_printf(seq, "\nPeriodic TXFIFOs:\n");
  2687. for (idx = 1; idx <= 15; idx++) {
  2688. val = readl(regs + DPTXFSIZn(idx));
  2689. seq_printf(seq, "\tDPTXFIFO%2d: Size %d, Start 0x%08x\n", idx,
  2690. val >> DPTXFSIZn_DPTxFSize_SHIFT,
  2691. val & DPTXFSIZn_DPTxFStAddr_MASK);
  2692. }
  2693. return 0;
  2694. }
  2695. static int fifo_open(struct inode *inode, struct file *file)
  2696. {
  2697. return single_open(file, fifo_show, inode->i_private);
  2698. }
  2699. static const struct file_operations fifo_fops = {
  2700. .owner = THIS_MODULE,
  2701. .open = fifo_open,
  2702. .read = seq_read,
  2703. .llseek = seq_lseek,
  2704. .release = single_release,
  2705. };
  2706. static const char *decode_direction(int is_in)
  2707. {
  2708. return is_in ? "in" : "out";
  2709. }
  2710. /**
  2711. * ep_show - debugfs: show the state of an endpoint.
  2712. * @seq: The seq_file to write data to.
  2713. * @v: Unused parameter.
  2714. *
  2715. * This debugfs entry shows the state of the given endpoint (one is
  2716. * registered for each available).
  2717. */
  2718. static int ep_show(struct seq_file *seq, void *v)
  2719. {
  2720. struct s3c_hsotg_ep *ep = seq->private;
  2721. struct s3c_hsotg *hsotg = ep->parent;
  2722. struct s3c_hsotg_req *req;
  2723. void __iomem *regs = hsotg->regs;
  2724. int index = ep->index;
  2725. int show_limit = 15;
  2726. unsigned long flags;
  2727. seq_printf(seq, "Endpoint index %d, named %s, dir %s:\n",
  2728. ep->index, ep->ep.name, decode_direction(ep->dir_in));
  2729. /* first show the register state */
  2730. seq_printf(seq, "\tDIEPCTL=0x%08x, DOEPCTL=0x%08x\n",
  2731. readl(regs + DIEPCTL(index)),
  2732. readl(regs + DOEPCTL(index)));
  2733. seq_printf(seq, "\tDIEPDMA=0x%08x, DOEPDMA=0x%08x\n",
  2734. readl(regs + DIEPDMA(index)),
  2735. readl(regs + DOEPDMA(index)));
  2736. seq_printf(seq, "\tDIEPINT=0x%08x, DOEPINT=0x%08x\n",
  2737. readl(regs + DIEPINT(index)),
  2738. readl(regs + DOEPINT(index)));
  2739. seq_printf(seq, "\tDIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x\n",
  2740. readl(regs + DIEPTSIZ(index)),
  2741. readl(regs + DOEPTSIZ(index)));
  2742. seq_printf(seq, "\n");
  2743. seq_printf(seq, "mps %d\n", ep->ep.maxpacket);
  2744. seq_printf(seq, "total_data=%ld\n", ep->total_data);
  2745. seq_printf(seq, "request list (%p,%p):\n",
  2746. ep->queue.next, ep->queue.prev);
  2747. spin_lock_irqsave(&hsotg->lock, flags);
  2748. list_for_each_entry(req, &ep->queue, queue) {
  2749. if (--show_limit < 0) {
  2750. seq_printf(seq, "not showing more requests...\n");
  2751. break;
  2752. }
  2753. seq_printf(seq, "%c req %p: %d bytes @%p, ",
  2754. req == ep->req ? '*' : ' ',
  2755. req, req->req.length, req->req.buf);
  2756. seq_printf(seq, "%d done, res %d\n",
  2757. req->req.actual, req->req.status);
  2758. }
  2759. spin_unlock_irqrestore(&hsotg->lock, flags);
  2760. return 0;
  2761. }
  2762. static int ep_open(struct inode *inode, struct file *file)
  2763. {
  2764. return single_open(file, ep_show, inode->i_private);
  2765. }
  2766. static const struct file_operations ep_fops = {
  2767. .owner = THIS_MODULE,
  2768. .open = ep_open,
  2769. .read = seq_read,
  2770. .llseek = seq_lseek,
  2771. .release = single_release,
  2772. };
  2773. /**
  2774. * s3c_hsotg_create_debug - create debugfs directory and files
  2775. * @hsotg: The driver state
  2776. *
  2777. * Create the debugfs files to allow the user to get information
  2778. * about the state of the system. The directory name is created
  2779. * with the same name as the device itself, in case we end up
  2780. * with multiple blocks in future systems.
  2781. */
  2782. static void __devinit s3c_hsotg_create_debug(struct s3c_hsotg *hsotg)
  2783. {
  2784. struct dentry *root;
  2785. unsigned epidx;
  2786. root = debugfs_create_dir(dev_name(hsotg->dev), NULL);
  2787. hsotg->debug_root = root;
  2788. if (IS_ERR(root)) {
  2789. dev_err(hsotg->dev, "cannot create debug root\n");
  2790. return;
  2791. }
  2792. /* create general state file */
  2793. hsotg->debug_file = debugfs_create_file("state", 0444, root,
  2794. hsotg, &state_fops);
  2795. if (IS_ERR(hsotg->debug_file))
  2796. dev_err(hsotg->dev, "%s: failed to create state\n", __func__);
  2797. hsotg->debug_fifo = debugfs_create_file("fifo", 0444, root,
  2798. hsotg, &fifo_fops);
  2799. if (IS_ERR(hsotg->debug_fifo))
  2800. dev_err(hsotg->dev, "%s: failed to create fifo\n", __func__);
  2801. /* create one file for each endpoint */
  2802. for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
  2803. struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
  2804. ep->debugfs = debugfs_create_file(ep->name, 0444,
  2805. root, ep, &ep_fops);
  2806. if (IS_ERR(ep->debugfs))
  2807. dev_err(hsotg->dev, "failed to create %s debug file\n",
  2808. ep->name);
  2809. }
  2810. }
  2811. /**
  2812. * s3c_hsotg_delete_debug - cleanup debugfs entries
  2813. * @hsotg: The driver state
  2814. *
  2815. * Cleanup (remove) the debugfs files for use on module exit.
  2816. */
  2817. static void __devexit s3c_hsotg_delete_debug(struct s3c_hsotg *hsotg)
  2818. {
  2819. unsigned epidx;
  2820. for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
  2821. struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
  2822. debugfs_remove(ep->debugfs);
  2823. }
  2824. debugfs_remove(hsotg->debug_file);
  2825. debugfs_remove(hsotg->debug_fifo);
  2826. debugfs_remove(hsotg->debug_root);
  2827. }
  2828. /**
  2829. * s3c_hsotg_release - release callback for hsotg device
  2830. * @dev: Device to for which release is called
  2831. */
  2832. static void s3c_hsotg_release(struct device *dev)
  2833. {
  2834. struct s3c_hsotg *hsotg = dev_get_drvdata(dev);
  2835. kfree(hsotg);
  2836. }
  2837. /**
  2838. * s3c_hsotg_probe - probe function for hsotg driver
  2839. * @pdev: The platform information for the driver
  2840. */
  2841. static int __devinit s3c_hsotg_probe(struct platform_device *pdev)
  2842. {
  2843. struct s3c_hsotg_plat *plat = pdev->dev.platform_data;
  2844. struct device *dev = &pdev->dev;
  2845. struct s3c_hsotg_ep *eps;
  2846. struct s3c_hsotg *hsotg;
  2847. struct resource *res;
  2848. int epnum;
  2849. int ret;
  2850. int i;
  2851. plat = pdev->dev.platform_data;
  2852. if (!plat) {
  2853. dev_err(&pdev->dev, "no platform data defined\n");
  2854. return -EINVAL;
  2855. }
  2856. hsotg = devm_kzalloc(&pdev->dev, sizeof(struct s3c_hsotg), GFP_KERNEL);
  2857. if (!hsotg) {
  2858. dev_err(dev, "cannot get memory\n");
  2859. return -ENOMEM;
  2860. }
  2861. hsotg->dev = dev;
  2862. hsotg->plat = plat;
  2863. hsotg->clk = clk_get(&pdev->dev, "otg");
  2864. if (IS_ERR(hsotg->clk)) {
  2865. dev_err(dev, "cannot get otg clock\n");
  2866. return PTR_ERR(hsotg->clk);
  2867. }
  2868. platform_set_drvdata(pdev, hsotg);
  2869. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2870. hsotg->regs = devm_request_and_ioremap(&pdev->dev, res);
  2871. if (!hsotg->regs) {
  2872. dev_err(dev, "cannot map registers\n");
  2873. ret = -ENXIO;
  2874. goto err_clk;
  2875. }
  2876. ret = platform_get_irq(pdev, 0);
  2877. if (ret < 0) {
  2878. dev_err(dev, "cannot find IRQ\n");
  2879. goto err_clk;
  2880. }
  2881. spin_lock_init(&hsotg->lock);
  2882. hsotg->irq = ret;
  2883. ret = devm_request_irq(&pdev->dev, hsotg->irq, s3c_hsotg_irq, 0,
  2884. dev_name(dev), hsotg);
  2885. if (ret < 0) {
  2886. dev_err(dev, "cannot claim IRQ\n");
  2887. goto err_clk;
  2888. }
  2889. dev_info(dev, "regs %p, irq %d\n", hsotg->regs, hsotg->irq);
  2890. device_initialize(&hsotg->gadget.dev);
  2891. dev_set_name(&hsotg->gadget.dev, "gadget");
  2892. hsotg->gadget.max_speed = USB_SPEED_HIGH;
  2893. hsotg->gadget.ops = &s3c_hsotg_gadget_ops;
  2894. hsotg->gadget.name = dev_name(dev);
  2895. hsotg->gadget.dev.parent = dev;
  2896. hsotg->gadget.dev.dma_mask = dev->dma_mask;
  2897. hsotg->gadget.dev.release = s3c_hsotg_release;
  2898. /* reset the system */
  2899. clk_prepare_enable(hsotg->clk);
  2900. /* regulators */
  2901. for (i = 0; i < ARRAY_SIZE(hsotg->supplies); i++)
  2902. hsotg->supplies[i].supply = s3c_hsotg_supply_names[i];
  2903. ret = regulator_bulk_get(dev, ARRAY_SIZE(hsotg->supplies),
  2904. hsotg->supplies);
  2905. if (ret) {
  2906. dev_err(dev, "failed to request supplies: %d\n", ret);
  2907. goto err_clk;
  2908. }
  2909. ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
  2910. hsotg->supplies);
  2911. if (ret) {
  2912. dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
  2913. goto err_supplies;
  2914. }
  2915. /* usb phy enable */
  2916. s3c_hsotg_phy_enable(hsotg);
  2917. s3c_hsotg_corereset(hsotg);
  2918. s3c_hsotg_init(hsotg);
  2919. s3c_hsotg_hw_cfg(hsotg);
  2920. /* hsotg->num_of_eps holds number of EPs other than ep0 */
  2921. if (hsotg->num_of_eps == 0) {
  2922. dev_err(dev, "wrong number of EPs (zero)\n");
  2923. goto err_supplies;
  2924. }
  2925. eps = kcalloc(hsotg->num_of_eps + 1, sizeof(struct s3c_hsotg_ep),
  2926. GFP_KERNEL);
  2927. if (!eps) {
  2928. dev_err(dev, "cannot get memory\n");
  2929. goto err_supplies;
  2930. }
  2931. hsotg->eps = eps;
  2932. /* setup endpoint information */
  2933. INIT_LIST_HEAD(&hsotg->gadget.ep_list);
  2934. hsotg->gadget.ep0 = &hsotg->eps[0].ep;
  2935. /* allocate EP0 request */
  2936. hsotg->ctrl_req = s3c_hsotg_ep_alloc_request(&hsotg->eps[0].ep,
  2937. GFP_KERNEL);
  2938. if (!hsotg->ctrl_req) {
  2939. dev_err(dev, "failed to allocate ctrl req\n");
  2940. goto err_ep_mem;
  2941. }
  2942. /* initialise the endpoints now the core has been initialised */
  2943. for (epnum = 0; epnum < hsotg->num_of_eps; epnum++)
  2944. s3c_hsotg_initep(hsotg, &hsotg->eps[epnum], epnum);
  2945. /* disable power and clock */
  2946. ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
  2947. hsotg->supplies);
  2948. if (ret) {
  2949. dev_err(hsotg->dev, "failed to disable supplies: %d\n", ret);
  2950. goto err_ep_mem;
  2951. }
  2952. s3c_hsotg_phy_disable(hsotg);
  2953. ret = device_add(&hsotg->gadget.dev);
  2954. if (ret) {
  2955. put_device(&hsotg->gadget.dev);
  2956. goto err_ep_mem;
  2957. }
  2958. ret = usb_add_gadget_udc(&pdev->dev, &hsotg->gadget);
  2959. if (ret)
  2960. goto err_ep_mem;
  2961. s3c_hsotg_create_debug(hsotg);
  2962. s3c_hsotg_dump(hsotg);
  2963. return 0;
  2964. err_ep_mem:
  2965. kfree(eps);
  2966. err_supplies:
  2967. s3c_hsotg_phy_disable(hsotg);
  2968. regulator_bulk_free(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
  2969. err_clk:
  2970. clk_disable_unprepare(hsotg->clk);
  2971. clk_put(hsotg->clk);
  2972. return ret;
  2973. }
  2974. /**
  2975. * s3c_hsotg_remove - remove function for hsotg driver
  2976. * @pdev: The platform information for the driver
  2977. */
  2978. static int __devexit s3c_hsotg_remove(struct platform_device *pdev)
  2979. {
  2980. struct s3c_hsotg *hsotg = platform_get_drvdata(pdev);
  2981. usb_del_gadget_udc(&hsotg->gadget);
  2982. s3c_hsotg_delete_debug(hsotg);
  2983. if (hsotg->driver) {
  2984. /* should have been done already by driver model core */
  2985. usb_gadget_unregister_driver(hsotg->driver);
  2986. }
  2987. s3c_hsotg_phy_disable(hsotg);
  2988. regulator_bulk_free(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
  2989. clk_disable_unprepare(hsotg->clk);
  2990. clk_put(hsotg->clk);
  2991. device_unregister(&hsotg->gadget.dev);
  2992. return 0;
  2993. }
  2994. #if 1
  2995. #define s3c_hsotg_suspend NULL
  2996. #define s3c_hsotg_resume NULL
  2997. #endif
  2998. static struct platform_driver s3c_hsotg_driver = {
  2999. .driver = {
  3000. .name = "s3c-hsotg",
  3001. .owner = THIS_MODULE,
  3002. },
  3003. .probe = s3c_hsotg_probe,
  3004. .remove = __devexit_p(s3c_hsotg_remove),
  3005. .suspend = s3c_hsotg_suspend,
  3006. .resume = s3c_hsotg_resume,
  3007. };
  3008. module_platform_driver(s3c_hsotg_driver);
  3009. MODULE_DESCRIPTION("Samsung S3C USB High-speed/OtG device");
  3010. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  3011. MODULE_LICENSE("GPL");
  3012. MODULE_ALIAS("platform:s3c-hsotg");