s3c-hsotg.c 94 KB

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