s3c-hsotg.c 90 KB

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