s3c-hsotg.c 95 KB

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