lpc32xx_udc.c 85 KB

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