s3c-hsotg.c 94 KB

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