s3c-hsotg.c 92 KB

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