s3c-hsotg.c 94 KB

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