lpc32xx_udc.c 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538
  1. /*
  2. * USB Gadget driver for LPC32xx
  3. *
  4. * Authors:
  5. * Kevin Wells <kevin.wells@nxp.com>
  6. * Mike James
  7. * Roland Stigge <stigge@antcom.de>
  8. *
  9. * Copyright (C) 2006 Philips Semiconductors
  10. * Copyright (C) 2009 NXP Semiconductors
  11. * Copyright (C) 2012 Roland Stigge
  12. *
  13. * Note: This driver is based on original work done by Mike James for
  14. * the LPC3180.
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/delay.h>
  34. #include <linux/ioport.h>
  35. #include <linux/slab.h>
  36. #include <linux/errno.h>
  37. #include <linux/init.h>
  38. #include <linux/list.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/proc_fs.h>
  41. #include <linux/clk.h>
  42. #include <linux/usb/ch9.h>
  43. #include <linux/usb/gadget.h>
  44. #include <linux/i2c.h>
  45. #include <linux/kthread.h>
  46. #include <linux/freezer.h>
  47. #include <linux/dma-mapping.h>
  48. #include <linux/dmapool.h>
  49. #include <linux/workqueue.h>
  50. #include <linux/of.h>
  51. #include <linux/usb/isp1301.h>
  52. #include <asm/byteorder.h>
  53. #include <mach/hardware.h>
  54. #include <linux/io.h>
  55. #include <asm/irq.h>
  56. #include <asm/system.h>
  57. #include <mach/platform.h>
  58. #include <mach/irqs.h>
  59. #include <mach/board.h>
  60. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  61. #include <linux/seq_file.h>
  62. #endif
  63. /*
  64. * USB device configuration structure
  65. */
  66. typedef void (*usc_chg_event)(int);
  67. struct lpc32xx_usbd_cfg {
  68. int vbus_drv_pol; /* 0=active low drive for VBUS via ISP1301 */
  69. usc_chg_event conn_chgb; /* Connection change event (optional) */
  70. usc_chg_event susp_chgb; /* Suspend/resume event (optional) */
  71. usc_chg_event rmwk_chgb; /* Enable/disable remote wakeup */
  72. };
  73. /*
  74. * controller driver data structures
  75. */
  76. /* 16 endpoints (not to be confused with 32 hardware endpoints) */
  77. #define NUM_ENDPOINTS 16
  78. /*
  79. * IRQ indices make reading the code a little easier
  80. */
  81. #define IRQ_USB_LP 0
  82. #define IRQ_USB_HP 1
  83. #define IRQ_USB_DEVDMA 2
  84. #define IRQ_USB_ATX 3
  85. #define EP_OUT 0 /* RX (from host) */
  86. #define EP_IN 1 /* TX (to host) */
  87. /* Returns the interrupt mask for the selected hardware endpoint */
  88. #define EP_MASK_SEL(ep, dir) (1 << (((ep) * 2) + dir))
  89. #define EP_INT_TYPE 0
  90. #define EP_ISO_TYPE 1
  91. #define EP_BLK_TYPE 2
  92. #define EP_CTL_TYPE 3
  93. /* EP0 states */
  94. #define WAIT_FOR_SETUP 0 /* Wait for setup packet */
  95. #define DATA_IN 1 /* Expect dev->host transfer */
  96. #define DATA_OUT 2 /* Expect host->dev transfer */
  97. /* DD (DMA Descriptor) structure, requires word alignment, this is already
  98. * defined in the LPC32XX USB device header file, but this version is slightly
  99. * modified to tag some work data with each DMA descriptor. */
  100. struct lpc32xx_usbd_dd_gad {
  101. u32 dd_next_phy;
  102. u32 dd_setup;
  103. u32 dd_buffer_addr;
  104. u32 dd_status;
  105. u32 dd_iso_ps_mem_addr;
  106. u32 this_dma;
  107. u32 iso_status[6]; /* 5 spare */
  108. u32 dd_next_v;
  109. };
  110. /*
  111. * Logical endpoint structure
  112. */
  113. struct lpc32xx_ep {
  114. struct usb_ep ep;
  115. struct list_head queue;
  116. struct lpc32xx_udc *udc;
  117. u32 hwep_num_base; /* Physical hardware EP */
  118. u32 hwep_num; /* Maps to hardware endpoint */
  119. u32 maxpacket;
  120. u32 lep;
  121. bool is_in;
  122. bool req_pending;
  123. u32 eptype;
  124. u32 totalints;
  125. bool wedge;
  126. const struct usb_endpoint_descriptor *desc;
  127. };
  128. /*
  129. * Common UDC structure
  130. */
  131. struct lpc32xx_udc {
  132. struct usb_gadget gadget;
  133. struct usb_gadget_driver *driver;
  134. struct platform_device *pdev;
  135. struct device *dev;
  136. struct dentry *pde;
  137. spinlock_t lock;
  138. struct i2c_client *isp1301_i2c_client;
  139. /* Board and device specific */
  140. struct lpc32xx_usbd_cfg *board;
  141. u32 io_p_start;
  142. u32 io_p_size;
  143. void __iomem *udp_baseaddr;
  144. int udp_irq[4];
  145. struct clk *usb_pll_clk;
  146. struct clk *usb_slv_clk;
  147. /* DMA support */
  148. u32 *udca_v_base;
  149. u32 udca_p_base;
  150. struct dma_pool *dd_cache;
  151. /* Common EP and control data */
  152. u32 enabled_devints;
  153. u32 enabled_hwepints;
  154. u32 dev_status;
  155. u32 realized_eps;
  156. /* VBUS detection, pullup, and power flags */
  157. u8 vbus;
  158. u8 last_vbus;
  159. int pullup;
  160. int poweron;
  161. /* Work queues related to I2C support */
  162. struct work_struct pullup_job;
  163. struct work_struct vbus_job;
  164. struct work_struct power_job;
  165. /* USB device peripheral - various */
  166. struct lpc32xx_ep ep[NUM_ENDPOINTS];
  167. bool enabled;
  168. bool clocked;
  169. bool suspended;
  170. bool selfpowered;
  171. int ep0state;
  172. atomic_t enabled_ep_cnt;
  173. wait_queue_head_t ep_disable_wait_queue;
  174. };
  175. /*
  176. * Endpoint request
  177. */
  178. struct lpc32xx_request {
  179. struct usb_request req;
  180. struct list_head queue;
  181. struct lpc32xx_usbd_dd_gad *dd_desc_ptr;
  182. bool mapped;
  183. bool send_zlp;
  184. };
  185. static inline struct lpc32xx_udc *to_udc(struct usb_gadget *g)
  186. {
  187. return container_of(g, struct lpc32xx_udc, gadget);
  188. }
  189. #define ep_dbg(epp, fmt, arg...) \
  190. dev_dbg(epp->udc->dev, "%s: " fmt, __func__, ## arg)
  191. #define ep_err(epp, fmt, arg...) \
  192. dev_err(epp->udc->dev, "%s: " fmt, __func__, ## arg)
  193. #define ep_info(epp, fmt, arg...) \
  194. dev_info(epp->udc->dev, "%s: " fmt, __func__, ## arg)
  195. #define ep_warn(epp, fmt, arg...) \
  196. dev_warn(epp->udc->dev, "%s:" fmt, __func__, ## arg)
  197. #define UDCA_BUFF_SIZE (128)
  198. /* TODO: When the clock framework is introduced in LPC32xx, IO_ADDRESS will
  199. * be replaced with an inremap()ed pointer, see USB_OTG_CLK_CTRL()
  200. * */
  201. #define USB_CTRL IO_ADDRESS(LPC32XX_CLK_PM_BASE + 0x64)
  202. #define USB_CLOCK_MASK (AHB_M_CLOCK_ON | OTG_CLOCK_ON | \
  203. DEV_CLOCK_ON | I2C_CLOCK_ON)
  204. /* USB_CTRL bit defines */
  205. #define USB_SLAVE_HCLK_EN (1 << 24)
  206. #define USB_HOST_NEED_CLK_EN (1 << 21)
  207. #define USB_DEV_NEED_CLK_EN (1 << 22)
  208. #define USB_OTG_CLK_CTRL(udc) ((udc)->udp_baseaddr + 0xFF4)
  209. #define USB_OTG_CLK_STAT(udc) ((udc)->udp_baseaddr + 0xFF8)
  210. /* USB_OTG_CLK_CTRL bit defines */
  211. #define AHB_M_CLOCK_ON (1 << 4)
  212. #define OTG_CLOCK_ON (1 << 3)
  213. #define I2C_CLOCK_ON (1 << 2)
  214. #define DEV_CLOCK_ON (1 << 1)
  215. #define HOST_CLOCK_ON (1 << 0)
  216. #define USB_OTG_STAT_CONTROL(udc) (udc->udp_baseaddr + 0x110)
  217. /* USB_OTG_STAT_CONTROL bit defines */
  218. #define TRANSPARENT_I2C_EN (1 << 7)
  219. #define HOST_EN (1 << 0)
  220. /**********************************************************************
  221. * USB device controller register offsets
  222. **********************************************************************/
  223. #define USBD_DEVINTST(x) ((x) + 0x200)
  224. #define USBD_DEVINTEN(x) ((x) + 0x204)
  225. #define USBD_DEVINTCLR(x) ((x) + 0x208)
  226. #define USBD_DEVINTSET(x) ((x) + 0x20C)
  227. #define USBD_CMDCODE(x) ((x) + 0x210)
  228. #define USBD_CMDDATA(x) ((x) + 0x214)
  229. #define USBD_RXDATA(x) ((x) + 0x218)
  230. #define USBD_TXDATA(x) ((x) + 0x21C)
  231. #define USBD_RXPLEN(x) ((x) + 0x220)
  232. #define USBD_TXPLEN(x) ((x) + 0x224)
  233. #define USBD_CTRL(x) ((x) + 0x228)
  234. #define USBD_DEVINTPRI(x) ((x) + 0x22C)
  235. #define USBD_EPINTST(x) ((x) + 0x230)
  236. #define USBD_EPINTEN(x) ((x) + 0x234)
  237. #define USBD_EPINTCLR(x) ((x) + 0x238)
  238. #define USBD_EPINTSET(x) ((x) + 0x23C)
  239. #define USBD_EPINTPRI(x) ((x) + 0x240)
  240. #define USBD_REEP(x) ((x) + 0x244)
  241. #define USBD_EPIND(x) ((x) + 0x248)
  242. #define USBD_EPMAXPSIZE(x) ((x) + 0x24C)
  243. /* DMA support registers only below */
  244. /* Set, clear, or get enabled state of the DMA request status. If
  245. * enabled, an IN or OUT token will start a DMA transfer for the EP */
  246. #define USBD_DMARST(x) ((x) + 0x250)
  247. #define USBD_DMARCLR(x) ((x) + 0x254)
  248. #define USBD_DMARSET(x) ((x) + 0x258)
  249. /* DMA UDCA head pointer */
  250. #define USBD_UDCAH(x) ((x) + 0x280)
  251. /* EP DMA status, enable, and disable. This is used to specifically
  252. * enabled or disable DMA for a specific EP */
  253. #define USBD_EPDMAST(x) ((x) + 0x284)
  254. #define USBD_EPDMAEN(x) ((x) + 0x288)
  255. #define USBD_EPDMADIS(x) ((x) + 0x28C)
  256. /* DMA master interrupts enable and pending interrupts */
  257. #define USBD_DMAINTST(x) ((x) + 0x290)
  258. #define USBD_DMAINTEN(x) ((x) + 0x294)
  259. /* DMA end of transfer interrupt enable, disable, status */
  260. #define USBD_EOTINTST(x) ((x) + 0x2A0)
  261. #define USBD_EOTINTCLR(x) ((x) + 0x2A4)
  262. #define USBD_EOTINTSET(x) ((x) + 0x2A8)
  263. /* New DD request interrupt enable, disable, status */
  264. #define USBD_NDDRTINTST(x) ((x) + 0x2AC)
  265. #define USBD_NDDRTINTCLR(x) ((x) + 0x2B0)
  266. #define USBD_NDDRTINTSET(x) ((x) + 0x2B4)
  267. /* DMA error interrupt enable, disable, status */
  268. #define USBD_SYSERRTINTST(x) ((x) + 0x2B8)
  269. #define USBD_SYSERRTINTCLR(x) ((x) + 0x2BC)
  270. #define USBD_SYSERRTINTSET(x) ((x) + 0x2C0)
  271. /**********************************************************************
  272. * USBD_DEVINTST/USBD_DEVINTEN/USBD_DEVINTCLR/USBD_DEVINTSET/
  273. * USBD_DEVINTPRI register definitions
  274. **********************************************************************/
  275. #define USBD_ERR_INT (1 << 9)
  276. #define USBD_EP_RLZED (1 << 8)
  277. #define USBD_TXENDPKT (1 << 7)
  278. #define USBD_RXENDPKT (1 << 6)
  279. #define USBD_CDFULL (1 << 5)
  280. #define USBD_CCEMPTY (1 << 4)
  281. #define USBD_DEV_STAT (1 << 3)
  282. #define USBD_EP_SLOW (1 << 2)
  283. #define USBD_EP_FAST (1 << 1)
  284. #define USBD_FRAME (1 << 0)
  285. /**********************************************************************
  286. * USBD_EPINTST/USBD_EPINTEN/USBD_EPINTCLR/USBD_EPINTSET/
  287. * USBD_EPINTPRI register definitions
  288. **********************************************************************/
  289. /* End point selection macro (RX) */
  290. #define USBD_RX_EP_SEL(e) (1 << ((e) << 1))
  291. /* End point selection macro (TX) */
  292. #define USBD_TX_EP_SEL(e) (1 << (((e) << 1) + 1))
  293. /**********************************************************************
  294. * USBD_REEP/USBD_DMARST/USBD_DMARCLR/USBD_DMARSET/USBD_EPDMAST/
  295. * USBD_EPDMAEN/USBD_EPDMADIS/
  296. * USBD_NDDRTINTST/USBD_NDDRTINTCLR/USBD_NDDRTINTSET/
  297. * USBD_EOTINTST/USBD_EOTINTCLR/USBD_EOTINTSET/
  298. * USBD_SYSERRTINTST/USBD_SYSERRTINTCLR/USBD_SYSERRTINTSET
  299. * register definitions
  300. **********************************************************************/
  301. /* Endpoint selection macro */
  302. #define USBD_EP_SEL(e) (1 << (e))
  303. /**********************************************************************
  304. * SBD_DMAINTST/USBD_DMAINTEN
  305. **********************************************************************/
  306. #define USBD_SYS_ERR_INT (1 << 2)
  307. #define USBD_NEW_DD_INT (1 << 1)
  308. #define USBD_EOT_INT (1 << 0)
  309. /**********************************************************************
  310. * USBD_RXPLEN register definitions
  311. **********************************************************************/
  312. #define USBD_PKT_RDY (1 << 11)
  313. #define USBD_DV (1 << 10)
  314. #define USBD_PK_LEN_MASK 0x3FF
  315. /**********************************************************************
  316. * USBD_CTRL register definitions
  317. **********************************************************************/
  318. #define USBD_LOG_ENDPOINT(e) ((e) << 2)
  319. #define USBD_WR_EN (1 << 1)
  320. #define USBD_RD_EN (1 << 0)
  321. /**********************************************************************
  322. * USBD_CMDCODE register definitions
  323. **********************************************************************/
  324. #define USBD_CMD_CODE(c) ((c) << 16)
  325. #define USBD_CMD_PHASE(p) ((p) << 8)
  326. /**********************************************************************
  327. * USBD_DMARST/USBD_DMARCLR/USBD_DMARSET register definitions
  328. **********************************************************************/
  329. #define USBD_DMAEP(e) (1 << (e))
  330. /* DD (DMA Descriptor) structure, requires word alignment */
  331. struct lpc32xx_usbd_dd {
  332. u32 *dd_next;
  333. u32 dd_setup;
  334. u32 dd_buffer_addr;
  335. u32 dd_status;
  336. u32 dd_iso_ps_mem_addr;
  337. };
  338. /* dd_setup bit defines */
  339. #define DD_SETUP_ATLE_DMA_MODE 0x01
  340. #define DD_SETUP_NEXT_DD_VALID 0x04
  341. #define DD_SETUP_ISO_EP 0x10
  342. #define DD_SETUP_PACKETLEN(n) (((n) & 0x7FF) << 5)
  343. #define DD_SETUP_DMALENBYTES(n) (((n) & 0xFFFF) << 16)
  344. /* dd_status bit defines */
  345. #define DD_STATUS_DD_RETIRED 0x01
  346. #define DD_STATUS_STS_MASK 0x1E
  347. #define DD_STATUS_STS_NS 0x00 /* Not serviced */
  348. #define DD_STATUS_STS_BS 0x02 /* Being serviced */
  349. #define DD_STATUS_STS_NC 0x04 /* Normal completion */
  350. #define DD_STATUS_STS_DUR 0x06 /* Data underrun (short packet) */
  351. #define DD_STATUS_STS_DOR 0x08 /* Data overrun */
  352. #define DD_STATUS_STS_SE 0x12 /* System error */
  353. #define DD_STATUS_PKT_VAL 0x20 /* Packet valid */
  354. #define DD_STATUS_LSB_EX 0x40 /* LS byte extracted (ATLE) */
  355. #define DD_STATUS_MSB_EX 0x80 /* MS byte extracted (ATLE) */
  356. #define DD_STATUS_MLEN(n) (((n) >> 8) & 0x3F)
  357. #define DD_STATUS_CURDMACNT(n) (((n) >> 16) & 0xFFFF)
  358. /*
  359. *
  360. * Protocol engine bits below
  361. *
  362. */
  363. /* Device Interrupt Bit Definitions */
  364. #define FRAME_INT 0x00000001
  365. #define EP_FAST_INT 0x00000002
  366. #define EP_SLOW_INT 0x00000004
  367. #define DEV_STAT_INT 0x00000008
  368. #define CCEMTY_INT 0x00000010
  369. #define CDFULL_INT 0x00000020
  370. #define RxENDPKT_INT 0x00000040
  371. #define TxENDPKT_INT 0x00000080
  372. #define EP_RLZED_INT 0x00000100
  373. #define ERR_INT 0x00000200
  374. /* Rx & Tx Packet Length Definitions */
  375. #define PKT_LNGTH_MASK 0x000003FF
  376. #define PKT_DV 0x00000400
  377. #define PKT_RDY 0x00000800
  378. /* USB Control Definitions */
  379. #define CTRL_RD_EN 0x00000001
  380. #define CTRL_WR_EN 0x00000002
  381. /* Command Codes */
  382. #define CMD_SET_ADDR 0x00D00500
  383. #define CMD_CFG_DEV 0x00D80500
  384. #define CMD_SET_MODE 0x00F30500
  385. #define CMD_RD_FRAME 0x00F50500
  386. #define DAT_RD_FRAME 0x00F50200
  387. #define CMD_RD_TEST 0x00FD0500
  388. #define DAT_RD_TEST 0x00FD0200
  389. #define CMD_SET_DEV_STAT 0x00FE0500
  390. #define CMD_GET_DEV_STAT 0x00FE0500
  391. #define DAT_GET_DEV_STAT 0x00FE0200
  392. #define CMD_GET_ERR_CODE 0x00FF0500
  393. #define DAT_GET_ERR_CODE 0x00FF0200
  394. #define CMD_RD_ERR_STAT 0x00FB0500
  395. #define DAT_RD_ERR_STAT 0x00FB0200
  396. #define DAT_WR_BYTE(x) (0x00000100 | ((x) << 16))
  397. #define CMD_SEL_EP(x) (0x00000500 | ((x) << 16))
  398. #define DAT_SEL_EP(x) (0x00000200 | ((x) << 16))
  399. #define CMD_SEL_EP_CLRI(x) (0x00400500 | ((x) << 16))
  400. #define DAT_SEL_EP_CLRI(x) (0x00400200 | ((x) << 16))
  401. #define CMD_SET_EP_STAT(x) (0x00400500 | ((x) << 16))
  402. #define CMD_CLR_BUF 0x00F20500
  403. #define DAT_CLR_BUF 0x00F20200
  404. #define CMD_VALID_BUF 0x00FA0500
  405. /* Device Address Register Definitions */
  406. #define DEV_ADDR_MASK 0x7F
  407. #define DEV_EN 0x80
  408. /* Device Configure Register Definitions */
  409. #define CONF_DVICE 0x01
  410. /* Device Mode Register Definitions */
  411. #define AP_CLK 0x01
  412. #define INAK_CI 0x02
  413. #define INAK_CO 0x04
  414. #define INAK_II 0x08
  415. #define INAK_IO 0x10
  416. #define INAK_BI 0x20
  417. #define INAK_BO 0x40
  418. /* Device Status Register Definitions */
  419. #define DEV_CON 0x01
  420. #define DEV_CON_CH 0x02
  421. #define DEV_SUS 0x04
  422. #define DEV_SUS_CH 0x08
  423. #define DEV_RST 0x10
  424. /* Error Code Register Definitions */
  425. #define ERR_EC_MASK 0x0F
  426. #define ERR_EA 0x10
  427. /* Error Status Register Definitions */
  428. #define ERR_PID 0x01
  429. #define ERR_UEPKT 0x02
  430. #define ERR_DCRC 0x04
  431. #define ERR_TIMOUT 0x08
  432. #define ERR_EOP 0x10
  433. #define ERR_B_OVRN 0x20
  434. #define ERR_BTSTF 0x40
  435. #define ERR_TGL 0x80
  436. /* Endpoint Select Register Definitions */
  437. #define EP_SEL_F 0x01
  438. #define EP_SEL_ST 0x02
  439. #define EP_SEL_STP 0x04
  440. #define EP_SEL_PO 0x08
  441. #define EP_SEL_EPN 0x10
  442. #define EP_SEL_B_1_FULL 0x20
  443. #define EP_SEL_B_2_FULL 0x40
  444. /* Endpoint Status Register Definitions */
  445. #define EP_STAT_ST 0x01
  446. #define EP_STAT_DA 0x20
  447. #define EP_STAT_RF_MO 0x40
  448. #define EP_STAT_CND_ST 0x80
  449. /* Clear Buffer Register Definitions */
  450. #define CLR_BUF_PO 0x01
  451. /* DMA Interrupt Bit Definitions */
  452. #define EOT_INT 0x01
  453. #define NDD_REQ_INT 0x02
  454. #define SYS_ERR_INT 0x04
  455. #define DRIVER_VERSION "1.03"
  456. static const char driver_name[] = "lpc32xx_udc";
  457. /*
  458. *
  459. * proc interface support
  460. *
  461. */
  462. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  463. static char *epnames[] = {"INT", "ISO", "BULK", "CTRL"};
  464. static const char debug_filename[] = "driver/udc";
  465. static void proc_ep_show(struct seq_file *s, struct lpc32xx_ep *ep)
  466. {
  467. struct lpc32xx_request *req;
  468. seq_printf(s, "\n");
  469. seq_printf(s, "%12s, maxpacket %4d %3s",
  470. ep->ep.name, ep->ep.maxpacket,
  471. ep->is_in ? "in" : "out");
  472. seq_printf(s, " type %4s", epnames[ep->eptype]);
  473. seq_printf(s, " ints: %12d", ep->totalints);
  474. if (list_empty(&ep->queue))
  475. seq_printf(s, "\t(queue empty)\n");
  476. else {
  477. list_for_each_entry(req, &ep->queue, queue) {
  478. u32 length = req->req.actual;
  479. seq_printf(s, "\treq %p len %d/%d buf %p\n",
  480. &req->req, length,
  481. req->req.length, req->req.buf);
  482. }
  483. }
  484. }
  485. static int proc_udc_show(struct seq_file *s, void *unused)
  486. {
  487. struct lpc32xx_udc *udc = s->private;
  488. struct lpc32xx_ep *ep;
  489. unsigned long flags;
  490. seq_printf(s, "%s: version %s\n", driver_name, DRIVER_VERSION);
  491. spin_lock_irqsave(&udc->lock, flags);
  492. seq_printf(s, "vbus %s, pullup %s, %s powered%s, gadget %s\n\n",
  493. udc->vbus ? "present" : "off",
  494. udc->enabled ? (udc->vbus ? "active" : "enabled") :
  495. "disabled",
  496. udc->selfpowered ? "self" : "VBUS",
  497. udc->suspended ? ", suspended" : "",
  498. udc->driver ? udc->driver->driver.name : "(none)");
  499. if (udc->enabled && udc->vbus) {
  500. proc_ep_show(s, &udc->ep[0]);
  501. list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
  502. if (ep->desc)
  503. proc_ep_show(s, ep);
  504. }
  505. }
  506. spin_unlock_irqrestore(&udc->lock, flags);
  507. return 0;
  508. }
  509. static int proc_udc_open(struct inode *inode, struct file *file)
  510. {
  511. return single_open(file, proc_udc_show, PDE(inode)->data);
  512. }
  513. static const struct file_operations proc_ops = {
  514. .owner = THIS_MODULE,
  515. .open = proc_udc_open,
  516. .read = seq_read,
  517. .llseek = seq_lseek,
  518. .release = single_release,
  519. };
  520. static void create_debug_file(struct lpc32xx_udc *udc)
  521. {
  522. udc->pde = debugfs_create_file(debug_filename, 0, NULL, udc, &proc_ops);
  523. }
  524. static void remove_debug_file(struct lpc32xx_udc *udc)
  525. {
  526. if (udc->pde)
  527. debugfs_remove(udc->pde);
  528. }
  529. #else
  530. static inline void create_debug_file(struct lpc32xx_udc *udc) {}
  531. static inline void remove_debug_file(struct lpc32xx_udc *udc) {}
  532. #endif
  533. /* Primary initialization sequence for the ISP1301 transceiver */
  534. static void isp1301_udc_configure(struct lpc32xx_udc *udc)
  535. {
  536. /* LPC32XX only supports DAT_SE0 USB mode */
  537. /* This sequence is important */
  538. /* Disable transparent UART mode first */
  539. i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
  540. (ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
  541. MC1_UART_EN);
  542. /* Set full speed and SE0 mode */
  543. i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
  544. (ISP1301_I2C_MODE_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR), ~0);
  545. i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
  546. ISP1301_I2C_MODE_CONTROL_1, (MC1_SPEED_REG | MC1_DAT_SE0));
  547. /*
  548. * The PSW_OE enable bit state is reversed in the ISP1301 User's Guide
  549. */
  550. i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
  551. (ISP1301_I2C_MODE_CONTROL_2 | ISP1301_I2C_REG_CLEAR_ADDR), ~0);
  552. i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
  553. ISP1301_I2C_MODE_CONTROL_2, (MC2_BI_DI | MC2_SPD_SUSP_CTRL));
  554. /* Driver VBUS_DRV high or low depending on board setup */
  555. if (udc->board->vbus_drv_pol != 0)
  556. i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
  557. ISP1301_I2C_OTG_CONTROL_1, OTG1_VBUS_DRV);
  558. else
  559. i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
  560. ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
  561. OTG1_VBUS_DRV);
  562. /* Bi-directional mode with suspend control
  563. * Enable both pulldowns for now - the pullup will be enable when VBUS
  564. * is detected */
  565. i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
  566. (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR), ~0);
  567. i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
  568. ISP1301_I2C_OTG_CONTROL_1,
  569. (0 | OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN));
  570. /* Discharge VBUS (just in case) */
  571. i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
  572. ISP1301_I2C_OTG_CONTROL_1, OTG1_VBUS_DISCHRG);
  573. msleep(1);
  574. i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
  575. (ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR),
  576. OTG1_VBUS_DISCHRG);
  577. /* Clear and enable VBUS high edge interrupt */
  578. i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
  579. ISP1301_I2C_INTERRUPT_LATCH | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
  580. i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
  581. ISP1301_I2C_INTERRUPT_FALLING | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
  582. i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
  583. ISP1301_I2C_INTERRUPT_FALLING, INT_VBUS_VLD);
  584. i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
  585. ISP1301_I2C_INTERRUPT_RISING | ISP1301_I2C_REG_CLEAR_ADDR, ~0);
  586. i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
  587. ISP1301_I2C_INTERRUPT_RISING, INT_VBUS_VLD);
  588. /* Enable usb_need_clk clock after transceiver is initialized */
  589. writel((readl(USB_CTRL) | (1 << 22)), USB_CTRL);
  590. dev_info(udc->dev, "ISP1301 Vendor ID : 0x%04x\n",
  591. i2c_smbus_read_word_data(udc->isp1301_i2c_client, 0x00));
  592. dev_info(udc->dev, "ISP1301 Product ID : 0x%04x\n",
  593. i2c_smbus_read_word_data(udc->isp1301_i2c_client, 0x02));
  594. dev_info(udc->dev, "ISP1301 Version ID : 0x%04x\n",
  595. i2c_smbus_read_word_data(udc->isp1301_i2c_client, 0x14));
  596. }
  597. /* Enables or disables the USB device pullup via the ISP1301 transceiver */
  598. static void isp1301_pullup_set(struct lpc32xx_udc *udc)
  599. {
  600. if (udc->pullup)
  601. /* Enable pullup for bus signalling */
  602. i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
  603. ISP1301_I2C_OTG_CONTROL_1, OTG1_DP_PULLUP);
  604. else
  605. /* Enable pullup for bus signalling */
  606. i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
  607. ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
  608. OTG1_DP_PULLUP);
  609. }
  610. static void pullup_work(struct work_struct *work)
  611. {
  612. struct lpc32xx_udc *udc =
  613. container_of(work, struct lpc32xx_udc, pullup_job);
  614. isp1301_pullup_set(udc);
  615. }
  616. static void isp1301_pullup_enable(struct lpc32xx_udc *udc, int en_pullup,
  617. int block)
  618. {
  619. if (en_pullup == udc->pullup)
  620. return;
  621. udc->pullup = en_pullup;
  622. if (block)
  623. isp1301_pullup_set(udc);
  624. else
  625. /* defer slow i2c pull up setting */
  626. schedule_work(&udc->pullup_job);
  627. }
  628. #ifdef CONFIG_PM
  629. /* Powers up or down the ISP1301 transceiver */
  630. static void isp1301_set_powerstate(struct lpc32xx_udc *udc, int enable)
  631. {
  632. if (enable != 0)
  633. /* Power up ISP1301 - this ISP1301 will automatically wakeup
  634. when VBUS is detected */
  635. i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
  636. ISP1301_I2C_MODE_CONTROL_2 | ISP1301_I2C_REG_CLEAR_ADDR,
  637. MC2_GLOBAL_PWR_DN);
  638. else
  639. /* Power down ISP1301 */
  640. i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
  641. ISP1301_I2C_MODE_CONTROL_2, MC2_GLOBAL_PWR_DN);
  642. }
  643. static void power_work(struct work_struct *work)
  644. {
  645. struct lpc32xx_udc *udc =
  646. container_of(work, struct lpc32xx_udc, power_job);
  647. isp1301_set_powerstate(udc, udc->poweron);
  648. }
  649. #endif
  650. /*
  651. *
  652. * USB protocol engine command/data read/write helper functions
  653. *
  654. */
  655. /* Issues a single command to the USB device state machine */
  656. static void udc_protocol_cmd_w(struct lpc32xx_udc *udc, u32 cmd)
  657. {
  658. u32 pass = 0;
  659. int to;
  660. /* EP may lock on CLRI if this read isn't done */
  661. u32 tmp = readl(USBD_DEVINTST(udc->udp_baseaddr));
  662. (void) tmp;
  663. while (pass == 0) {
  664. writel(USBD_CCEMPTY, USBD_DEVINTCLR(udc->udp_baseaddr));
  665. /* Write command code */
  666. writel(cmd, USBD_CMDCODE(udc->udp_baseaddr));
  667. to = 10000;
  668. while (((readl(USBD_DEVINTST(udc->udp_baseaddr)) &
  669. USBD_CCEMPTY) == 0) && (to > 0)) {
  670. to--;
  671. }
  672. if (to > 0)
  673. pass = 1;
  674. cpu_relax();
  675. }
  676. }
  677. /* Issues 2 commands (or command and data) to the USB device state machine */
  678. static inline void udc_protocol_cmd_data_w(struct lpc32xx_udc *udc, u32 cmd,
  679. u32 data)
  680. {
  681. udc_protocol_cmd_w(udc, cmd);
  682. udc_protocol_cmd_w(udc, data);
  683. }
  684. /* Issues a single command to the USB device state machine and reads
  685. * response data */
  686. static u32 udc_protocol_cmd_r(struct lpc32xx_udc *udc, u32 cmd)
  687. {
  688. u32 tmp;
  689. int to = 1000;
  690. /* Write a command and read data from the protocol engine */
  691. writel((USBD_CDFULL | USBD_CCEMPTY),
  692. USBD_DEVINTCLR(udc->udp_baseaddr));
  693. /* Write command code */
  694. udc_protocol_cmd_w(udc, cmd);
  695. tmp = readl(USBD_DEVINTST(udc->udp_baseaddr));
  696. while ((!(readl(USBD_DEVINTST(udc->udp_baseaddr)) & USBD_CDFULL))
  697. && (to > 0))
  698. to--;
  699. if (!to)
  700. dev_dbg(udc->dev,
  701. "Protocol engine didn't receive response (CDFULL)\n");
  702. return readl(USBD_CMDDATA(udc->udp_baseaddr));
  703. }
  704. /*
  705. *
  706. * USB device interrupt mask support functions
  707. *
  708. */
  709. /* Enable one or more USB device interrupts */
  710. static inline void uda_enable_devint(struct lpc32xx_udc *udc, u32 devmask)
  711. {
  712. udc->enabled_devints |= devmask;
  713. writel(udc->enabled_devints, USBD_DEVINTEN(udc->udp_baseaddr));
  714. }
  715. /* Disable one or more USB device interrupts */
  716. static inline void uda_disable_devint(struct lpc32xx_udc *udc, u32 mask)
  717. {
  718. udc->enabled_devints &= ~mask;
  719. writel(udc->enabled_devints, USBD_DEVINTEN(udc->udp_baseaddr));
  720. }
  721. /* Clear one or more USB device interrupts */
  722. static inline void uda_clear_devint(struct lpc32xx_udc *udc, u32 mask)
  723. {
  724. writel(mask, USBD_DEVINTCLR(udc->udp_baseaddr));
  725. }
  726. /*
  727. *
  728. * Endpoint interrupt disable/enable functions
  729. *
  730. */
  731. /* Enable one or more USB endpoint interrupts */
  732. static void uda_enable_hwepint(struct lpc32xx_udc *udc, u32 hwep)
  733. {
  734. udc->enabled_hwepints |= (1 << hwep);
  735. writel(udc->enabled_hwepints, USBD_EPINTEN(udc->udp_baseaddr));
  736. }
  737. /* Disable one or more USB endpoint interrupts */
  738. static void uda_disable_hwepint(struct lpc32xx_udc *udc, u32 hwep)
  739. {
  740. udc->enabled_hwepints &= ~(1 << hwep);
  741. writel(udc->enabled_hwepints, USBD_EPINTEN(udc->udp_baseaddr));
  742. }
  743. /* Clear one or more USB endpoint interrupts */
  744. static inline void uda_clear_hwepint(struct lpc32xx_udc *udc, u32 hwep)
  745. {
  746. writel((1 << hwep), USBD_EPINTCLR(udc->udp_baseaddr));
  747. }
  748. /* Enable DMA for the HW channel */
  749. static inline void udc_ep_dma_enable(struct lpc32xx_udc *udc, u32 hwep)
  750. {
  751. writel((1 << hwep), USBD_EPDMAEN(udc->udp_baseaddr));
  752. }
  753. /* Disable DMA for the HW channel */
  754. static inline void udc_ep_dma_disable(struct lpc32xx_udc *udc, u32 hwep)
  755. {
  756. writel((1 << hwep), USBD_EPDMADIS(udc->udp_baseaddr));
  757. }
  758. /*
  759. *
  760. * Endpoint realize/unrealize functions
  761. *
  762. */
  763. /* Before an endpoint can be used, it needs to be realized
  764. * in the USB protocol engine - this realizes the endpoint.
  765. * The interrupt (FIFO or DMA) is not enabled with this function */
  766. static void udc_realize_hwep(struct lpc32xx_udc *udc, u32 hwep,
  767. u32 maxpacket)
  768. {
  769. int to = 1000;
  770. writel(USBD_EP_RLZED, USBD_DEVINTCLR(udc->udp_baseaddr));
  771. writel(hwep, USBD_EPIND(udc->udp_baseaddr));
  772. udc->realized_eps |= (1 << hwep);
  773. writel(udc->realized_eps, USBD_REEP(udc->udp_baseaddr));
  774. writel(maxpacket, USBD_EPMAXPSIZE(udc->udp_baseaddr));
  775. /* Wait until endpoint is realized in hardware */
  776. while ((!(readl(USBD_DEVINTST(udc->udp_baseaddr)) &
  777. USBD_EP_RLZED)) && (to > 0))
  778. to--;
  779. if (!to)
  780. dev_dbg(udc->dev, "EP not correctly realized in hardware\n");
  781. writel(USBD_EP_RLZED, USBD_DEVINTCLR(udc->udp_baseaddr));
  782. }
  783. /* Unrealize an EP */
  784. static void udc_unrealize_hwep(struct lpc32xx_udc *udc, u32 hwep)
  785. {
  786. udc->realized_eps &= ~(1 << hwep);
  787. writel(udc->realized_eps, USBD_REEP(udc->udp_baseaddr));
  788. }
  789. /*
  790. *
  791. * Endpoint support functions
  792. *
  793. */
  794. /* Select and clear endpoint interrupt */
  795. static u32 udc_selep_clrint(struct lpc32xx_udc *udc, u32 hwep)
  796. {
  797. udc_protocol_cmd_w(udc, CMD_SEL_EP_CLRI(hwep));
  798. return udc_protocol_cmd_r(udc, DAT_SEL_EP_CLRI(hwep));
  799. }
  800. /* Disables the endpoint in the USB protocol engine */
  801. static void udc_disable_hwep(struct lpc32xx_udc *udc, u32 hwep)
  802. {
  803. udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(hwep),
  804. DAT_WR_BYTE(EP_STAT_DA));
  805. }
  806. /* Stalls the endpoint - endpoint will return STALL */
  807. static void udc_stall_hwep(struct lpc32xx_udc *udc, u32 hwep)
  808. {
  809. udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(hwep),
  810. DAT_WR_BYTE(EP_STAT_ST));
  811. }
  812. /* Clear stall or reset endpoint */
  813. static void udc_clrstall_hwep(struct lpc32xx_udc *udc, u32 hwep)
  814. {
  815. udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(hwep),
  816. DAT_WR_BYTE(0));
  817. }
  818. /* Select an endpoint for endpoint status, clear, validate */
  819. static void udc_select_hwep(struct lpc32xx_udc *udc, u32 hwep)
  820. {
  821. udc_protocol_cmd_w(udc, CMD_SEL_EP(hwep));
  822. }
  823. /*
  824. *
  825. * Endpoint buffer management functions
  826. *
  827. */
  828. /* Clear the current endpoint's buffer */
  829. static void udc_clr_buffer_hwep(struct lpc32xx_udc *udc, u32 hwep)
  830. {
  831. udc_select_hwep(udc, hwep);
  832. udc_protocol_cmd_w(udc, CMD_CLR_BUF);
  833. }
  834. /* Validate the current endpoint's buffer */
  835. static void udc_val_buffer_hwep(struct lpc32xx_udc *udc, u32 hwep)
  836. {
  837. udc_select_hwep(udc, hwep);
  838. udc_protocol_cmd_w(udc, CMD_VALID_BUF);
  839. }
  840. static inline u32 udc_clearep_getsts(struct lpc32xx_udc *udc, u32 hwep)
  841. {
  842. /* Clear EP interrupt */
  843. uda_clear_hwepint(udc, hwep);
  844. return udc_selep_clrint(udc, hwep);
  845. }
  846. /*
  847. *
  848. * USB EP DMA support
  849. *
  850. */
  851. /* Allocate a DMA Descriptor */
  852. static struct lpc32xx_usbd_dd_gad *udc_dd_alloc(struct lpc32xx_udc *udc)
  853. {
  854. dma_addr_t dma;
  855. struct lpc32xx_usbd_dd_gad *dd;
  856. dd = (struct lpc32xx_usbd_dd_gad *) dma_pool_alloc(
  857. udc->dd_cache, (GFP_KERNEL | GFP_DMA), &dma);
  858. if (dd)
  859. dd->this_dma = dma;
  860. return dd;
  861. }
  862. /* Free a DMA Descriptor */
  863. static void udc_dd_free(struct lpc32xx_udc *udc, struct lpc32xx_usbd_dd_gad *dd)
  864. {
  865. dma_pool_free(udc->dd_cache, dd, dd->this_dma);
  866. }
  867. /*
  868. *
  869. * USB setup and shutdown functions
  870. *
  871. */
  872. /* Enables or disables most of the USB system clocks when low power mode is
  873. * needed. Clocks are typically started on a connection event, and disabled
  874. * when a cable is disconnected */
  875. #define OTGOFF_CLK_MASK (AHB_M_CLOCK_ON | I2C_CLOCK_ON)
  876. static void udc_clk_set(struct lpc32xx_udc *udc, int enable)
  877. {
  878. int to = 1000;
  879. if (enable != 0) {
  880. if (udc->clocked)
  881. return;
  882. udc->clocked = 1;
  883. /* 48MHz PLL up */
  884. clk_enable(udc->usb_pll_clk);
  885. /* Enable the USB device clock */
  886. writel(readl(USB_CTRL) | USB_DEV_NEED_CLK_EN,
  887. USB_CTRL);
  888. /* Set to enable all needed USB OTG clocks */
  889. writel(USB_CLOCK_MASK, USB_OTG_CLK_CTRL(udc));
  890. while (((readl(USB_OTG_CLK_STAT(udc)) & USB_CLOCK_MASK) !=
  891. USB_CLOCK_MASK) && (to > 0))
  892. to--;
  893. if (!to)
  894. dev_dbg(udc->dev, "Cannot enable USB OTG clocking\n");
  895. } else {
  896. if (!udc->clocked)
  897. return;
  898. udc->clocked = 0;
  899. /* Never disable the USB_HCLK during normal operation */
  900. /* 48MHz PLL dpwn */
  901. clk_disable(udc->usb_pll_clk);
  902. /* Enable the USB device clock */
  903. writel(readl(USB_CTRL) & ~USB_DEV_NEED_CLK_EN,
  904. USB_CTRL);
  905. /* Set to enable all needed USB OTG clocks */
  906. writel(OTGOFF_CLK_MASK, USB_OTG_CLK_CTRL(udc));
  907. while (((readl(USB_OTG_CLK_STAT(udc)) &
  908. OTGOFF_CLK_MASK) !=
  909. OTGOFF_CLK_MASK) && (to > 0))
  910. to--;
  911. if (!to)
  912. dev_dbg(udc->dev, "Cannot disable USB OTG clocking\n");
  913. }
  914. }
  915. /* Set/reset USB device address */
  916. static void udc_set_address(struct lpc32xx_udc *udc, u32 addr)
  917. {
  918. /* Address will be latched at the end of the status phase, or
  919. latched immediately if function is called twice */
  920. udc_protocol_cmd_data_w(udc, CMD_SET_ADDR,
  921. DAT_WR_BYTE(DEV_EN | addr));
  922. }
  923. /* Setup up a IN request for DMA transfer - this consists of determining the
  924. * list of DMA addresses for the transfer, allocating DMA Descriptors,
  925. * installing the DD into the UDCA, and then enabling the DMA for that EP */
  926. static int udc_ep_in_req_dma(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep)
  927. {
  928. struct lpc32xx_request *req;
  929. u32 hwep = ep->hwep_num;
  930. ep->req_pending = 1;
  931. /* There will always be a request waiting here */
  932. req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
  933. /* Place the DD Descriptor into the UDCA */
  934. udc->udca_v_base[hwep] = req->dd_desc_ptr->this_dma;
  935. /* Enable DMA and interrupt for the HW EP */
  936. udc_ep_dma_enable(udc, hwep);
  937. /* Clear ZLP if last packet is not of MAXP size */
  938. if (req->req.length % ep->ep.maxpacket)
  939. req->send_zlp = 0;
  940. return 0;
  941. }
  942. /* Setup up a OUT request for DMA transfer - this consists of determining the
  943. * list of DMA addresses for the transfer, allocating DMA Descriptors,
  944. * installing the DD into the UDCA, and then enabling the DMA for that EP */
  945. static int udc_ep_out_req_dma(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep)
  946. {
  947. struct lpc32xx_request *req;
  948. u32 hwep = ep->hwep_num;
  949. ep->req_pending = 1;
  950. /* There will always be a request waiting here */
  951. req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
  952. /* Place the DD Descriptor into the UDCA */
  953. udc->udca_v_base[hwep] = req->dd_desc_ptr->this_dma;
  954. /* Enable DMA and interrupt for the HW EP */
  955. udc_ep_dma_enable(udc, hwep);
  956. return 0;
  957. }
  958. static void udc_disable(struct lpc32xx_udc *udc)
  959. {
  960. u32 i;
  961. /* Disable device */
  962. udc_protocol_cmd_data_w(udc, CMD_CFG_DEV, DAT_WR_BYTE(0));
  963. udc_protocol_cmd_data_w(udc, CMD_SET_DEV_STAT, DAT_WR_BYTE(0));
  964. /* Disable all device interrupts (including EP0) */
  965. uda_disable_devint(udc, 0x3FF);
  966. /* Disable and reset all endpoint interrupts */
  967. for (i = 0; i < 32; i++) {
  968. uda_disable_hwepint(udc, i);
  969. uda_clear_hwepint(udc, i);
  970. udc_disable_hwep(udc, i);
  971. udc_unrealize_hwep(udc, i);
  972. udc->udca_v_base[i] = 0;
  973. /* Disable and clear all interrupts and DMA */
  974. udc_ep_dma_disable(udc, i);
  975. writel((1 << i), USBD_EOTINTCLR(udc->udp_baseaddr));
  976. writel((1 << i), USBD_NDDRTINTCLR(udc->udp_baseaddr));
  977. writel((1 << i), USBD_SYSERRTINTCLR(udc->udp_baseaddr));
  978. writel((1 << i), USBD_DMARCLR(udc->udp_baseaddr));
  979. }
  980. /* Disable DMA interrupts */
  981. writel(0, USBD_DMAINTEN(udc->udp_baseaddr));
  982. writel(0, USBD_UDCAH(udc->udp_baseaddr));
  983. }
  984. static void udc_enable(struct lpc32xx_udc *udc)
  985. {
  986. u32 i;
  987. struct lpc32xx_ep *ep = &udc->ep[0];
  988. /* Start with known state */
  989. udc_disable(udc);
  990. /* Enable device */
  991. udc_protocol_cmd_data_w(udc, CMD_SET_DEV_STAT, DAT_WR_BYTE(DEV_CON));
  992. /* EP interrupts on high priority, FRAME interrupt on low priority */
  993. writel(USBD_EP_FAST, USBD_DEVINTPRI(udc->udp_baseaddr));
  994. writel(0xFFFF, USBD_EPINTPRI(udc->udp_baseaddr));
  995. /* Clear any pending device interrupts */
  996. writel(0x3FF, USBD_DEVINTCLR(udc->udp_baseaddr));
  997. /* Setup UDCA - not yet used (DMA) */
  998. writel(udc->udca_p_base, USBD_UDCAH(udc->udp_baseaddr));
  999. /* Only enable EP0 in and out for now, EP0 only works in FIFO mode */
  1000. for (i = 0; i <= 1; i++) {
  1001. udc_realize_hwep(udc, i, ep->ep.maxpacket);
  1002. uda_enable_hwepint(udc, i);
  1003. udc_select_hwep(udc, i);
  1004. udc_clrstall_hwep(udc, i);
  1005. udc_clr_buffer_hwep(udc, i);
  1006. }
  1007. /* Device interrupt setup */
  1008. uda_clear_devint(udc, (USBD_ERR_INT | USBD_DEV_STAT | USBD_EP_SLOW |
  1009. USBD_EP_FAST));
  1010. uda_enable_devint(udc, (USBD_ERR_INT | USBD_DEV_STAT | USBD_EP_SLOW |
  1011. USBD_EP_FAST));
  1012. /* Set device address to 0 - called twice to force a latch in the USB
  1013. engine without the need of a setup packet status closure */
  1014. udc_set_address(udc, 0);
  1015. udc_set_address(udc, 0);
  1016. /* Enable master DMA interrupts */
  1017. writel((USBD_SYS_ERR_INT | USBD_EOT_INT),
  1018. USBD_DMAINTEN(udc->udp_baseaddr));
  1019. udc->dev_status = 0;
  1020. }
  1021. /*
  1022. *
  1023. * USB device board specific events handled via callbacks
  1024. *
  1025. */
  1026. /* Connection change event - notify board function of change */
  1027. static void uda_power_event(struct lpc32xx_udc *udc, u32 conn)
  1028. {
  1029. /* Just notify of a connection change event (optional) */
  1030. if (udc->board->conn_chgb != NULL)
  1031. udc->board->conn_chgb(conn);
  1032. }
  1033. /* Suspend/resume event - notify board function of change */
  1034. static void uda_resm_susp_event(struct lpc32xx_udc *udc, u32 conn)
  1035. {
  1036. /* Just notify of a Suspend/resume change event (optional) */
  1037. if (udc->board->susp_chgb != NULL)
  1038. udc->board->susp_chgb(conn);
  1039. if (conn)
  1040. udc->suspended = 0;
  1041. else
  1042. udc->suspended = 1;
  1043. }
  1044. /* Remote wakeup enable/disable - notify board function of change */
  1045. static void uda_remwkp_cgh(struct lpc32xx_udc *udc)
  1046. {
  1047. if (udc->board->rmwk_chgb != NULL)
  1048. udc->board->rmwk_chgb(udc->dev_status &
  1049. (1 << USB_DEVICE_REMOTE_WAKEUP));
  1050. }
  1051. /* Reads data from FIFO, adjusts for alignment and data size */
  1052. static void udc_pop_fifo(struct lpc32xx_udc *udc, u8 *data, u32 bytes)
  1053. {
  1054. int n, i, bl;
  1055. u16 *p16;
  1056. u32 *p32, tmp, cbytes;
  1057. /* Use optimal data transfer method based on source address and size */
  1058. switch (((u32) data) & 0x3) {
  1059. case 0: /* 32-bit aligned */
  1060. p32 = (u32 *) data;
  1061. cbytes = (bytes & ~0x3);
  1062. /* Copy 32-bit aligned data first */
  1063. for (n = 0; n < cbytes; n += 4)
  1064. *p32++ = readl(USBD_RXDATA(udc->udp_baseaddr));
  1065. /* Handle any remaining bytes */
  1066. bl = bytes - cbytes;
  1067. if (bl) {
  1068. tmp = readl(USBD_RXDATA(udc->udp_baseaddr));
  1069. for (n = 0; n < bl; n++)
  1070. data[cbytes + n] = ((tmp >> (n * 8)) & 0xFF);
  1071. }
  1072. break;
  1073. case 1: /* 8-bit aligned */
  1074. case 3:
  1075. /* Each byte has to be handled independently */
  1076. for (n = 0; n < bytes; n += 4) {
  1077. tmp = readl(USBD_RXDATA(udc->udp_baseaddr));
  1078. bl = bytes - n;
  1079. if (bl > 3)
  1080. bl = 3;
  1081. for (i = 0; i < bl; i++)
  1082. data[n + i] = (u8) ((tmp >> (n * 8)) & 0xFF);
  1083. }
  1084. break;
  1085. case 2: /* 16-bit aligned */
  1086. p16 = (u16 *) data;
  1087. cbytes = (bytes & ~0x3);
  1088. /* Copy 32-bit sized objects first with 16-bit alignment */
  1089. for (n = 0; n < cbytes; n += 4) {
  1090. tmp = readl(USBD_RXDATA(udc->udp_baseaddr));
  1091. *p16++ = (u16)(tmp & 0xFFFF);
  1092. *p16++ = (u16)((tmp >> 16) & 0xFFFF);
  1093. }
  1094. /* Handle any remaining bytes */
  1095. bl = bytes - cbytes;
  1096. if (bl) {
  1097. tmp = readl(USBD_RXDATA(udc->udp_baseaddr));
  1098. for (n = 0; n < bl; n++)
  1099. data[cbytes + n] = ((tmp >> (n * 8)) & 0xFF);
  1100. }
  1101. break;
  1102. }
  1103. }
  1104. /* Read data from the FIFO for an endpoint. This function is for endpoints (such
  1105. * as EP0) that don't use DMA. This function should only be called if a packet
  1106. * is known to be ready to read for the endpoint. Note that the endpoint must
  1107. * be selected in the protocol engine prior to this call. */
  1108. static u32 udc_read_hwep(struct lpc32xx_udc *udc, u32 hwep, u32 *data,
  1109. u32 bytes)
  1110. {
  1111. u32 tmpv;
  1112. int to = 1000;
  1113. u32 tmp, hwrep = ((hwep & 0x1E) << 1) | CTRL_RD_EN;
  1114. /* Setup read of endpoint */
  1115. writel(hwrep, USBD_CTRL(udc->udp_baseaddr));
  1116. /* Wait until packet is ready */
  1117. while ((((tmpv = readl(USBD_RXPLEN(udc->udp_baseaddr))) &
  1118. PKT_RDY) == 0) && (to > 0))
  1119. to--;
  1120. if (!to)
  1121. dev_dbg(udc->dev, "No packet ready on FIFO EP read\n");
  1122. /* Mask out count */
  1123. tmp = tmpv & PKT_LNGTH_MASK;
  1124. if (bytes < tmp)
  1125. tmp = bytes;
  1126. if ((tmp > 0) && (data != NULL))
  1127. udc_pop_fifo(udc, (u8 *) data, tmp);
  1128. writel(((hwep & 0x1E) << 1), USBD_CTRL(udc->udp_baseaddr));
  1129. /* Clear the buffer */
  1130. udc_clr_buffer_hwep(udc, hwep);
  1131. return tmp;
  1132. }
  1133. /* Stuffs data into the FIFO, adjusts for alignment and data size */
  1134. static void udc_stuff_fifo(struct lpc32xx_udc *udc, u8 *data, u32 bytes)
  1135. {
  1136. int n, i, bl;
  1137. u16 *p16;
  1138. u32 *p32, tmp, cbytes;
  1139. /* Use optimal data transfer method based on source address and size */
  1140. switch (((u32) data) & 0x3) {
  1141. case 0: /* 32-bit aligned */
  1142. p32 = (u32 *) data;
  1143. cbytes = (bytes & ~0x3);
  1144. /* Copy 32-bit aligned data first */
  1145. for (n = 0; n < cbytes; n += 4)
  1146. writel(*p32++, USBD_TXDATA(udc->udp_baseaddr));
  1147. /* Handle any remaining bytes */
  1148. bl = bytes - cbytes;
  1149. if (bl) {
  1150. tmp = 0;
  1151. for (n = 0; n < bl; n++)
  1152. tmp |= data[cbytes + n] << (n * 8);
  1153. writel(tmp, USBD_TXDATA(udc->udp_baseaddr));
  1154. }
  1155. break;
  1156. case 1: /* 8-bit aligned */
  1157. case 3:
  1158. /* Each byte has to be handled independently */
  1159. for (n = 0; n < bytes; n += 4) {
  1160. bl = bytes - n;
  1161. if (bl > 4)
  1162. bl = 4;
  1163. tmp = 0;
  1164. for (i = 0; i < bl; i++)
  1165. tmp |= data[n + i] << (i * 8);
  1166. writel(tmp, USBD_TXDATA(udc->udp_baseaddr));
  1167. }
  1168. break;
  1169. case 2: /* 16-bit aligned */
  1170. p16 = (u16 *) data;
  1171. cbytes = (bytes & ~0x3);
  1172. /* Copy 32-bit aligned data first */
  1173. for (n = 0; n < cbytes; n += 4) {
  1174. tmp = *p16++ & 0xFFFF;
  1175. tmp |= (*p16++ & 0xFFFF) << 16;
  1176. writel(tmp, USBD_TXDATA(udc->udp_baseaddr));
  1177. }
  1178. /* Handle any remaining bytes */
  1179. bl = bytes - cbytes;
  1180. if (bl) {
  1181. tmp = 0;
  1182. for (n = 0; n < bl; n++)
  1183. tmp |= data[cbytes + n] << (n * 8);
  1184. writel(tmp, USBD_TXDATA(udc->udp_baseaddr));
  1185. }
  1186. break;
  1187. }
  1188. }
  1189. /* Write data to the FIFO for an endpoint. This function is for endpoints (such
  1190. * as EP0) that don't use DMA. Note that the endpoint must be selected in the
  1191. * protocol engine prior to this call. */
  1192. static void udc_write_hwep(struct lpc32xx_udc *udc, u32 hwep, u32 *data,
  1193. u32 bytes)
  1194. {
  1195. u32 hwwep = ((hwep & 0x1E) << 1) | CTRL_WR_EN;
  1196. if ((bytes > 0) && (data == NULL))
  1197. return;
  1198. /* Setup write of endpoint */
  1199. writel(hwwep, USBD_CTRL(udc->udp_baseaddr));
  1200. writel(bytes, USBD_TXPLEN(udc->udp_baseaddr));
  1201. /* Need at least 1 byte to trigger TX */
  1202. if (bytes == 0)
  1203. writel(0, USBD_TXDATA(udc->udp_baseaddr));
  1204. else
  1205. udc_stuff_fifo(udc, (u8 *) data, bytes);
  1206. writel(((hwep & 0x1E) << 1), USBD_CTRL(udc->udp_baseaddr));
  1207. udc_val_buffer_hwep(udc, hwep);
  1208. }
  1209. /* USB device reset - resets USB to a default state with just EP0
  1210. enabled */
  1211. static void uda_usb_reset(struct lpc32xx_udc *udc)
  1212. {
  1213. u32 i = 0;
  1214. /* Re-init device controller and EP0 */
  1215. udc_enable(udc);
  1216. udc->gadget.speed = USB_SPEED_FULL;
  1217. for (i = 1; i < NUM_ENDPOINTS; i++) {
  1218. struct lpc32xx_ep *ep = &udc->ep[i];
  1219. ep->req_pending = 0;
  1220. }
  1221. }
  1222. /* Send a ZLP on EP0 */
  1223. static void udc_ep0_send_zlp(struct lpc32xx_udc *udc)
  1224. {
  1225. udc_write_hwep(udc, EP_IN, NULL, 0);
  1226. }
  1227. /* Get current frame number */
  1228. static u16 udc_get_current_frame(struct lpc32xx_udc *udc)
  1229. {
  1230. u16 flo, fhi;
  1231. udc_protocol_cmd_w(udc, CMD_RD_FRAME);
  1232. flo = (u16) udc_protocol_cmd_r(udc, DAT_RD_FRAME);
  1233. fhi = (u16) udc_protocol_cmd_r(udc, DAT_RD_FRAME);
  1234. return (fhi << 8) | flo;
  1235. }
  1236. /* Set the device as configured - enables all endpoints */
  1237. static inline void udc_set_device_configured(struct lpc32xx_udc *udc)
  1238. {
  1239. udc_protocol_cmd_data_w(udc, CMD_CFG_DEV, DAT_WR_BYTE(CONF_DVICE));
  1240. }
  1241. /* Set the device as unconfigured - disables all endpoints */
  1242. static inline void udc_set_device_unconfigured(struct lpc32xx_udc *udc)
  1243. {
  1244. udc_protocol_cmd_data_w(udc, CMD_CFG_DEV, DAT_WR_BYTE(0));
  1245. }
  1246. /* reinit == restore initial software state */
  1247. static void udc_reinit(struct lpc32xx_udc *udc)
  1248. {
  1249. u32 i;
  1250. INIT_LIST_HEAD(&udc->gadget.ep_list);
  1251. INIT_LIST_HEAD(&udc->gadget.ep0->ep_list);
  1252. for (i = 0; i < NUM_ENDPOINTS; i++) {
  1253. struct lpc32xx_ep *ep = &udc->ep[i];
  1254. if (i != 0)
  1255. list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
  1256. ep->desc = NULL;
  1257. ep->ep.maxpacket = ep->maxpacket;
  1258. INIT_LIST_HEAD(&ep->queue);
  1259. ep->req_pending = 0;
  1260. }
  1261. udc->ep0state = WAIT_FOR_SETUP;
  1262. }
  1263. /* Must be called with lock */
  1264. static void done(struct lpc32xx_ep *ep, struct lpc32xx_request *req, int status)
  1265. {
  1266. struct lpc32xx_udc *udc = ep->udc;
  1267. list_del_init(&req->queue);
  1268. if (req->req.status == -EINPROGRESS)
  1269. req->req.status = status;
  1270. else
  1271. status = req->req.status;
  1272. if (ep->lep) {
  1273. enum dma_data_direction direction;
  1274. if (ep->is_in)
  1275. direction = DMA_TO_DEVICE;
  1276. else
  1277. direction = DMA_FROM_DEVICE;
  1278. if (req->mapped) {
  1279. dma_unmap_single(ep->udc->gadget.dev.parent,
  1280. req->req.dma, req->req.length,
  1281. direction);
  1282. req->req.dma = 0;
  1283. req->mapped = 0;
  1284. } else
  1285. dma_sync_single_for_cpu(ep->udc->gadget.dev.parent,
  1286. req->req.dma, req->req.length,
  1287. direction);
  1288. /* Free DDs */
  1289. udc_dd_free(udc, req->dd_desc_ptr);
  1290. }
  1291. if (status && status != -ESHUTDOWN)
  1292. ep_dbg(ep, "%s done %p, status %d\n", ep->ep.name, req, status);
  1293. ep->req_pending = 0;
  1294. spin_unlock(&udc->lock);
  1295. req->req.complete(&ep->ep, &req->req);
  1296. spin_lock(&udc->lock);
  1297. }
  1298. /* Must be called with lock */
  1299. static void nuke(struct lpc32xx_ep *ep, int status)
  1300. {
  1301. struct lpc32xx_request *req;
  1302. while (!list_empty(&ep->queue)) {
  1303. req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
  1304. done(ep, req, status);
  1305. }
  1306. if (ep->desc && status == -ESHUTDOWN) {
  1307. uda_disable_hwepint(ep->udc, ep->hwep_num);
  1308. udc_disable_hwep(ep->udc, ep->hwep_num);
  1309. }
  1310. }
  1311. /* IN endpoint 0 transfer */
  1312. static int udc_ep0_in_req(struct lpc32xx_udc *udc)
  1313. {
  1314. struct lpc32xx_request *req;
  1315. struct lpc32xx_ep *ep0 = &udc->ep[0];
  1316. u32 tsend, ts = 0;
  1317. if (list_empty(&ep0->queue))
  1318. /* Nothing to send */
  1319. return 0;
  1320. else
  1321. req = list_entry(ep0->queue.next, struct lpc32xx_request,
  1322. queue);
  1323. tsend = ts = req->req.length - req->req.actual;
  1324. if (ts == 0) {
  1325. /* Send a ZLP */
  1326. udc_ep0_send_zlp(udc);
  1327. done(ep0, req, 0);
  1328. return 1;
  1329. } else if (ts > ep0->ep.maxpacket)
  1330. ts = ep0->ep.maxpacket; /* Just send what we can */
  1331. /* Write data to the EP0 FIFO and start transfer */
  1332. udc_write_hwep(udc, EP_IN, (req->req.buf + req->req.actual), ts);
  1333. /* Increment data pointer */
  1334. req->req.actual += ts;
  1335. if (tsend >= ep0->ep.maxpacket)
  1336. return 0; /* Stay in data transfer state */
  1337. /* Transfer request is complete */
  1338. udc->ep0state = WAIT_FOR_SETUP;
  1339. done(ep0, req, 0);
  1340. return 1;
  1341. }
  1342. /* OUT endpoint 0 transfer */
  1343. static int udc_ep0_out_req(struct lpc32xx_udc *udc)
  1344. {
  1345. struct lpc32xx_request *req;
  1346. struct lpc32xx_ep *ep0 = &udc->ep[0];
  1347. u32 tr, bufferspace;
  1348. if (list_empty(&ep0->queue))
  1349. return 0;
  1350. else
  1351. req = list_entry(ep0->queue.next, struct lpc32xx_request,
  1352. queue);
  1353. if (req) {
  1354. if (req->req.length == 0) {
  1355. /* Just dequeue request */
  1356. done(ep0, req, 0);
  1357. udc->ep0state = WAIT_FOR_SETUP;
  1358. return 1;
  1359. }
  1360. /* Get data from FIFO */
  1361. bufferspace = req->req.length - req->req.actual;
  1362. if (bufferspace > ep0->ep.maxpacket)
  1363. bufferspace = ep0->ep.maxpacket;
  1364. /* Copy data to buffer */
  1365. prefetchw(req->req.buf + req->req.actual);
  1366. tr = udc_read_hwep(udc, EP_OUT, req->req.buf + req->req.actual,
  1367. bufferspace);
  1368. req->req.actual += bufferspace;
  1369. if (tr < ep0->ep.maxpacket) {
  1370. /* This is the last packet */
  1371. done(ep0, req, 0);
  1372. udc->ep0state = WAIT_FOR_SETUP;
  1373. return 1;
  1374. }
  1375. }
  1376. return 0;
  1377. }
  1378. /* Must be called with lock */
  1379. static void stop_activity(struct lpc32xx_udc *udc)
  1380. {
  1381. struct usb_gadget_driver *driver = udc->driver;
  1382. int i;
  1383. if (udc->gadget.speed == USB_SPEED_UNKNOWN)
  1384. driver = NULL;
  1385. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1386. udc->suspended = 0;
  1387. for (i = 0; i < NUM_ENDPOINTS; i++) {
  1388. struct lpc32xx_ep *ep = &udc->ep[i];
  1389. nuke(ep, -ESHUTDOWN);
  1390. }
  1391. if (driver) {
  1392. spin_unlock(&udc->lock);
  1393. driver->disconnect(&udc->gadget);
  1394. spin_lock(&udc->lock);
  1395. }
  1396. isp1301_pullup_enable(udc, 0, 0);
  1397. udc_disable(udc);
  1398. udc_reinit(udc);
  1399. }
  1400. /*
  1401. * Activate or kill host pullup
  1402. * Can be called with or without lock
  1403. */
  1404. static void pullup(struct lpc32xx_udc *udc, int is_on)
  1405. {
  1406. if (!udc->clocked)
  1407. return;
  1408. if (!udc->enabled || !udc->vbus)
  1409. is_on = 0;
  1410. if (is_on != udc->pullup)
  1411. isp1301_pullup_enable(udc, is_on, 0);
  1412. }
  1413. /* Must be called without lock */
  1414. static int lpc32xx_ep_disable(struct usb_ep *_ep)
  1415. {
  1416. struct lpc32xx_ep *ep = container_of(_ep, struct lpc32xx_ep, ep);
  1417. struct lpc32xx_udc *udc = ep->udc;
  1418. unsigned long flags;
  1419. if ((ep->hwep_num_base == 0) || (ep->hwep_num == 0))
  1420. return -EINVAL;
  1421. spin_lock_irqsave(&udc->lock, flags);
  1422. nuke(ep, -ESHUTDOWN);
  1423. /* restore the endpoint's pristine config */
  1424. ep->desc = NULL;
  1425. /* Clear all DMA statuses for this EP */
  1426. udc_ep_dma_disable(udc, ep->hwep_num);
  1427. writel(1 << ep->hwep_num, USBD_EOTINTCLR(udc->udp_baseaddr));
  1428. writel(1 << ep->hwep_num, USBD_NDDRTINTCLR(udc->udp_baseaddr));
  1429. writel(1 << ep->hwep_num, USBD_SYSERRTINTCLR(udc->udp_baseaddr));
  1430. writel(1 << ep->hwep_num, USBD_DMARCLR(udc->udp_baseaddr));
  1431. /* Remove the DD pointer in the UDCA */
  1432. udc->udca_v_base[ep->hwep_num] = 0;
  1433. /* Disable and reset endpoint and interrupt */
  1434. uda_clear_hwepint(udc, ep->hwep_num);
  1435. udc_unrealize_hwep(udc, ep->hwep_num);
  1436. ep->hwep_num = 0;
  1437. spin_unlock_irqrestore(&udc->lock, flags);
  1438. atomic_dec(&udc->enabled_ep_cnt);
  1439. wake_up(&udc->ep_disable_wait_queue);
  1440. return 0;
  1441. }
  1442. /* Must be called without lock */
  1443. static int lpc32xx_ep_enable(struct usb_ep *_ep,
  1444. const struct usb_endpoint_descriptor *desc)
  1445. {
  1446. struct lpc32xx_ep *ep = container_of(_ep, struct lpc32xx_ep, ep);
  1447. struct lpc32xx_udc *udc = ep->udc;
  1448. u16 maxpacket;
  1449. u32 tmp;
  1450. unsigned long flags;
  1451. /* Verify EP data */
  1452. if ((!_ep) || (!ep) || (!desc) || (ep->desc) ||
  1453. (desc->bDescriptorType != USB_DT_ENDPOINT)) {
  1454. dev_dbg(udc->dev, "bad ep or descriptor\n");
  1455. return -EINVAL;
  1456. }
  1457. maxpacket = usb_endpoint_maxp(desc);
  1458. if ((maxpacket == 0) || (maxpacket > ep->maxpacket)) {
  1459. dev_dbg(udc->dev, "bad ep descriptor's packet size\n");
  1460. return -EINVAL;
  1461. }
  1462. /* Don't touch EP0 */
  1463. if (ep->hwep_num_base == 0) {
  1464. dev_dbg(udc->dev, "Can't re-enable EP0!!!\n");
  1465. return -EINVAL;
  1466. }
  1467. /* Is driver ready? */
  1468. if ((!udc->driver) || (udc->gadget.speed == USB_SPEED_UNKNOWN)) {
  1469. dev_dbg(udc->dev, "bogus device state\n");
  1470. return -ESHUTDOWN;
  1471. }
  1472. tmp = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  1473. switch (tmp) {
  1474. case USB_ENDPOINT_XFER_CONTROL:
  1475. return -EINVAL;
  1476. case USB_ENDPOINT_XFER_INT:
  1477. if (maxpacket > ep->maxpacket) {
  1478. dev_dbg(udc->dev,
  1479. "Bad INT endpoint maxpacket %d\n", maxpacket);
  1480. return -EINVAL;
  1481. }
  1482. break;
  1483. case USB_ENDPOINT_XFER_BULK:
  1484. switch (maxpacket) {
  1485. case 8:
  1486. case 16:
  1487. case 32:
  1488. case 64:
  1489. break;
  1490. default:
  1491. dev_dbg(udc->dev,
  1492. "Bad BULK endpoint maxpacket %d\n", maxpacket);
  1493. return -EINVAL;
  1494. }
  1495. break;
  1496. case USB_ENDPOINT_XFER_ISOC:
  1497. break;
  1498. }
  1499. spin_lock_irqsave(&udc->lock, flags);
  1500. /* Initialize endpoint to match the selected descriptor */
  1501. ep->is_in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
  1502. ep->desc = desc;
  1503. ep->ep.maxpacket = maxpacket;
  1504. /* Map hardware endpoint from base and direction */
  1505. if (ep->is_in)
  1506. /* IN endpoints are offset 1 from the OUT endpoint */
  1507. ep->hwep_num = ep->hwep_num_base + EP_IN;
  1508. else
  1509. ep->hwep_num = ep->hwep_num_base;
  1510. ep_dbg(ep, "EP enabled: %s, HW:%d, MP:%d IN:%d\n", ep->ep.name,
  1511. ep->hwep_num, maxpacket, (ep->is_in == 1));
  1512. /* Realize the endpoint, interrupt is enabled later when
  1513. * buffers are queued, IN EPs will NAK until buffers are ready */
  1514. udc_realize_hwep(udc, ep->hwep_num, ep->ep.maxpacket);
  1515. udc_clr_buffer_hwep(udc, ep->hwep_num);
  1516. uda_disable_hwepint(udc, ep->hwep_num);
  1517. udc_clrstall_hwep(udc, ep->hwep_num);
  1518. /* Clear all DMA statuses for this EP */
  1519. udc_ep_dma_disable(udc, ep->hwep_num);
  1520. writel(1 << ep->hwep_num, USBD_EOTINTCLR(udc->udp_baseaddr));
  1521. writel(1 << ep->hwep_num, USBD_NDDRTINTCLR(udc->udp_baseaddr));
  1522. writel(1 << ep->hwep_num, USBD_SYSERRTINTCLR(udc->udp_baseaddr));
  1523. writel(1 << ep->hwep_num, USBD_DMARCLR(udc->udp_baseaddr));
  1524. spin_unlock_irqrestore(&udc->lock, flags);
  1525. atomic_inc(&udc->enabled_ep_cnt);
  1526. return 0;
  1527. }
  1528. /*
  1529. * Allocate a USB request list
  1530. * Can be called with or without lock
  1531. */
  1532. static struct usb_request *lpc32xx_ep_alloc_request(struct usb_ep *_ep,
  1533. gfp_t gfp_flags)
  1534. {
  1535. struct lpc32xx_request *req;
  1536. req = kzalloc(sizeof(struct lpc32xx_request), gfp_flags);
  1537. if (!req)
  1538. return NULL;
  1539. INIT_LIST_HEAD(&req->queue);
  1540. return &req->req;
  1541. }
  1542. /*
  1543. * De-allocate a USB request list
  1544. * Can be called with or without lock
  1545. */
  1546. static void lpc32xx_ep_free_request(struct usb_ep *_ep,
  1547. struct usb_request *_req)
  1548. {
  1549. struct lpc32xx_request *req;
  1550. req = container_of(_req, struct lpc32xx_request, req);
  1551. BUG_ON(!list_empty(&req->queue));
  1552. kfree(req);
  1553. }
  1554. /* Must be called without lock */
  1555. static int lpc32xx_ep_queue(struct usb_ep *_ep,
  1556. struct usb_request *_req, gfp_t gfp_flags)
  1557. {
  1558. struct lpc32xx_request *req;
  1559. struct lpc32xx_ep *ep;
  1560. struct lpc32xx_udc *udc;
  1561. unsigned long flags;
  1562. int status = 0;
  1563. req = container_of(_req, struct lpc32xx_request, req);
  1564. ep = container_of(_ep, struct lpc32xx_ep, ep);
  1565. if (!_req || !_req->complete || !_req->buf ||
  1566. !list_empty(&req->queue))
  1567. return -EINVAL;
  1568. udc = ep->udc;
  1569. if (!_ep || (!ep->desc && ep->hwep_num_base != 0)) {
  1570. dev_dbg(udc->dev, "invalid ep\n");
  1571. return -EINVAL;
  1572. }
  1573. if ((!udc) || (!udc->driver) ||
  1574. (udc->gadget.speed == USB_SPEED_UNKNOWN)) {
  1575. dev_dbg(udc->dev, "invalid device\n");
  1576. return -EINVAL;
  1577. }
  1578. if (ep->lep) {
  1579. enum dma_data_direction direction;
  1580. struct lpc32xx_usbd_dd_gad *dd;
  1581. /* Map DMA pointer */
  1582. if (ep->is_in)
  1583. direction = DMA_TO_DEVICE;
  1584. else
  1585. direction = DMA_FROM_DEVICE;
  1586. if (req->req.dma == 0) {
  1587. req->req.dma = dma_map_single(
  1588. ep->udc->gadget.dev.parent,
  1589. req->req.buf, req->req.length, direction);
  1590. req->mapped = 1;
  1591. } else {
  1592. dma_sync_single_for_device(
  1593. ep->udc->gadget.dev.parent, req->req.dma,
  1594. req->req.length, direction);
  1595. req->mapped = 0;
  1596. }
  1597. /* For the request, build a list of DDs */
  1598. dd = udc_dd_alloc(udc);
  1599. if (!dd) {
  1600. /* Error allocating DD */
  1601. return -ENOMEM;
  1602. }
  1603. req->dd_desc_ptr = dd;
  1604. /* Setup the DMA descriptor */
  1605. dd->dd_next_phy = dd->dd_next_v = 0;
  1606. dd->dd_buffer_addr = req->req.dma;
  1607. dd->dd_status = 0;
  1608. /* Special handling for ISO EPs */
  1609. if (ep->eptype == EP_ISO_TYPE) {
  1610. dd->dd_setup = DD_SETUP_ISO_EP |
  1611. DD_SETUP_PACKETLEN(0) |
  1612. DD_SETUP_DMALENBYTES(1);
  1613. dd->dd_iso_ps_mem_addr = dd->this_dma + 24;
  1614. if (ep->is_in)
  1615. dd->iso_status[0] = req->req.length;
  1616. else
  1617. dd->iso_status[0] = 0;
  1618. } else
  1619. dd->dd_setup = DD_SETUP_PACKETLEN(ep->ep.maxpacket) |
  1620. DD_SETUP_DMALENBYTES(req->req.length);
  1621. }
  1622. ep_dbg(ep, "%s queue req %p len %d buf %p (in=%d) z=%d\n", _ep->name,
  1623. _req, _req->length, _req->buf, ep->is_in, _req->zero);
  1624. spin_lock_irqsave(&udc->lock, flags);
  1625. _req->status = -EINPROGRESS;
  1626. _req->actual = 0;
  1627. req->send_zlp = _req->zero;
  1628. /* Kickstart empty queues */
  1629. if (list_empty(&ep->queue)) {
  1630. list_add_tail(&req->queue, &ep->queue);
  1631. if (ep->hwep_num_base == 0) {
  1632. /* Handle expected data direction */
  1633. if (ep->is_in) {
  1634. /* IN packet to host */
  1635. udc->ep0state = DATA_IN;
  1636. status = udc_ep0_in_req(udc);
  1637. } else {
  1638. /* OUT packet from host */
  1639. udc->ep0state = DATA_OUT;
  1640. status = udc_ep0_out_req(udc);
  1641. }
  1642. } else if (ep->is_in) {
  1643. /* IN packet to host and kick off transfer */
  1644. if (!ep->req_pending)
  1645. udc_ep_in_req_dma(udc, ep);
  1646. } else
  1647. /* OUT packet from host and kick off list */
  1648. if (!ep->req_pending)
  1649. udc_ep_out_req_dma(udc, ep);
  1650. } else
  1651. list_add_tail(&req->queue, &ep->queue);
  1652. spin_unlock_irqrestore(&udc->lock, flags);
  1653. return (status < 0) ? status : 0;
  1654. }
  1655. /* Must be called without lock */
  1656. static int lpc32xx_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  1657. {
  1658. struct lpc32xx_ep *ep;
  1659. struct lpc32xx_request *req;
  1660. unsigned long flags;
  1661. ep = container_of(_ep, struct lpc32xx_ep, ep);
  1662. if (!_ep || ep->hwep_num_base == 0)
  1663. return -EINVAL;
  1664. spin_lock_irqsave(&ep->udc->lock, flags);
  1665. /* make sure it's actually queued on this endpoint */
  1666. list_for_each_entry(req, &ep->queue, queue) {
  1667. if (&req->req == _req)
  1668. break;
  1669. }
  1670. if (&req->req != _req) {
  1671. spin_unlock_irqrestore(&ep->udc->lock, flags);
  1672. return -EINVAL;
  1673. }
  1674. done(ep, req, -ECONNRESET);
  1675. spin_unlock_irqrestore(&ep->udc->lock, flags);
  1676. return 0;
  1677. }
  1678. /* Must be called without lock */
  1679. static int lpc32xx_ep_set_halt(struct usb_ep *_ep, int value)
  1680. {
  1681. struct lpc32xx_ep *ep = container_of(_ep, struct lpc32xx_ep, ep);
  1682. struct lpc32xx_udc *udc = ep->udc;
  1683. unsigned long flags;
  1684. if ((!ep) || (ep->desc == NULL) || (ep->hwep_num <= 1))
  1685. return -EINVAL;
  1686. /* Don't halt an IN EP */
  1687. if (ep->is_in)
  1688. return -EAGAIN;
  1689. spin_lock_irqsave(&udc->lock, flags);
  1690. if (value == 1) {
  1691. /* stall */
  1692. udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(ep->hwep_num),
  1693. DAT_WR_BYTE(EP_STAT_ST));
  1694. } else {
  1695. /* End stall */
  1696. ep->wedge = 0;
  1697. udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(ep->hwep_num),
  1698. DAT_WR_BYTE(0));
  1699. }
  1700. spin_unlock_irqrestore(&udc->lock, flags);
  1701. return 0;
  1702. }
  1703. /* set the halt feature and ignores clear requests */
  1704. static int lpc32xx_ep_set_wedge(struct usb_ep *_ep)
  1705. {
  1706. struct lpc32xx_ep *ep = container_of(_ep, struct lpc32xx_ep, ep);
  1707. if (!_ep || !ep->udc)
  1708. return -EINVAL;
  1709. ep->wedge = 1;
  1710. return usb_ep_set_halt(_ep);
  1711. }
  1712. static const struct usb_ep_ops lpc32xx_ep_ops = {
  1713. .enable = lpc32xx_ep_enable,
  1714. .disable = lpc32xx_ep_disable,
  1715. .alloc_request = lpc32xx_ep_alloc_request,
  1716. .free_request = lpc32xx_ep_free_request,
  1717. .queue = lpc32xx_ep_queue,
  1718. .dequeue = lpc32xx_ep_dequeue,
  1719. .set_halt = lpc32xx_ep_set_halt,
  1720. .set_wedge = lpc32xx_ep_set_wedge,
  1721. };
  1722. /* Send a ZLP on a non-0 IN EP */
  1723. void udc_send_in_zlp(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep)
  1724. {
  1725. /* Clear EP status */
  1726. udc_clearep_getsts(udc, ep->hwep_num);
  1727. /* Send ZLP via FIFO mechanism */
  1728. udc_write_hwep(udc, ep->hwep_num, NULL, 0);
  1729. }
  1730. /*
  1731. * Handle EP completion for ZLP
  1732. * This function will only be called when a delayed ZLP needs to be sent out
  1733. * after a DMA transfer has filled both buffers.
  1734. */
  1735. void udc_handle_eps(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep)
  1736. {
  1737. u32 epstatus;
  1738. struct lpc32xx_request *req;
  1739. if (ep->hwep_num <= 0)
  1740. return;
  1741. uda_clear_hwepint(udc, ep->hwep_num);
  1742. /* If this interrupt isn't enabled, return now */
  1743. if (!(udc->enabled_hwepints & (1 << ep->hwep_num)))
  1744. return;
  1745. /* Get endpoint status */
  1746. epstatus = udc_clearep_getsts(udc, ep->hwep_num);
  1747. /*
  1748. * This should never happen, but protect against writing to the
  1749. * buffer when full.
  1750. */
  1751. if (epstatus & EP_SEL_F)
  1752. return;
  1753. if (ep->is_in) {
  1754. udc_send_in_zlp(udc, ep);
  1755. uda_disable_hwepint(udc, ep->hwep_num);
  1756. } else
  1757. return;
  1758. /* If there isn't a request waiting, something went wrong */
  1759. req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
  1760. if (req) {
  1761. done(ep, req, 0);
  1762. /* Start another request if ready */
  1763. if (!list_empty(&ep->queue)) {
  1764. if (ep->is_in)
  1765. udc_ep_in_req_dma(udc, ep);
  1766. else
  1767. udc_ep_out_req_dma(udc, ep);
  1768. } else
  1769. ep->req_pending = 0;
  1770. }
  1771. }
  1772. /* DMA end of transfer completion */
  1773. static void udc_handle_dma_ep(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep)
  1774. {
  1775. u32 status, epstatus;
  1776. struct lpc32xx_request *req;
  1777. struct lpc32xx_usbd_dd_gad *dd;
  1778. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  1779. ep->totalints++;
  1780. #endif
  1781. req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
  1782. if (!req) {
  1783. ep_err(ep, "DMA interrupt on no req!\n");
  1784. return;
  1785. }
  1786. dd = req->dd_desc_ptr;
  1787. /* DMA descriptor should always be retired for this call */
  1788. if (!(dd->dd_status & DD_STATUS_DD_RETIRED))
  1789. ep_warn(ep, "DMA descriptor did not retire\n");
  1790. /* Disable DMA */
  1791. udc_ep_dma_disable(udc, ep->hwep_num);
  1792. writel((1 << ep->hwep_num), USBD_EOTINTCLR(udc->udp_baseaddr));
  1793. writel((1 << ep->hwep_num), USBD_NDDRTINTCLR(udc->udp_baseaddr));
  1794. /* System error? */
  1795. if (readl(USBD_SYSERRTINTST(udc->udp_baseaddr)) &
  1796. (1 << ep->hwep_num)) {
  1797. writel((1 << ep->hwep_num),
  1798. USBD_SYSERRTINTCLR(udc->udp_baseaddr));
  1799. ep_err(ep, "AHB critical error!\n");
  1800. ep->req_pending = 0;
  1801. /* The error could have occurred on a packet of a multipacket
  1802. * transfer, so recovering the transfer is not possible. Close
  1803. * the request with an error */
  1804. done(ep, req, -ECONNABORTED);
  1805. return;
  1806. }
  1807. /* Handle the current DD's status */
  1808. status = dd->dd_status;
  1809. switch (status & DD_STATUS_STS_MASK) {
  1810. case DD_STATUS_STS_NS:
  1811. /* DD not serviced? This shouldn't happen! */
  1812. ep->req_pending = 0;
  1813. ep_err(ep, "DMA critical EP error: DD not serviced (0x%x)!\n",
  1814. status);
  1815. done(ep, req, -ECONNABORTED);
  1816. return;
  1817. case DD_STATUS_STS_BS:
  1818. /* Interrupt only fires on EOT - This shouldn't happen! */
  1819. ep->req_pending = 0;
  1820. ep_err(ep, "DMA critical EP error: EOT prior to service completion (0x%x)!\n",
  1821. status);
  1822. done(ep, req, -ECONNABORTED);
  1823. return;
  1824. case DD_STATUS_STS_NC:
  1825. case DD_STATUS_STS_DUR:
  1826. /* Really just a short packet, not an underrun */
  1827. /* This is a good status and what we expect */
  1828. break;
  1829. default:
  1830. /* Data overrun, system error, or unknown */
  1831. ep->req_pending = 0;
  1832. ep_err(ep, "DMA critical EP error: System error (0x%x)!\n",
  1833. status);
  1834. done(ep, req, -ECONNABORTED);
  1835. return;
  1836. }
  1837. /* ISO endpoints are handled differently */
  1838. if (ep->eptype == EP_ISO_TYPE) {
  1839. if (ep->is_in)
  1840. req->req.actual = req->req.length;
  1841. else
  1842. req->req.actual = dd->iso_status[0] & 0xFFFF;
  1843. } else
  1844. req->req.actual += DD_STATUS_CURDMACNT(status);
  1845. /* Send a ZLP if necessary. This will be done for non-int
  1846. * packets which have a size that is a divisor of MAXP */
  1847. if (req->send_zlp) {
  1848. /*
  1849. * If at least 1 buffer is available, send the ZLP now.
  1850. * Otherwise, the ZLP send needs to be deferred until a
  1851. * buffer is available.
  1852. */
  1853. if (udc_clearep_getsts(udc, ep->hwep_num) & EP_SEL_F) {
  1854. udc_clearep_getsts(udc, ep->hwep_num);
  1855. uda_enable_hwepint(udc, ep->hwep_num);
  1856. epstatus = udc_clearep_getsts(udc, ep->hwep_num);
  1857. /* Let the EP interrupt handle the ZLP */
  1858. return;
  1859. } else
  1860. udc_send_in_zlp(udc, ep);
  1861. }
  1862. /* Transfer request is complete */
  1863. done(ep, req, 0);
  1864. /* Start another request if ready */
  1865. udc_clearep_getsts(udc, ep->hwep_num);
  1866. if (!list_empty((&ep->queue))) {
  1867. if (ep->is_in)
  1868. udc_ep_in_req_dma(udc, ep);
  1869. else
  1870. udc_ep_out_req_dma(udc, ep);
  1871. } else
  1872. ep->req_pending = 0;
  1873. }
  1874. /*
  1875. *
  1876. * Endpoint 0 functions
  1877. *
  1878. */
  1879. static void udc_handle_dev(struct lpc32xx_udc *udc)
  1880. {
  1881. u32 tmp;
  1882. udc_protocol_cmd_w(udc, CMD_GET_DEV_STAT);
  1883. tmp = udc_protocol_cmd_r(udc, DAT_GET_DEV_STAT);
  1884. if (tmp & DEV_RST)
  1885. uda_usb_reset(udc);
  1886. else if (tmp & DEV_CON_CH)
  1887. uda_power_event(udc, (tmp & DEV_CON));
  1888. else if (tmp & DEV_SUS_CH) {
  1889. if (tmp & DEV_SUS) {
  1890. if (udc->vbus == 0)
  1891. stop_activity(udc);
  1892. else if ((udc->gadget.speed != USB_SPEED_UNKNOWN) &&
  1893. udc->driver) {
  1894. /* Power down transceiver */
  1895. udc->poweron = 0;
  1896. schedule_work(&udc->pullup_job);
  1897. uda_resm_susp_event(udc, 1);
  1898. }
  1899. } else if ((udc->gadget.speed != USB_SPEED_UNKNOWN) &&
  1900. udc->driver && udc->vbus) {
  1901. uda_resm_susp_event(udc, 0);
  1902. /* Power up transceiver */
  1903. udc->poweron = 1;
  1904. schedule_work(&udc->pullup_job);
  1905. }
  1906. }
  1907. }
  1908. static int udc_get_status(struct lpc32xx_udc *udc, u16 reqtype, u16 wIndex)
  1909. {
  1910. struct lpc32xx_ep *ep;
  1911. u32 ep0buff = 0, tmp;
  1912. switch (reqtype & USB_RECIP_MASK) {
  1913. case USB_RECIP_INTERFACE:
  1914. break; /* Not supported */
  1915. case USB_RECIP_DEVICE:
  1916. ep0buff = (udc->selfpowered << USB_DEVICE_SELF_POWERED);
  1917. if (udc->dev_status & (1 << USB_DEVICE_REMOTE_WAKEUP))
  1918. ep0buff |= (1 << USB_DEVICE_REMOTE_WAKEUP);
  1919. break;
  1920. case USB_RECIP_ENDPOINT:
  1921. tmp = wIndex & USB_ENDPOINT_NUMBER_MASK;
  1922. ep = &udc->ep[tmp];
  1923. if ((tmp == 0) || (tmp >= NUM_ENDPOINTS) || (tmp && !ep->desc))
  1924. return -EOPNOTSUPP;
  1925. if (wIndex & USB_DIR_IN) {
  1926. if (!ep->is_in)
  1927. return -EOPNOTSUPP; /* Something's wrong */
  1928. } else if (ep->is_in)
  1929. return -EOPNOTSUPP; /* Not an IN endpoint */
  1930. /* Get status of the endpoint */
  1931. udc_protocol_cmd_w(udc, CMD_SEL_EP(ep->hwep_num));
  1932. tmp = udc_protocol_cmd_r(udc, DAT_SEL_EP(ep->hwep_num));
  1933. if (tmp & EP_SEL_ST)
  1934. ep0buff = (1 << USB_ENDPOINT_HALT);
  1935. else
  1936. ep0buff = 0;
  1937. break;
  1938. default:
  1939. break;
  1940. }
  1941. /* Return data */
  1942. udc_write_hwep(udc, EP_IN, &ep0buff, 2);
  1943. return 0;
  1944. }
  1945. static void udc_handle_ep0_setup(struct lpc32xx_udc *udc)
  1946. {
  1947. struct lpc32xx_ep *ep, *ep0 = &udc->ep[0];
  1948. struct usb_ctrlrequest ctrlpkt;
  1949. int i, bytes;
  1950. u16 wIndex, wValue, wLength, reqtype, req, tmp;
  1951. /* Nuke previous transfers */
  1952. nuke(ep0, -EPROTO);
  1953. /* Get setup packet */
  1954. bytes = udc_read_hwep(udc, EP_OUT, (u32 *) &ctrlpkt, 8);
  1955. if (bytes != 8) {
  1956. ep_warn(ep0, "Incorrectly sized setup packet (s/b 8, is %d)!\n",
  1957. bytes);
  1958. return;
  1959. }
  1960. /* Native endianness */
  1961. wIndex = le16_to_cpu(ctrlpkt.wIndex);
  1962. wValue = le16_to_cpu(ctrlpkt.wValue);
  1963. wLength = le16_to_cpu(ctrlpkt.wLength);
  1964. reqtype = le16_to_cpu(ctrlpkt.bRequestType);
  1965. /* Set direction of EP0 */
  1966. if (likely(reqtype & USB_DIR_IN))
  1967. ep0->is_in = 1;
  1968. else
  1969. ep0->is_in = 0;
  1970. /* Handle SETUP packet */
  1971. req = le16_to_cpu(ctrlpkt.bRequest);
  1972. switch (req) {
  1973. case USB_REQ_CLEAR_FEATURE:
  1974. case USB_REQ_SET_FEATURE:
  1975. switch (reqtype) {
  1976. case (USB_TYPE_STANDARD | USB_RECIP_DEVICE):
  1977. if (wValue != USB_DEVICE_REMOTE_WAKEUP)
  1978. goto stall; /* Nothing else handled */
  1979. /* Tell board about event */
  1980. if (req == USB_REQ_CLEAR_FEATURE)
  1981. udc->dev_status &=
  1982. ~(1 << USB_DEVICE_REMOTE_WAKEUP);
  1983. else
  1984. udc->dev_status |=
  1985. (1 << USB_DEVICE_REMOTE_WAKEUP);
  1986. uda_remwkp_cgh(udc);
  1987. goto zlp_send;
  1988. case (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
  1989. tmp = wIndex & USB_ENDPOINT_NUMBER_MASK;
  1990. if ((wValue != USB_ENDPOINT_HALT) ||
  1991. (tmp >= NUM_ENDPOINTS))
  1992. break;
  1993. /* Find hardware endpoint from logical endpoint */
  1994. ep = &udc->ep[tmp];
  1995. tmp = ep->hwep_num;
  1996. if (tmp == 0)
  1997. break;
  1998. if (req == USB_REQ_SET_FEATURE)
  1999. udc_stall_hwep(udc, tmp);
  2000. else if (!ep->wedge)
  2001. udc_clrstall_hwep(udc, tmp);
  2002. goto zlp_send;
  2003. default:
  2004. break;
  2005. }
  2006. case USB_REQ_SET_ADDRESS:
  2007. if (reqtype == (USB_TYPE_STANDARD | USB_RECIP_DEVICE)) {
  2008. udc_set_address(udc, wValue);
  2009. goto zlp_send;
  2010. }
  2011. break;
  2012. case USB_REQ_GET_STATUS:
  2013. udc_get_status(udc, reqtype, wIndex);
  2014. return;
  2015. default:
  2016. break; /* Let GadgetFS handle the descriptor instead */
  2017. }
  2018. if (likely(udc->driver)) {
  2019. /* device-2-host (IN) or no data setup command, process
  2020. * immediately */
  2021. spin_unlock(&udc->lock);
  2022. i = udc->driver->setup(&udc->gadget, &ctrlpkt);
  2023. spin_lock(&udc->lock);
  2024. if (req == USB_REQ_SET_CONFIGURATION) {
  2025. /* Configuration is set after endpoints are realized */
  2026. if (wValue) {
  2027. /* Set configuration */
  2028. udc_set_device_configured(udc);
  2029. udc_protocol_cmd_data_w(udc, CMD_SET_MODE,
  2030. DAT_WR_BYTE(AP_CLK |
  2031. INAK_BI | INAK_II));
  2032. } else {
  2033. /* Clear configuration */
  2034. udc_set_device_unconfigured(udc);
  2035. /* Disable NAK interrupts */
  2036. udc_protocol_cmd_data_w(udc, CMD_SET_MODE,
  2037. DAT_WR_BYTE(AP_CLK));
  2038. }
  2039. }
  2040. if (i < 0) {
  2041. /* setup processing failed, force stall */
  2042. dev_err(udc->dev,
  2043. "req %02x.%02x protocol STALL; stat %d\n",
  2044. reqtype, req, i);
  2045. udc->ep0state = WAIT_FOR_SETUP;
  2046. goto stall;
  2047. }
  2048. }
  2049. if (!ep0->is_in)
  2050. udc_ep0_send_zlp(udc); /* ZLP IN packet on data phase */
  2051. return;
  2052. stall:
  2053. udc_stall_hwep(udc, EP_IN);
  2054. return;
  2055. zlp_send:
  2056. udc_ep0_send_zlp(udc);
  2057. return;
  2058. }
  2059. /* IN endpoint 0 transfer */
  2060. static void udc_handle_ep0_in(struct lpc32xx_udc *udc)
  2061. {
  2062. struct lpc32xx_ep *ep0 = &udc->ep[0];
  2063. u32 epstatus;
  2064. /* Clear EP interrupt */
  2065. epstatus = udc_clearep_getsts(udc, EP_IN);
  2066. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  2067. ep0->totalints++;
  2068. #endif
  2069. /* Stalled? Clear stall and reset buffers */
  2070. if (epstatus & EP_SEL_ST) {
  2071. udc_clrstall_hwep(udc, EP_IN);
  2072. nuke(ep0, -ECONNABORTED);
  2073. udc->ep0state = WAIT_FOR_SETUP;
  2074. return;
  2075. }
  2076. /* Is a buffer available? */
  2077. if (!(epstatus & EP_SEL_F)) {
  2078. /* Handle based on current state */
  2079. if (udc->ep0state == DATA_IN)
  2080. udc_ep0_in_req(udc);
  2081. else {
  2082. /* Unknown state for EP0 oe end of DATA IN phase */
  2083. nuke(ep0, -ECONNABORTED);
  2084. udc->ep0state = WAIT_FOR_SETUP;
  2085. }
  2086. }
  2087. }
  2088. /* OUT endpoint 0 transfer */
  2089. static void udc_handle_ep0_out(struct lpc32xx_udc *udc)
  2090. {
  2091. struct lpc32xx_ep *ep0 = &udc->ep[0];
  2092. u32 epstatus;
  2093. /* Clear EP interrupt */
  2094. epstatus = udc_clearep_getsts(udc, EP_OUT);
  2095. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  2096. ep0->totalints++;
  2097. #endif
  2098. /* Stalled? */
  2099. if (epstatus & EP_SEL_ST) {
  2100. udc_clrstall_hwep(udc, EP_OUT);
  2101. nuke(ep0, -ECONNABORTED);
  2102. udc->ep0state = WAIT_FOR_SETUP;
  2103. return;
  2104. }
  2105. /* A NAK may occur if a packet couldn't be received yet */
  2106. if (epstatus & EP_SEL_EPN)
  2107. return;
  2108. /* Setup packet incoming? */
  2109. if (epstatus & EP_SEL_STP) {
  2110. nuke(ep0, 0);
  2111. udc->ep0state = WAIT_FOR_SETUP;
  2112. }
  2113. /* Data available? */
  2114. if (epstatus & EP_SEL_F)
  2115. /* Handle based on current state */
  2116. switch (udc->ep0state) {
  2117. case WAIT_FOR_SETUP:
  2118. udc_handle_ep0_setup(udc);
  2119. break;
  2120. case DATA_OUT:
  2121. udc_ep0_out_req(udc);
  2122. break;
  2123. default:
  2124. /* Unknown state for EP0 */
  2125. nuke(ep0, -ECONNABORTED);
  2126. udc->ep0state = WAIT_FOR_SETUP;
  2127. }
  2128. }
  2129. /* Must be called without lock */
  2130. static int lpc32xx_get_frame(struct usb_gadget *gadget)
  2131. {
  2132. int frame;
  2133. unsigned long flags;
  2134. struct lpc32xx_udc *udc = to_udc(gadget);
  2135. if (!udc->clocked)
  2136. return -EINVAL;
  2137. spin_lock_irqsave(&udc->lock, flags);
  2138. frame = (int) udc_get_current_frame(udc);
  2139. spin_unlock_irqrestore(&udc->lock, flags);
  2140. return frame;
  2141. }
  2142. static int lpc32xx_wakeup(struct usb_gadget *gadget)
  2143. {
  2144. return -ENOTSUPP;
  2145. }
  2146. static int lpc32xx_set_selfpowered(struct usb_gadget *gadget, int is_on)
  2147. {
  2148. struct lpc32xx_udc *udc = to_udc(gadget);
  2149. /* Always self-powered */
  2150. udc->selfpowered = (is_on != 0);
  2151. return 0;
  2152. }
  2153. /*
  2154. * vbus is here! turn everything on that's ready
  2155. * Must be called without lock
  2156. */
  2157. static int lpc32xx_vbus_session(struct usb_gadget *gadget, int is_active)
  2158. {
  2159. unsigned long flags;
  2160. struct lpc32xx_udc *udc = to_udc(gadget);
  2161. spin_lock_irqsave(&udc->lock, flags);
  2162. /* Doesn't need lock */
  2163. if (udc->driver) {
  2164. udc_clk_set(udc, 1);
  2165. udc_enable(udc);
  2166. pullup(udc, is_active);
  2167. } else {
  2168. stop_activity(udc);
  2169. pullup(udc, 0);
  2170. spin_unlock_irqrestore(&udc->lock, flags);
  2171. /*
  2172. * Wait for all the endpoints to disable,
  2173. * before disabling clocks. Don't wait if
  2174. * endpoints are not enabled.
  2175. */
  2176. if (atomic_read(&udc->enabled_ep_cnt))
  2177. wait_event_interruptible(udc->ep_disable_wait_queue,
  2178. (atomic_read(&udc->enabled_ep_cnt) == 0));
  2179. spin_lock_irqsave(&udc->lock, flags);
  2180. udc_clk_set(udc, 0);
  2181. }
  2182. spin_unlock_irqrestore(&udc->lock, flags);
  2183. return 0;
  2184. }
  2185. /* Can be called with or without lock */
  2186. static int lpc32xx_pullup(struct usb_gadget *gadget, int is_on)
  2187. {
  2188. struct lpc32xx_udc *udc = to_udc(gadget);
  2189. /* Doesn't need lock */
  2190. pullup(udc, is_on);
  2191. return 0;
  2192. }
  2193. static int lpc32xx_start(struct usb_gadget_driver *driver,
  2194. int (*bind)(struct usb_gadget *));
  2195. static int lpc32xx_stop(struct usb_gadget_driver *driver);
  2196. static const struct usb_gadget_ops lpc32xx_udc_ops = {
  2197. .get_frame = lpc32xx_get_frame,
  2198. .wakeup = lpc32xx_wakeup,
  2199. .set_selfpowered = lpc32xx_set_selfpowered,
  2200. .vbus_session = lpc32xx_vbus_session,
  2201. .pullup = lpc32xx_pullup,
  2202. .start = lpc32xx_start,
  2203. .stop = lpc32xx_stop,
  2204. };
  2205. static void nop_release(struct device *dev)
  2206. {
  2207. /* nothing to free */
  2208. }
  2209. static struct lpc32xx_udc controller = {
  2210. .gadget = {
  2211. .ops = &lpc32xx_udc_ops,
  2212. .ep0 = &controller.ep[0].ep,
  2213. .name = driver_name,
  2214. .dev = {
  2215. .init_name = "gadget",
  2216. .release = nop_release,
  2217. }
  2218. },
  2219. .ep[0] = {
  2220. .ep = {
  2221. .name = "ep0",
  2222. .ops = &lpc32xx_ep_ops,
  2223. },
  2224. .udc = &controller,
  2225. .maxpacket = 64,
  2226. .hwep_num_base = 0,
  2227. .hwep_num = 0, /* Can be 0 or 1, has special handling */
  2228. .lep = 0,
  2229. .eptype = EP_CTL_TYPE,
  2230. },
  2231. .ep[1] = {
  2232. .ep = {
  2233. .name = "ep1-int",
  2234. .ops = &lpc32xx_ep_ops,
  2235. },
  2236. .udc = &controller,
  2237. .maxpacket = 64,
  2238. .hwep_num_base = 2,
  2239. .hwep_num = 0, /* 2 or 3, will be set later */
  2240. .lep = 1,
  2241. .eptype = EP_INT_TYPE,
  2242. },
  2243. .ep[2] = {
  2244. .ep = {
  2245. .name = "ep2-bulk",
  2246. .ops = &lpc32xx_ep_ops,
  2247. },
  2248. .udc = &controller,
  2249. .maxpacket = 64,
  2250. .hwep_num_base = 4,
  2251. .hwep_num = 0, /* 4 or 5, will be set later */
  2252. .lep = 2,
  2253. .eptype = EP_BLK_TYPE,
  2254. },
  2255. .ep[3] = {
  2256. .ep = {
  2257. .name = "ep3-iso",
  2258. .ops = &lpc32xx_ep_ops,
  2259. },
  2260. .udc = &controller,
  2261. .maxpacket = 1023,
  2262. .hwep_num_base = 6,
  2263. .hwep_num = 0, /* 6 or 7, will be set later */
  2264. .lep = 3,
  2265. .eptype = EP_ISO_TYPE,
  2266. },
  2267. .ep[4] = {
  2268. .ep = {
  2269. .name = "ep4-int",
  2270. .ops = &lpc32xx_ep_ops,
  2271. },
  2272. .udc = &controller,
  2273. .maxpacket = 64,
  2274. .hwep_num_base = 8,
  2275. .hwep_num = 0, /* 8 or 9, will be set later */
  2276. .lep = 4,
  2277. .eptype = EP_INT_TYPE,
  2278. },
  2279. .ep[5] = {
  2280. .ep = {
  2281. .name = "ep5-bulk",
  2282. .ops = &lpc32xx_ep_ops,
  2283. },
  2284. .udc = &controller,
  2285. .maxpacket = 64,
  2286. .hwep_num_base = 10,
  2287. .hwep_num = 0, /* 10 or 11, will be set later */
  2288. .lep = 5,
  2289. .eptype = EP_BLK_TYPE,
  2290. },
  2291. .ep[6] = {
  2292. .ep = {
  2293. .name = "ep6-iso",
  2294. .ops = &lpc32xx_ep_ops,
  2295. },
  2296. .udc = &controller,
  2297. .maxpacket = 1023,
  2298. .hwep_num_base = 12,
  2299. .hwep_num = 0, /* 12 or 13, will be set later */
  2300. .lep = 6,
  2301. .eptype = EP_ISO_TYPE,
  2302. },
  2303. .ep[7] = {
  2304. .ep = {
  2305. .name = "ep7-int",
  2306. .ops = &lpc32xx_ep_ops,
  2307. },
  2308. .udc = &controller,
  2309. .maxpacket = 64,
  2310. .hwep_num_base = 14,
  2311. .hwep_num = 0,
  2312. .lep = 7,
  2313. .eptype = EP_INT_TYPE,
  2314. },
  2315. .ep[8] = {
  2316. .ep = {
  2317. .name = "ep8-bulk",
  2318. .ops = &lpc32xx_ep_ops,
  2319. },
  2320. .udc = &controller,
  2321. .maxpacket = 64,
  2322. .hwep_num_base = 16,
  2323. .hwep_num = 0,
  2324. .lep = 8,
  2325. .eptype = EP_BLK_TYPE,
  2326. },
  2327. .ep[9] = {
  2328. .ep = {
  2329. .name = "ep9-iso",
  2330. .ops = &lpc32xx_ep_ops,
  2331. },
  2332. .udc = &controller,
  2333. .maxpacket = 1023,
  2334. .hwep_num_base = 18,
  2335. .hwep_num = 0,
  2336. .lep = 9,
  2337. .eptype = EP_ISO_TYPE,
  2338. },
  2339. .ep[10] = {
  2340. .ep = {
  2341. .name = "ep10-int",
  2342. .ops = &lpc32xx_ep_ops,
  2343. },
  2344. .udc = &controller,
  2345. .maxpacket = 64,
  2346. .hwep_num_base = 20,
  2347. .hwep_num = 0,
  2348. .lep = 10,
  2349. .eptype = EP_INT_TYPE,
  2350. },
  2351. .ep[11] = {
  2352. .ep = {
  2353. .name = "ep11-bulk",
  2354. .ops = &lpc32xx_ep_ops,
  2355. },
  2356. .udc = &controller,
  2357. .maxpacket = 64,
  2358. .hwep_num_base = 22,
  2359. .hwep_num = 0,
  2360. .lep = 11,
  2361. .eptype = EP_BLK_TYPE,
  2362. },
  2363. .ep[12] = {
  2364. .ep = {
  2365. .name = "ep12-iso",
  2366. .ops = &lpc32xx_ep_ops,
  2367. },
  2368. .udc = &controller,
  2369. .maxpacket = 1023,
  2370. .hwep_num_base = 24,
  2371. .hwep_num = 0,
  2372. .lep = 12,
  2373. .eptype = EP_ISO_TYPE,
  2374. },
  2375. .ep[13] = {
  2376. .ep = {
  2377. .name = "ep13-int",
  2378. .ops = &lpc32xx_ep_ops,
  2379. },
  2380. .udc = &controller,
  2381. .maxpacket = 64,
  2382. .hwep_num_base = 26,
  2383. .hwep_num = 0,
  2384. .lep = 13,
  2385. .eptype = EP_INT_TYPE,
  2386. },
  2387. .ep[14] = {
  2388. .ep = {
  2389. .name = "ep14-bulk",
  2390. .ops = &lpc32xx_ep_ops,
  2391. },
  2392. .udc = &controller,
  2393. .maxpacket = 64,
  2394. .hwep_num_base = 28,
  2395. .hwep_num = 0,
  2396. .lep = 14,
  2397. .eptype = EP_BLK_TYPE,
  2398. },
  2399. .ep[15] = {
  2400. .ep = {
  2401. .name = "ep15-bulk",
  2402. .ops = &lpc32xx_ep_ops,
  2403. },
  2404. .udc = &controller,
  2405. .maxpacket = 1023,
  2406. .hwep_num_base = 30,
  2407. .hwep_num = 0,
  2408. .lep = 15,
  2409. .eptype = EP_BLK_TYPE,
  2410. },
  2411. };
  2412. /* ISO and status interrupts */
  2413. static irqreturn_t lpc32xx_usb_lp_irq(int irq, void *_udc)
  2414. {
  2415. u32 tmp, devstat;
  2416. struct lpc32xx_udc *udc = _udc;
  2417. spin_lock(&udc->lock);
  2418. /* Read the device status register */
  2419. devstat = readl(USBD_DEVINTST(udc->udp_baseaddr));
  2420. devstat &= ~USBD_EP_FAST;
  2421. writel(devstat, USBD_DEVINTCLR(udc->udp_baseaddr));
  2422. devstat = devstat & udc->enabled_devints;
  2423. /* Device specific handling needed? */
  2424. if (devstat & USBD_DEV_STAT)
  2425. udc_handle_dev(udc);
  2426. /* Start of frame? (devstat & FRAME_INT):
  2427. * The frame interrupt isn't really needed for ISO support,
  2428. * as the driver will queue the necessary packets */
  2429. /* Error? */
  2430. if (devstat & ERR_INT) {
  2431. /* All types of errors, from cable removal during transfer to
  2432. * misc protocol and bit errors. These are mostly for just info,
  2433. * as the USB hardware will work around these. If these errors
  2434. * happen alot, something is wrong. */
  2435. udc_protocol_cmd_w(udc, CMD_RD_ERR_STAT);
  2436. tmp = udc_protocol_cmd_r(udc, DAT_RD_ERR_STAT);
  2437. dev_dbg(udc->dev, "Device error (0x%x)!\n", tmp);
  2438. }
  2439. spin_unlock(&udc->lock);
  2440. return IRQ_HANDLED;
  2441. }
  2442. /* EP interrupts */
  2443. static irqreturn_t lpc32xx_usb_hp_irq(int irq, void *_udc)
  2444. {
  2445. u32 tmp;
  2446. struct lpc32xx_udc *udc = _udc;
  2447. spin_lock(&udc->lock);
  2448. /* Read the device status register */
  2449. writel(USBD_EP_FAST, USBD_DEVINTCLR(udc->udp_baseaddr));
  2450. /* Endpoints */
  2451. tmp = readl(USBD_EPINTST(udc->udp_baseaddr));
  2452. /* Special handling for EP0 */
  2453. if (tmp & (EP_MASK_SEL(0, EP_OUT) | EP_MASK_SEL(0, EP_IN))) {
  2454. /* Handle EP0 IN */
  2455. if (tmp & (EP_MASK_SEL(0, EP_IN)))
  2456. udc_handle_ep0_in(udc);
  2457. /* Handle EP0 OUT */
  2458. if (tmp & (EP_MASK_SEL(0, EP_OUT)))
  2459. udc_handle_ep0_out(udc);
  2460. }
  2461. /* All other EPs */
  2462. if (tmp & ~(EP_MASK_SEL(0, EP_OUT) | EP_MASK_SEL(0, EP_IN))) {
  2463. int i;
  2464. /* Handle other EP interrupts */
  2465. for (i = 1; i < NUM_ENDPOINTS; i++) {
  2466. if (tmp & (1 << udc->ep[i].hwep_num))
  2467. udc_handle_eps(udc, &udc->ep[i]);
  2468. }
  2469. }
  2470. spin_unlock(&udc->lock);
  2471. return IRQ_HANDLED;
  2472. }
  2473. static irqreturn_t lpc32xx_usb_devdma_irq(int irq, void *_udc)
  2474. {
  2475. struct lpc32xx_udc *udc = _udc;
  2476. int i;
  2477. u32 tmp;
  2478. spin_lock(&udc->lock);
  2479. /* Handle EP DMA EOT interrupts */
  2480. tmp = readl(USBD_EOTINTST(udc->udp_baseaddr)) |
  2481. (readl(USBD_EPDMAST(udc->udp_baseaddr)) &
  2482. readl(USBD_NDDRTINTST(udc->udp_baseaddr))) |
  2483. readl(USBD_SYSERRTINTST(udc->udp_baseaddr));
  2484. for (i = 1; i < NUM_ENDPOINTS; i++) {
  2485. if (tmp & (1 << udc->ep[i].hwep_num))
  2486. udc_handle_dma_ep(udc, &udc->ep[i]);
  2487. }
  2488. spin_unlock(&udc->lock);
  2489. return IRQ_HANDLED;
  2490. }
  2491. /*
  2492. *
  2493. * VBUS detection, pullup handler, and Gadget cable state notification
  2494. *
  2495. */
  2496. static void vbus_work(struct work_struct *work)
  2497. {
  2498. u8 value;
  2499. struct lpc32xx_udc *udc = container_of(work, struct lpc32xx_udc,
  2500. vbus_job);
  2501. if (udc->enabled != 0) {
  2502. /* Discharge VBUS real quick */
  2503. i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
  2504. ISP1301_I2C_OTG_CONTROL_1, OTG1_VBUS_DISCHRG);
  2505. /* Give VBUS some time (100mS) to discharge */
  2506. msleep(100);
  2507. /* Disable VBUS discharge resistor */
  2508. i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
  2509. ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
  2510. OTG1_VBUS_DISCHRG);
  2511. /* Clear interrupt */
  2512. i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
  2513. ISP1301_I2C_INTERRUPT_LATCH |
  2514. ISP1301_I2C_REG_CLEAR_ADDR, ~0);
  2515. /* Get the VBUS status from the transceiver */
  2516. value = i2c_smbus_read_byte_data(udc->isp1301_i2c_client,
  2517. ISP1301_I2C_OTG_CONTROL_2);
  2518. /* VBUS on or off? */
  2519. if (value & OTG_B_SESS_VLD)
  2520. udc->vbus = 1;
  2521. else
  2522. udc->vbus = 0;
  2523. /* VBUS changed? */
  2524. if (udc->last_vbus != udc->vbus) {
  2525. udc->last_vbus = udc->vbus;
  2526. lpc32xx_vbus_session(&udc->gadget, udc->vbus);
  2527. }
  2528. }
  2529. /* Re-enable after completion */
  2530. enable_irq(udc->udp_irq[IRQ_USB_ATX]);
  2531. }
  2532. static irqreturn_t lpc32xx_usb_vbus_irq(int irq, void *_udc)
  2533. {
  2534. struct lpc32xx_udc *udc = _udc;
  2535. /* Defer handling of VBUS IRQ to work queue */
  2536. disable_irq_nosync(udc->udp_irq[IRQ_USB_ATX]);
  2537. schedule_work(&udc->vbus_job);
  2538. return IRQ_HANDLED;
  2539. }
  2540. static int lpc32xx_start(struct usb_gadget_driver *driver,
  2541. int (*bind)(struct usb_gadget *))
  2542. {
  2543. struct lpc32xx_udc *udc = &controller;
  2544. int retval, i;
  2545. if (!driver || driver->max_speed < USB_SPEED_FULL ||
  2546. !bind || !driver->setup) {
  2547. dev_err(udc->dev, "bad parameter.\n");
  2548. return -EINVAL;
  2549. }
  2550. if (udc->driver) {
  2551. dev_err(udc->dev, "UDC already has a gadget driver\n");
  2552. return -EBUSY;
  2553. }
  2554. udc->driver = driver;
  2555. udc->gadget.dev.driver = &driver->driver;
  2556. udc->enabled = 1;
  2557. udc->selfpowered = 1;
  2558. udc->vbus = 0;
  2559. retval = bind(&udc->gadget);
  2560. if (retval) {
  2561. dev_err(udc->dev, "bind() returned %d\n", retval);
  2562. udc->enabled = 0;
  2563. udc->selfpowered = 0;
  2564. udc->driver = NULL;
  2565. udc->gadget.dev.driver = NULL;
  2566. return retval;
  2567. }
  2568. dev_dbg(udc->dev, "bound to %s\n", driver->driver.name);
  2569. /* Force VBUS process once to check for cable insertion */
  2570. udc->last_vbus = udc->vbus = 0;
  2571. schedule_work(&udc->vbus_job);
  2572. /* Do not re-enable ATX IRQ (3) */
  2573. for (i = IRQ_USB_LP; i < IRQ_USB_ATX; i++)
  2574. enable_irq(udc->udp_irq[i]);
  2575. return 0;
  2576. }
  2577. static int lpc32xx_stop(struct usb_gadget_driver *driver)
  2578. {
  2579. int i;
  2580. struct lpc32xx_udc *udc = &controller;
  2581. if (!driver || driver != udc->driver || !driver->unbind)
  2582. return -EINVAL;
  2583. /* Disable USB pullup */
  2584. isp1301_pullup_enable(udc, 0, 1);
  2585. for (i = IRQ_USB_LP; i <= IRQ_USB_ATX; i++)
  2586. disable_irq(udc->udp_irq[i]);
  2587. if (udc->clocked) {
  2588. spin_lock(&udc->lock);
  2589. stop_activity(udc);
  2590. spin_unlock(&udc->lock);
  2591. /*
  2592. * Wait for all the endpoints to disable,
  2593. * before disabling clocks. Don't wait if
  2594. * endpoints are not enabled.
  2595. */
  2596. if (atomic_read(&udc->enabled_ep_cnt))
  2597. wait_event_interruptible(udc->ep_disable_wait_queue,
  2598. (atomic_read(&udc->enabled_ep_cnt) == 0));
  2599. spin_lock(&udc->lock);
  2600. udc_clk_set(udc, 0);
  2601. spin_unlock(&udc->lock);
  2602. }
  2603. udc->enabled = 0;
  2604. pullup(udc, 0);
  2605. driver->unbind(&udc->gadget);
  2606. udc->gadget.dev.driver = NULL;
  2607. udc->driver = NULL;
  2608. dev_dbg(udc->dev, "unbound from %s\n", driver->driver.name);
  2609. return 0;
  2610. }
  2611. static void lpc32xx_udc_shutdown(struct platform_device *dev)
  2612. {
  2613. /* Force disconnect on reboot */
  2614. struct lpc32xx_udc *udc = &controller;
  2615. pullup(udc, 0);
  2616. }
  2617. /*
  2618. * Callbacks to be overridden by options passed via OF (TODO)
  2619. */
  2620. static void lpc32xx_usbd_conn_chg(int conn)
  2621. {
  2622. /* Do nothing, it might be nice to enable an LED
  2623. * based on conn state being !0 */
  2624. }
  2625. static void lpc32xx_usbd_susp_chg(int susp)
  2626. {
  2627. /* Device suspend if susp != 0 */
  2628. }
  2629. static void lpc32xx_rmwkup_chg(int remote_wakup_enable)
  2630. {
  2631. /* Enable or disable USB remote wakeup */
  2632. }
  2633. struct lpc32xx_usbd_cfg lpc32xx_usbddata = {
  2634. .vbus_drv_pol = 0,
  2635. .conn_chgb = &lpc32xx_usbd_conn_chg,
  2636. .susp_chgb = &lpc32xx_usbd_susp_chg,
  2637. .rmwk_chgb = &lpc32xx_rmwkup_chg,
  2638. };
  2639. static u64 lpc32xx_usbd_dmamask = ~(u32) 0x7F;
  2640. static int __init lpc32xx_udc_probe(struct platform_device *pdev)
  2641. {
  2642. struct device *dev = &pdev->dev;
  2643. struct lpc32xx_udc *udc = &controller;
  2644. int retval, i;
  2645. struct resource *res;
  2646. dma_addr_t dma_handle;
  2647. struct device_node *isp1301_node;
  2648. /* init software state */
  2649. udc->gadget.dev.parent = dev;
  2650. udc->pdev = pdev;
  2651. udc->dev = &pdev->dev;
  2652. udc->enabled = 0;
  2653. if (pdev->dev.of_node) {
  2654. isp1301_node = of_parse_phandle(pdev->dev.of_node,
  2655. "transceiver", 0);
  2656. } else {
  2657. isp1301_node = NULL;
  2658. }
  2659. udc->isp1301_i2c_client = isp1301_get_client(isp1301_node);
  2660. if (!udc->isp1301_i2c_client)
  2661. return -EPROBE_DEFER;
  2662. dev_info(udc->dev, "ISP1301 I2C device at address 0x%x\n",
  2663. udc->isp1301_i2c_client->addr);
  2664. pdev->dev.dma_mask = &lpc32xx_usbd_dmamask;
  2665. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  2666. udc->board = &lpc32xx_usbddata;
  2667. /*
  2668. * Resources are mapped as follows:
  2669. * IORESOURCE_MEM, base address and size of USB space
  2670. * IORESOURCE_IRQ, USB device low priority interrupt number
  2671. * IORESOURCE_IRQ, USB device high priority interrupt number
  2672. * IORESOURCE_IRQ, USB device interrupt number
  2673. * IORESOURCE_IRQ, USB transceiver interrupt number
  2674. */
  2675. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2676. if (!res)
  2677. return -ENXIO;
  2678. spin_lock_init(&udc->lock);
  2679. /* Get IRQs */
  2680. for (i = 0; i < 4; i++) {
  2681. udc->udp_irq[i] = platform_get_irq(pdev, i);
  2682. if (udc->udp_irq[i] < 0) {
  2683. dev_err(udc->dev,
  2684. "irq resource %d not available!\n", i);
  2685. return udc->udp_irq[i];
  2686. }
  2687. }
  2688. udc->io_p_start = res->start;
  2689. udc->io_p_size = resource_size(res);
  2690. if (!request_mem_region(udc->io_p_start, udc->io_p_size, driver_name)) {
  2691. dev_err(udc->dev, "someone's using UDC memory\n");
  2692. return -EBUSY;
  2693. }
  2694. udc->udp_baseaddr = ioremap(udc->io_p_start, udc->io_p_size);
  2695. if (!udc->udp_baseaddr) {
  2696. retval = -ENOMEM;
  2697. dev_err(udc->dev, "IO map failure\n");
  2698. goto io_map_fail;
  2699. }
  2700. /* Enable AHB slave USB clock, needed for further USB clock control */
  2701. writel(USB_SLAVE_HCLK_EN | (1 << 19), USB_CTRL);
  2702. /* Get required clocks */
  2703. udc->usb_pll_clk = clk_get(&pdev->dev, "ck_pll5");
  2704. if (IS_ERR(udc->usb_pll_clk)) {
  2705. dev_err(udc->dev, "failed to acquire USB PLL\n");
  2706. retval = PTR_ERR(udc->usb_pll_clk);
  2707. goto pll_get_fail;
  2708. }
  2709. udc->usb_slv_clk = clk_get(&pdev->dev, "ck_usbd");
  2710. if (IS_ERR(udc->usb_slv_clk)) {
  2711. dev_err(udc->dev, "failed to acquire USB device clock\n");
  2712. retval = PTR_ERR(udc->usb_slv_clk);
  2713. goto usb_clk_get_fail;
  2714. }
  2715. /* Setup PLL clock to 48MHz */
  2716. retval = clk_enable(udc->usb_pll_clk);
  2717. if (retval < 0) {
  2718. dev_err(udc->dev, "failed to start USB PLL\n");
  2719. goto pll_enable_fail;
  2720. }
  2721. retval = clk_set_rate(udc->usb_pll_clk, 48000);
  2722. if (retval < 0) {
  2723. dev_err(udc->dev, "failed to set USB clock rate\n");
  2724. goto pll_set_fail;
  2725. }
  2726. writel(readl(USB_CTRL) | USB_DEV_NEED_CLK_EN, USB_CTRL);
  2727. /* Enable USB device clock */
  2728. retval = clk_enable(udc->usb_slv_clk);
  2729. if (retval < 0) {
  2730. dev_err(udc->dev, "failed to start USB device clock\n");
  2731. goto usb_clk_enable_fail;
  2732. }
  2733. /* Set to enable all needed USB OTG clocks */
  2734. writel(USB_CLOCK_MASK, USB_OTG_CLK_CTRL(udc));
  2735. i = 1000;
  2736. while (((readl(USB_OTG_CLK_STAT(udc)) & USB_CLOCK_MASK) !=
  2737. USB_CLOCK_MASK) && (i > 0))
  2738. i--;
  2739. if (!i)
  2740. dev_dbg(udc->dev, "USB OTG clocks not correctly enabled\n");
  2741. /* Setup deferred workqueue data */
  2742. udc->poweron = udc->pullup = 0;
  2743. INIT_WORK(&udc->pullup_job, pullup_work);
  2744. INIT_WORK(&udc->vbus_job, vbus_work);
  2745. #ifdef CONFIG_PM
  2746. INIT_WORK(&udc->power_job, power_work);
  2747. #endif
  2748. /* All clocks are now on */
  2749. udc->clocked = 1;
  2750. isp1301_udc_configure(udc);
  2751. /* Allocate memory for the UDCA */
  2752. udc->udca_v_base = dma_alloc_coherent(&pdev->dev, UDCA_BUFF_SIZE,
  2753. &dma_handle,
  2754. (GFP_KERNEL | GFP_DMA));
  2755. if (!udc->udca_v_base) {
  2756. dev_err(udc->dev, "error getting UDCA region\n");
  2757. retval = -ENOMEM;
  2758. goto i2c_fail;
  2759. }
  2760. udc->udca_p_base = dma_handle;
  2761. dev_dbg(udc->dev, "DMA buffer(0x%x bytes), P:0x%08x, V:0x%p\n",
  2762. UDCA_BUFF_SIZE, udc->udca_p_base, udc->udca_v_base);
  2763. /* Setup the DD DMA memory pool */
  2764. udc->dd_cache = dma_pool_create("udc_dd", udc->dev,
  2765. sizeof(struct lpc32xx_usbd_dd_gad),
  2766. sizeof(u32), 0);
  2767. if (!udc->dd_cache) {
  2768. dev_err(udc->dev, "error getting DD DMA region\n");
  2769. retval = -ENOMEM;
  2770. goto dma_alloc_fail;
  2771. }
  2772. /* Clear USB peripheral and initialize gadget endpoints */
  2773. udc_disable(udc);
  2774. udc_reinit(udc);
  2775. retval = device_register(&udc->gadget.dev);
  2776. if (retval < 0) {
  2777. dev_err(udc->dev, "Device registration failure\n");
  2778. goto dev_register_fail;
  2779. }
  2780. /* Request IRQs - low and high priority USB device IRQs are routed to
  2781. * the same handler, while the DMA interrupt is routed elsewhere */
  2782. retval = request_irq(udc->udp_irq[IRQ_USB_LP], lpc32xx_usb_lp_irq,
  2783. 0, "udc_lp", udc);
  2784. if (retval < 0) {
  2785. dev_err(udc->dev, "LP request irq %d failed\n",
  2786. udc->udp_irq[IRQ_USB_LP]);
  2787. goto irq_lp_fail;
  2788. }
  2789. retval = request_irq(udc->udp_irq[IRQ_USB_HP], lpc32xx_usb_hp_irq,
  2790. 0, "udc_hp", udc);
  2791. if (retval < 0) {
  2792. dev_err(udc->dev, "HP request irq %d failed\n",
  2793. udc->udp_irq[IRQ_USB_HP]);
  2794. goto irq_hp_fail;
  2795. }
  2796. retval = request_irq(udc->udp_irq[IRQ_USB_DEVDMA],
  2797. lpc32xx_usb_devdma_irq, 0, "udc_dma", udc);
  2798. if (retval < 0) {
  2799. dev_err(udc->dev, "DEV request irq %d failed\n",
  2800. udc->udp_irq[IRQ_USB_DEVDMA]);
  2801. goto irq_dev_fail;
  2802. }
  2803. /* The transceiver interrupt is used for VBUS detection and will
  2804. kick off the VBUS handler function */
  2805. retval = request_irq(udc->udp_irq[IRQ_USB_ATX], lpc32xx_usb_vbus_irq,
  2806. 0, "udc_otg", udc);
  2807. if (retval < 0) {
  2808. dev_err(udc->dev, "VBUS request irq %d failed\n",
  2809. udc->udp_irq[IRQ_USB_ATX]);
  2810. goto irq_xcvr_fail;
  2811. }
  2812. /* Initialize wait queue */
  2813. init_waitqueue_head(&udc->ep_disable_wait_queue);
  2814. atomic_set(&udc->enabled_ep_cnt, 0);
  2815. /* Keep all IRQs disabled until GadgetFS starts up */
  2816. for (i = IRQ_USB_LP; i <= IRQ_USB_ATX; i++)
  2817. disable_irq(udc->udp_irq[i]);
  2818. retval = usb_add_gadget_udc(dev, &udc->gadget);
  2819. if (retval < 0)
  2820. goto add_gadget_fail;
  2821. dev_set_drvdata(dev, udc);
  2822. device_init_wakeup(dev, 1);
  2823. create_debug_file(udc);
  2824. /* Disable clocks for now */
  2825. udc_clk_set(udc, 0);
  2826. dev_info(udc->dev, "%s version %s\n", driver_name, DRIVER_VERSION);
  2827. return 0;
  2828. add_gadget_fail:
  2829. free_irq(udc->udp_irq[IRQ_USB_ATX], udc);
  2830. irq_xcvr_fail:
  2831. free_irq(udc->udp_irq[IRQ_USB_DEVDMA], udc);
  2832. irq_dev_fail:
  2833. free_irq(udc->udp_irq[IRQ_USB_HP], udc);
  2834. irq_hp_fail:
  2835. free_irq(udc->udp_irq[IRQ_USB_LP], udc);
  2836. irq_lp_fail:
  2837. device_unregister(&udc->gadget.dev);
  2838. dev_register_fail:
  2839. dma_pool_destroy(udc->dd_cache);
  2840. dma_alloc_fail:
  2841. dma_free_coherent(&pdev->dev, UDCA_BUFF_SIZE,
  2842. udc->udca_v_base, udc->udca_p_base);
  2843. i2c_fail:
  2844. clk_disable(udc->usb_slv_clk);
  2845. usb_clk_enable_fail:
  2846. pll_set_fail:
  2847. clk_disable(udc->usb_pll_clk);
  2848. pll_enable_fail:
  2849. clk_put(udc->usb_slv_clk);
  2850. usb_clk_get_fail:
  2851. clk_put(udc->usb_pll_clk);
  2852. pll_get_fail:
  2853. iounmap(udc->udp_baseaddr);
  2854. io_map_fail:
  2855. release_mem_region(udc->io_p_start, udc->io_p_size);
  2856. dev_err(udc->dev, "%s probe failed, %d\n", driver_name, retval);
  2857. return retval;
  2858. }
  2859. static int __devexit lpc32xx_udc_remove(struct platform_device *pdev)
  2860. {
  2861. struct lpc32xx_udc *udc = platform_get_drvdata(pdev);
  2862. usb_del_gadget_udc(&udc->gadget);
  2863. if (udc->driver)
  2864. return -EBUSY;
  2865. udc_clk_set(udc, 1);
  2866. udc_disable(udc);
  2867. pullup(udc, 0);
  2868. free_irq(udc->udp_irq[IRQ_USB_ATX], udc);
  2869. device_init_wakeup(&pdev->dev, 0);
  2870. remove_debug_file(udc);
  2871. dma_pool_destroy(udc->dd_cache);
  2872. dma_free_coherent(&pdev->dev, UDCA_BUFF_SIZE,
  2873. udc->udca_v_base, udc->udca_p_base);
  2874. free_irq(udc->udp_irq[IRQ_USB_DEVDMA], udc);
  2875. free_irq(udc->udp_irq[IRQ_USB_HP], udc);
  2876. free_irq(udc->udp_irq[IRQ_USB_LP], udc);
  2877. device_unregister(&udc->gadget.dev);
  2878. clk_disable(udc->usb_slv_clk);
  2879. clk_put(udc->usb_slv_clk);
  2880. clk_disable(udc->usb_pll_clk);
  2881. clk_put(udc->usb_pll_clk);
  2882. iounmap(udc->udp_baseaddr);
  2883. release_mem_region(udc->io_p_start, udc->io_p_size);
  2884. return 0;
  2885. }
  2886. #ifdef CONFIG_PM
  2887. static int lpc32xx_udc_suspend(struct platform_device *pdev, pm_message_t mesg)
  2888. {
  2889. int to = 1000;
  2890. struct lpc32xx_udc *udc = platform_get_drvdata(pdev);
  2891. if (udc->clocked) {
  2892. /* Power down ISP */
  2893. udc->poweron = 0;
  2894. isp1301_set_powerstate(udc, 0);
  2895. /* Disable clocking */
  2896. udc_clk_set(udc, 0);
  2897. /* Keep clock flag on, so we know to re-enable clocks
  2898. on resume */
  2899. udc->clocked = 1;
  2900. /* Kill OTG and I2C clocks */
  2901. writel(0, USB_OTG_CLK_CTRL(udc));
  2902. while (((readl(USB_OTG_CLK_STAT(udc)) & OTGOFF_CLK_MASK) !=
  2903. OTGOFF_CLK_MASK) && (to > 0))
  2904. to--;
  2905. if (!to)
  2906. dev_dbg(udc->dev,
  2907. "USB OTG clocks not correctly enabled\n");
  2908. /* Kill global USB clock */
  2909. clk_disable(udc->usb_slv_clk);
  2910. }
  2911. return 0;
  2912. }
  2913. static int lpc32xx_udc_resume(struct platform_device *pdev)
  2914. {
  2915. struct lpc32xx_udc *udc = platform_get_drvdata(pdev);
  2916. if (udc->clocked) {
  2917. /* Enable global USB clock */
  2918. clk_enable(udc->usb_slv_clk);
  2919. /* Enable clocking */
  2920. udc_clk_set(udc, 1);
  2921. /* ISP back to normal power mode */
  2922. udc->poweron = 1;
  2923. isp1301_set_powerstate(udc, 1);
  2924. }
  2925. return 0;
  2926. }
  2927. #else
  2928. #define lpc32xx_udc_suspend NULL
  2929. #define lpc32xx_udc_resume NULL
  2930. #endif
  2931. #ifdef CONFIG_OF
  2932. static struct of_device_id lpc32xx_udc_of_match[] = {
  2933. { .compatible = "nxp,lpc3220-udc", },
  2934. { },
  2935. };
  2936. MODULE_DEVICE_TABLE(of, lpc32xx_udc_of_match);
  2937. #endif
  2938. static struct platform_driver lpc32xx_udc_driver = {
  2939. .remove = __devexit_p(lpc32xx_udc_remove),
  2940. .shutdown = lpc32xx_udc_shutdown,
  2941. .suspend = lpc32xx_udc_suspend,
  2942. .resume = lpc32xx_udc_resume,
  2943. .driver = {
  2944. .name = (char *) driver_name,
  2945. .owner = THIS_MODULE,
  2946. .of_match_table = of_match_ptr(lpc32xx_udc_of_match),
  2947. },
  2948. };
  2949. static int __init udc_init_module(void)
  2950. {
  2951. return platform_driver_probe(&lpc32xx_udc_driver, lpc32xx_udc_probe);
  2952. }
  2953. module_init(udc_init_module);
  2954. static void __exit udc_exit_module(void)
  2955. {
  2956. platform_driver_unregister(&lpc32xx_udc_driver);
  2957. }
  2958. module_exit(udc_exit_module);
  2959. MODULE_DESCRIPTION("LPC32XX udc driver");
  2960. MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com>");
  2961. MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
  2962. MODULE_LICENSE("GPL");
  2963. MODULE_ALIAS("platform:lpc32xx_udc");