gadget.c 91 KB

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