s3c-hsotg.c 94 KB

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