s3c-hsotg.c 92 KB

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