s3c-hsotg.c 92 KB

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