s3c-hsotg.c 87 KB

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