s3c-hsotg.c 90 KB

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