s3c-hsotg.c 87 KB

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