s3c-hsotg.c 87 KB

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