lpc32xx_udc.c 83 KB

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