s3c-hsotg.c 92 KB

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