lpc32xx_udc.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477
  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(inode)->data);
  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. enum dma_data_direction direction;
  1243. if (ep->is_in)
  1244. direction = DMA_TO_DEVICE;
  1245. else
  1246. direction = DMA_FROM_DEVICE;
  1247. if (req->mapped) {
  1248. dma_unmap_single(ep->udc->gadget.dev.parent,
  1249. req->req.dma, req->req.length,
  1250. direction);
  1251. req->req.dma = 0;
  1252. req->mapped = 0;
  1253. } else
  1254. dma_sync_single_for_cpu(ep->udc->gadget.dev.parent,
  1255. req->req.dma, req->req.length,
  1256. direction);
  1257. /* Free DDs */
  1258. udc_dd_free(udc, req->dd_desc_ptr);
  1259. }
  1260. if (status && status != -ESHUTDOWN)
  1261. ep_dbg(ep, "%s done %p, status %d\n", ep->ep.name, req, status);
  1262. ep->req_pending = 0;
  1263. spin_unlock(&udc->lock);
  1264. req->req.complete(&ep->ep, &req->req);
  1265. spin_lock(&udc->lock);
  1266. }
  1267. /* Must be called with lock */
  1268. static void nuke(struct lpc32xx_ep *ep, int status)
  1269. {
  1270. struct lpc32xx_request *req;
  1271. while (!list_empty(&ep->queue)) {
  1272. req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
  1273. done(ep, req, status);
  1274. }
  1275. if (status == -ESHUTDOWN) {
  1276. uda_disable_hwepint(ep->udc, ep->hwep_num);
  1277. udc_disable_hwep(ep->udc, ep->hwep_num);
  1278. }
  1279. }
  1280. /* IN endpoint 0 transfer */
  1281. static int udc_ep0_in_req(struct lpc32xx_udc *udc)
  1282. {
  1283. struct lpc32xx_request *req;
  1284. struct lpc32xx_ep *ep0 = &udc->ep[0];
  1285. u32 tsend, ts = 0;
  1286. if (list_empty(&ep0->queue))
  1287. /* Nothing to send */
  1288. return 0;
  1289. else
  1290. req = list_entry(ep0->queue.next, struct lpc32xx_request,
  1291. queue);
  1292. tsend = ts = req->req.length - req->req.actual;
  1293. if (ts == 0) {
  1294. /* Send a ZLP */
  1295. udc_ep0_send_zlp(udc);
  1296. done(ep0, req, 0);
  1297. return 1;
  1298. } else if (ts > ep0->ep.maxpacket)
  1299. ts = ep0->ep.maxpacket; /* Just send what we can */
  1300. /* Write data to the EP0 FIFO and start transfer */
  1301. udc_write_hwep(udc, EP_IN, (req->req.buf + req->req.actual), ts);
  1302. /* Increment data pointer */
  1303. req->req.actual += ts;
  1304. if (tsend >= ep0->ep.maxpacket)
  1305. return 0; /* Stay in data transfer state */
  1306. /* Transfer request is complete */
  1307. udc->ep0state = WAIT_FOR_SETUP;
  1308. done(ep0, req, 0);
  1309. return 1;
  1310. }
  1311. /* OUT endpoint 0 transfer */
  1312. static int udc_ep0_out_req(struct lpc32xx_udc *udc)
  1313. {
  1314. struct lpc32xx_request *req;
  1315. struct lpc32xx_ep *ep0 = &udc->ep[0];
  1316. u32 tr, bufferspace;
  1317. if (list_empty(&ep0->queue))
  1318. return 0;
  1319. else
  1320. req = list_entry(ep0->queue.next, struct lpc32xx_request,
  1321. queue);
  1322. if (req) {
  1323. if (req->req.length == 0) {
  1324. /* Just dequeue request */
  1325. done(ep0, req, 0);
  1326. udc->ep0state = WAIT_FOR_SETUP;
  1327. return 1;
  1328. }
  1329. /* Get data from FIFO */
  1330. bufferspace = req->req.length - req->req.actual;
  1331. if (bufferspace > ep0->ep.maxpacket)
  1332. bufferspace = ep0->ep.maxpacket;
  1333. /* Copy data to buffer */
  1334. prefetchw(req->req.buf + req->req.actual);
  1335. tr = udc_read_hwep(udc, EP_OUT, req->req.buf + req->req.actual,
  1336. bufferspace);
  1337. req->req.actual += bufferspace;
  1338. if (tr < ep0->ep.maxpacket) {
  1339. /* This is the last packet */
  1340. done(ep0, req, 0);
  1341. udc->ep0state = WAIT_FOR_SETUP;
  1342. return 1;
  1343. }
  1344. }
  1345. return 0;
  1346. }
  1347. /* Must be called with lock */
  1348. static void stop_activity(struct lpc32xx_udc *udc)
  1349. {
  1350. struct usb_gadget_driver *driver = udc->driver;
  1351. int i;
  1352. if (udc->gadget.speed == USB_SPEED_UNKNOWN)
  1353. driver = NULL;
  1354. udc->gadget.speed = USB_SPEED_UNKNOWN;
  1355. udc->suspended = 0;
  1356. for (i = 0; i < NUM_ENDPOINTS; i++) {
  1357. struct lpc32xx_ep *ep = &udc->ep[i];
  1358. nuke(ep, -ESHUTDOWN);
  1359. }
  1360. if (driver) {
  1361. spin_unlock(&udc->lock);
  1362. driver->disconnect(&udc->gadget);
  1363. spin_lock(&udc->lock);
  1364. }
  1365. isp1301_pullup_enable(udc, 0, 0);
  1366. udc_disable(udc);
  1367. udc_reinit(udc);
  1368. }
  1369. /*
  1370. * Activate or kill host pullup
  1371. * Can be called with or without lock
  1372. */
  1373. static void pullup(struct lpc32xx_udc *udc, int is_on)
  1374. {
  1375. if (!udc->clocked)
  1376. return;
  1377. if (!udc->enabled || !udc->vbus)
  1378. is_on = 0;
  1379. if (is_on != udc->pullup)
  1380. isp1301_pullup_enable(udc, is_on, 0);
  1381. }
  1382. /* Must be called without lock */
  1383. static int lpc32xx_ep_disable(struct usb_ep *_ep)
  1384. {
  1385. struct lpc32xx_ep *ep = container_of(_ep, struct lpc32xx_ep, ep);
  1386. struct lpc32xx_udc *udc = ep->udc;
  1387. unsigned long flags;
  1388. if ((ep->hwep_num_base == 0) || (ep->hwep_num == 0))
  1389. return -EINVAL;
  1390. spin_lock_irqsave(&udc->lock, flags);
  1391. nuke(ep, -ESHUTDOWN);
  1392. /* Clear all DMA statuses for this EP */
  1393. udc_ep_dma_disable(udc, ep->hwep_num);
  1394. writel(1 << ep->hwep_num, USBD_EOTINTCLR(udc->udp_baseaddr));
  1395. writel(1 << ep->hwep_num, USBD_NDDRTINTCLR(udc->udp_baseaddr));
  1396. writel(1 << ep->hwep_num, USBD_SYSERRTINTCLR(udc->udp_baseaddr));
  1397. writel(1 << ep->hwep_num, USBD_DMARCLR(udc->udp_baseaddr));
  1398. /* Remove the DD pointer in the UDCA */
  1399. udc->udca_v_base[ep->hwep_num] = 0;
  1400. /* Disable and reset endpoint and interrupt */
  1401. uda_clear_hwepint(udc, ep->hwep_num);
  1402. udc_unrealize_hwep(udc, ep->hwep_num);
  1403. ep->hwep_num = 0;
  1404. spin_unlock_irqrestore(&udc->lock, flags);
  1405. atomic_dec(&udc->enabled_ep_cnt);
  1406. wake_up(&udc->ep_disable_wait_queue);
  1407. return 0;
  1408. }
  1409. /* Must be called without lock */
  1410. static int lpc32xx_ep_enable(struct usb_ep *_ep,
  1411. const struct usb_endpoint_descriptor *desc)
  1412. {
  1413. struct lpc32xx_ep *ep = container_of(_ep, struct lpc32xx_ep, ep);
  1414. struct lpc32xx_udc *udc = ep->udc;
  1415. u16 maxpacket;
  1416. u32 tmp;
  1417. unsigned long flags;
  1418. /* Verify EP data */
  1419. if ((!_ep) || (!ep) || (!desc) ||
  1420. (desc->bDescriptorType != USB_DT_ENDPOINT)) {
  1421. dev_dbg(udc->dev, "bad ep or descriptor\n");
  1422. return -EINVAL;
  1423. }
  1424. maxpacket = usb_endpoint_maxp(desc);
  1425. if ((maxpacket == 0) || (maxpacket > ep->maxpacket)) {
  1426. dev_dbg(udc->dev, "bad ep descriptor's packet size\n");
  1427. return -EINVAL;
  1428. }
  1429. /* Don't touch EP0 */
  1430. if (ep->hwep_num_base == 0) {
  1431. dev_dbg(udc->dev, "Can't re-enable EP0!!!\n");
  1432. return -EINVAL;
  1433. }
  1434. /* Is driver ready? */
  1435. if ((!udc->driver) || (udc->gadget.speed == USB_SPEED_UNKNOWN)) {
  1436. dev_dbg(udc->dev, "bogus device state\n");
  1437. return -ESHUTDOWN;
  1438. }
  1439. tmp = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  1440. switch (tmp) {
  1441. case USB_ENDPOINT_XFER_CONTROL:
  1442. return -EINVAL;
  1443. case USB_ENDPOINT_XFER_INT:
  1444. if (maxpacket > ep->maxpacket) {
  1445. dev_dbg(udc->dev,
  1446. "Bad INT endpoint maxpacket %d\n", maxpacket);
  1447. return -EINVAL;
  1448. }
  1449. break;
  1450. case USB_ENDPOINT_XFER_BULK:
  1451. switch (maxpacket) {
  1452. case 8:
  1453. case 16:
  1454. case 32:
  1455. case 64:
  1456. break;
  1457. default:
  1458. dev_dbg(udc->dev,
  1459. "Bad BULK endpoint maxpacket %d\n", maxpacket);
  1460. return -EINVAL;
  1461. }
  1462. break;
  1463. case USB_ENDPOINT_XFER_ISOC:
  1464. break;
  1465. }
  1466. spin_lock_irqsave(&udc->lock, flags);
  1467. /* Initialize endpoint to match the selected descriptor */
  1468. ep->is_in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
  1469. ep->ep.maxpacket = maxpacket;
  1470. /* Map hardware endpoint from base and direction */
  1471. if (ep->is_in)
  1472. /* IN endpoints are offset 1 from the OUT endpoint */
  1473. ep->hwep_num = ep->hwep_num_base + EP_IN;
  1474. else
  1475. ep->hwep_num = ep->hwep_num_base;
  1476. ep_dbg(ep, "EP enabled: %s, HW:%d, MP:%d IN:%d\n", ep->ep.name,
  1477. ep->hwep_num, maxpacket, (ep->is_in == 1));
  1478. /* Realize the endpoint, interrupt is enabled later when
  1479. * buffers are queued, IN EPs will NAK until buffers are ready */
  1480. udc_realize_hwep(udc, ep->hwep_num, ep->ep.maxpacket);
  1481. udc_clr_buffer_hwep(udc, ep->hwep_num);
  1482. uda_disable_hwepint(udc, ep->hwep_num);
  1483. udc_clrstall_hwep(udc, ep->hwep_num);
  1484. /* Clear all DMA statuses for this EP */
  1485. udc_ep_dma_disable(udc, ep->hwep_num);
  1486. writel(1 << ep->hwep_num, USBD_EOTINTCLR(udc->udp_baseaddr));
  1487. writel(1 << ep->hwep_num, USBD_NDDRTINTCLR(udc->udp_baseaddr));
  1488. writel(1 << ep->hwep_num, USBD_SYSERRTINTCLR(udc->udp_baseaddr));
  1489. writel(1 << ep->hwep_num, USBD_DMARCLR(udc->udp_baseaddr));
  1490. spin_unlock_irqrestore(&udc->lock, flags);
  1491. atomic_inc(&udc->enabled_ep_cnt);
  1492. return 0;
  1493. }
  1494. /*
  1495. * Allocate a USB request list
  1496. * Can be called with or without lock
  1497. */
  1498. static struct usb_request *lpc32xx_ep_alloc_request(struct usb_ep *_ep,
  1499. gfp_t gfp_flags)
  1500. {
  1501. struct lpc32xx_request *req;
  1502. req = kzalloc(sizeof(struct lpc32xx_request), gfp_flags);
  1503. if (!req)
  1504. return NULL;
  1505. INIT_LIST_HEAD(&req->queue);
  1506. return &req->req;
  1507. }
  1508. /*
  1509. * De-allocate a USB request list
  1510. * Can be called with or without lock
  1511. */
  1512. static void lpc32xx_ep_free_request(struct usb_ep *_ep,
  1513. struct usb_request *_req)
  1514. {
  1515. struct lpc32xx_request *req;
  1516. req = container_of(_req, struct lpc32xx_request, req);
  1517. BUG_ON(!list_empty(&req->queue));
  1518. kfree(req);
  1519. }
  1520. /* Must be called without lock */
  1521. static int lpc32xx_ep_queue(struct usb_ep *_ep,
  1522. struct usb_request *_req, gfp_t gfp_flags)
  1523. {
  1524. struct lpc32xx_request *req;
  1525. struct lpc32xx_ep *ep;
  1526. struct lpc32xx_udc *udc;
  1527. unsigned long flags;
  1528. int status = 0;
  1529. req = container_of(_req, struct lpc32xx_request, req);
  1530. ep = container_of(_ep, struct lpc32xx_ep, ep);
  1531. if (!_req || !_req->complete || !_req->buf ||
  1532. !list_empty(&req->queue))
  1533. return -EINVAL;
  1534. udc = ep->udc;
  1535. if (!_ep) {
  1536. dev_dbg(udc->dev, "invalid ep\n");
  1537. return -EINVAL;
  1538. }
  1539. if ((!udc) || (!udc->driver) ||
  1540. (udc->gadget.speed == USB_SPEED_UNKNOWN)) {
  1541. dev_dbg(udc->dev, "invalid device\n");
  1542. return -EINVAL;
  1543. }
  1544. if (ep->lep) {
  1545. enum dma_data_direction direction;
  1546. struct lpc32xx_usbd_dd_gad *dd;
  1547. /* Map DMA pointer */
  1548. if (ep->is_in)
  1549. direction = DMA_TO_DEVICE;
  1550. else
  1551. direction = DMA_FROM_DEVICE;
  1552. if (req->req.dma == 0) {
  1553. req->req.dma = dma_map_single(
  1554. ep->udc->gadget.dev.parent,
  1555. req->req.buf, req->req.length, direction);
  1556. req->mapped = 1;
  1557. } else {
  1558. dma_sync_single_for_device(
  1559. ep->udc->gadget.dev.parent, req->req.dma,
  1560. req->req.length, direction);
  1561. req->mapped = 0;
  1562. }
  1563. /* For the request, build a list of DDs */
  1564. dd = udc_dd_alloc(udc);
  1565. if (!dd) {
  1566. /* Error allocating DD */
  1567. return -ENOMEM;
  1568. }
  1569. req->dd_desc_ptr = dd;
  1570. /* Setup the DMA descriptor */
  1571. dd->dd_next_phy = dd->dd_next_v = 0;
  1572. dd->dd_buffer_addr = req->req.dma;
  1573. dd->dd_status = 0;
  1574. /* Special handling for ISO EPs */
  1575. if (ep->eptype == EP_ISO_TYPE) {
  1576. dd->dd_setup = DD_SETUP_ISO_EP |
  1577. DD_SETUP_PACKETLEN(0) |
  1578. DD_SETUP_DMALENBYTES(1);
  1579. dd->dd_iso_ps_mem_addr = dd->this_dma + 24;
  1580. if (ep->is_in)
  1581. dd->iso_status[0] = req->req.length;
  1582. else
  1583. dd->iso_status[0] = 0;
  1584. } else
  1585. dd->dd_setup = DD_SETUP_PACKETLEN(ep->ep.maxpacket) |
  1586. DD_SETUP_DMALENBYTES(req->req.length);
  1587. }
  1588. ep_dbg(ep, "%s queue req %p len %d buf %p (in=%d) z=%d\n", _ep->name,
  1589. _req, _req->length, _req->buf, ep->is_in, _req->zero);
  1590. spin_lock_irqsave(&udc->lock, flags);
  1591. _req->status = -EINPROGRESS;
  1592. _req->actual = 0;
  1593. req->send_zlp = _req->zero;
  1594. /* Kickstart empty queues */
  1595. if (list_empty(&ep->queue)) {
  1596. list_add_tail(&req->queue, &ep->queue);
  1597. if (ep->hwep_num_base == 0) {
  1598. /* Handle expected data direction */
  1599. if (ep->is_in) {
  1600. /* IN packet to host */
  1601. udc->ep0state = DATA_IN;
  1602. status = udc_ep0_in_req(udc);
  1603. } else {
  1604. /* OUT packet from host */
  1605. udc->ep0state = DATA_OUT;
  1606. status = udc_ep0_out_req(udc);
  1607. }
  1608. } else if (ep->is_in) {
  1609. /* IN packet to host and kick off transfer */
  1610. if (!ep->req_pending)
  1611. udc_ep_in_req_dma(udc, ep);
  1612. } else
  1613. /* OUT packet from host and kick off list */
  1614. if (!ep->req_pending)
  1615. udc_ep_out_req_dma(udc, ep);
  1616. } else
  1617. list_add_tail(&req->queue, &ep->queue);
  1618. spin_unlock_irqrestore(&udc->lock, flags);
  1619. return (status < 0) ? status : 0;
  1620. }
  1621. /* Must be called without lock */
  1622. static int lpc32xx_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  1623. {
  1624. struct lpc32xx_ep *ep;
  1625. struct lpc32xx_request *req;
  1626. unsigned long flags;
  1627. ep = container_of(_ep, struct lpc32xx_ep, ep);
  1628. if (!_ep || ep->hwep_num_base == 0)
  1629. return -EINVAL;
  1630. spin_lock_irqsave(&ep->udc->lock, flags);
  1631. /* make sure it's actually queued on this endpoint */
  1632. list_for_each_entry(req, &ep->queue, queue) {
  1633. if (&req->req == _req)
  1634. break;
  1635. }
  1636. if (&req->req != _req) {
  1637. spin_unlock_irqrestore(&ep->udc->lock, flags);
  1638. return -EINVAL;
  1639. }
  1640. done(ep, req, -ECONNRESET);
  1641. spin_unlock_irqrestore(&ep->udc->lock, flags);
  1642. return 0;
  1643. }
  1644. /* Must be called without lock */
  1645. static int lpc32xx_ep_set_halt(struct usb_ep *_ep, int value)
  1646. {
  1647. struct lpc32xx_ep *ep = container_of(_ep, struct lpc32xx_ep, ep);
  1648. struct lpc32xx_udc *udc = ep->udc;
  1649. unsigned long flags;
  1650. if ((!ep) || (ep->hwep_num <= 1))
  1651. return -EINVAL;
  1652. /* Don't halt an IN EP */
  1653. if (ep->is_in)
  1654. return -EAGAIN;
  1655. spin_lock_irqsave(&udc->lock, flags);
  1656. if (value == 1) {
  1657. /* stall */
  1658. udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(ep->hwep_num),
  1659. DAT_WR_BYTE(EP_STAT_ST));
  1660. } else {
  1661. /* End stall */
  1662. ep->wedge = 0;
  1663. udc_protocol_cmd_data_w(udc, CMD_SET_EP_STAT(ep->hwep_num),
  1664. DAT_WR_BYTE(0));
  1665. }
  1666. spin_unlock_irqrestore(&udc->lock, flags);
  1667. return 0;
  1668. }
  1669. /* set the halt feature and ignores clear requests */
  1670. static int lpc32xx_ep_set_wedge(struct usb_ep *_ep)
  1671. {
  1672. struct lpc32xx_ep *ep = container_of(_ep, struct lpc32xx_ep, ep);
  1673. if (!_ep || !ep->udc)
  1674. return -EINVAL;
  1675. ep->wedge = 1;
  1676. return usb_ep_set_halt(_ep);
  1677. }
  1678. static const struct usb_ep_ops lpc32xx_ep_ops = {
  1679. .enable = lpc32xx_ep_enable,
  1680. .disable = lpc32xx_ep_disable,
  1681. .alloc_request = lpc32xx_ep_alloc_request,
  1682. .free_request = lpc32xx_ep_free_request,
  1683. .queue = lpc32xx_ep_queue,
  1684. .dequeue = lpc32xx_ep_dequeue,
  1685. .set_halt = lpc32xx_ep_set_halt,
  1686. .set_wedge = lpc32xx_ep_set_wedge,
  1687. };
  1688. /* Send a ZLP on a non-0 IN EP */
  1689. void udc_send_in_zlp(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep)
  1690. {
  1691. /* Clear EP status */
  1692. udc_clearep_getsts(udc, ep->hwep_num);
  1693. /* Send ZLP via FIFO mechanism */
  1694. udc_write_hwep(udc, ep->hwep_num, NULL, 0);
  1695. }
  1696. /*
  1697. * Handle EP completion for ZLP
  1698. * This function will only be called when a delayed ZLP needs to be sent out
  1699. * after a DMA transfer has filled both buffers.
  1700. */
  1701. void udc_handle_eps(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep)
  1702. {
  1703. u32 epstatus;
  1704. struct lpc32xx_request *req;
  1705. if (ep->hwep_num <= 0)
  1706. return;
  1707. uda_clear_hwepint(udc, ep->hwep_num);
  1708. /* If this interrupt isn't enabled, return now */
  1709. if (!(udc->enabled_hwepints & (1 << ep->hwep_num)))
  1710. return;
  1711. /* Get endpoint status */
  1712. epstatus = udc_clearep_getsts(udc, ep->hwep_num);
  1713. /*
  1714. * This should never happen, but protect against writing to the
  1715. * buffer when full.
  1716. */
  1717. if (epstatus & EP_SEL_F)
  1718. return;
  1719. if (ep->is_in) {
  1720. udc_send_in_zlp(udc, ep);
  1721. uda_disable_hwepint(udc, ep->hwep_num);
  1722. } else
  1723. return;
  1724. /* If there isn't a request waiting, something went wrong */
  1725. req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
  1726. if (req) {
  1727. done(ep, req, 0);
  1728. /* Start another request if ready */
  1729. if (!list_empty(&ep->queue)) {
  1730. if (ep->is_in)
  1731. udc_ep_in_req_dma(udc, ep);
  1732. else
  1733. udc_ep_out_req_dma(udc, ep);
  1734. } else
  1735. ep->req_pending = 0;
  1736. }
  1737. }
  1738. /* DMA end of transfer completion */
  1739. static void udc_handle_dma_ep(struct lpc32xx_udc *udc, struct lpc32xx_ep *ep)
  1740. {
  1741. u32 status, epstatus;
  1742. struct lpc32xx_request *req;
  1743. struct lpc32xx_usbd_dd_gad *dd;
  1744. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  1745. ep->totalints++;
  1746. #endif
  1747. req = list_entry(ep->queue.next, struct lpc32xx_request, queue);
  1748. if (!req) {
  1749. ep_err(ep, "DMA interrupt on no req!\n");
  1750. return;
  1751. }
  1752. dd = req->dd_desc_ptr;
  1753. /* DMA descriptor should always be retired for this call */
  1754. if (!(dd->dd_status & DD_STATUS_DD_RETIRED))
  1755. ep_warn(ep, "DMA descriptor did not retire\n");
  1756. /* Disable DMA */
  1757. udc_ep_dma_disable(udc, ep->hwep_num);
  1758. writel((1 << ep->hwep_num), USBD_EOTINTCLR(udc->udp_baseaddr));
  1759. writel((1 << ep->hwep_num), USBD_NDDRTINTCLR(udc->udp_baseaddr));
  1760. /* System error? */
  1761. if (readl(USBD_SYSERRTINTST(udc->udp_baseaddr)) &
  1762. (1 << ep->hwep_num)) {
  1763. writel((1 << ep->hwep_num),
  1764. USBD_SYSERRTINTCLR(udc->udp_baseaddr));
  1765. ep_err(ep, "AHB critical error!\n");
  1766. ep->req_pending = 0;
  1767. /* The error could have occurred on a packet of a multipacket
  1768. * transfer, so recovering the transfer is not possible. Close
  1769. * the request with an error */
  1770. done(ep, req, -ECONNABORTED);
  1771. return;
  1772. }
  1773. /* Handle the current DD's status */
  1774. status = dd->dd_status;
  1775. switch (status & DD_STATUS_STS_MASK) {
  1776. case DD_STATUS_STS_NS:
  1777. /* DD not serviced? This shouldn't happen! */
  1778. ep->req_pending = 0;
  1779. ep_err(ep, "DMA critical EP error: DD not serviced (0x%x)!\n",
  1780. status);
  1781. done(ep, req, -ECONNABORTED);
  1782. return;
  1783. case DD_STATUS_STS_BS:
  1784. /* Interrupt only fires on EOT - This shouldn't happen! */
  1785. ep->req_pending = 0;
  1786. ep_err(ep, "DMA critical EP error: EOT prior to service completion (0x%x)!\n",
  1787. status);
  1788. done(ep, req, -ECONNABORTED);
  1789. return;
  1790. case DD_STATUS_STS_NC:
  1791. case DD_STATUS_STS_DUR:
  1792. /* Really just a short packet, not an underrun */
  1793. /* This is a good status and what we expect */
  1794. break;
  1795. default:
  1796. /* Data overrun, system error, or unknown */
  1797. ep->req_pending = 0;
  1798. ep_err(ep, "DMA critical EP error: System error (0x%x)!\n",
  1799. status);
  1800. done(ep, req, -ECONNABORTED);
  1801. return;
  1802. }
  1803. /* ISO endpoints are handled differently */
  1804. if (ep->eptype == EP_ISO_TYPE) {
  1805. if (ep->is_in)
  1806. req->req.actual = req->req.length;
  1807. else
  1808. req->req.actual = dd->iso_status[0] & 0xFFFF;
  1809. } else
  1810. req->req.actual += DD_STATUS_CURDMACNT(status);
  1811. /* Send a ZLP if necessary. This will be done for non-int
  1812. * packets which have a size that is a divisor of MAXP */
  1813. if (req->send_zlp) {
  1814. /*
  1815. * If at least 1 buffer is available, send the ZLP now.
  1816. * Otherwise, the ZLP send needs to be deferred until a
  1817. * buffer is available.
  1818. */
  1819. if (udc_clearep_getsts(udc, ep->hwep_num) & EP_SEL_F) {
  1820. udc_clearep_getsts(udc, ep->hwep_num);
  1821. uda_enable_hwepint(udc, ep->hwep_num);
  1822. epstatus = udc_clearep_getsts(udc, ep->hwep_num);
  1823. /* Let the EP interrupt handle the ZLP */
  1824. return;
  1825. } else
  1826. udc_send_in_zlp(udc, ep);
  1827. }
  1828. /* Transfer request is complete */
  1829. done(ep, req, 0);
  1830. /* Start another request if ready */
  1831. udc_clearep_getsts(udc, ep->hwep_num);
  1832. if (!list_empty((&ep->queue))) {
  1833. if (ep->is_in)
  1834. udc_ep_in_req_dma(udc, ep);
  1835. else
  1836. udc_ep_out_req_dma(udc, ep);
  1837. } else
  1838. ep->req_pending = 0;
  1839. }
  1840. /*
  1841. *
  1842. * Endpoint 0 functions
  1843. *
  1844. */
  1845. static void udc_handle_dev(struct lpc32xx_udc *udc)
  1846. {
  1847. u32 tmp;
  1848. udc_protocol_cmd_w(udc, CMD_GET_DEV_STAT);
  1849. tmp = udc_protocol_cmd_r(udc, DAT_GET_DEV_STAT);
  1850. if (tmp & DEV_RST)
  1851. uda_usb_reset(udc);
  1852. else if (tmp & DEV_CON_CH)
  1853. uda_power_event(udc, (tmp & DEV_CON));
  1854. else if (tmp & DEV_SUS_CH) {
  1855. if (tmp & DEV_SUS) {
  1856. if (udc->vbus == 0)
  1857. stop_activity(udc);
  1858. else if ((udc->gadget.speed != USB_SPEED_UNKNOWN) &&
  1859. udc->driver) {
  1860. /* Power down transceiver */
  1861. udc->poweron = 0;
  1862. schedule_work(&udc->pullup_job);
  1863. uda_resm_susp_event(udc, 1);
  1864. }
  1865. } else if ((udc->gadget.speed != USB_SPEED_UNKNOWN) &&
  1866. udc->driver && udc->vbus) {
  1867. uda_resm_susp_event(udc, 0);
  1868. /* Power up transceiver */
  1869. udc->poweron = 1;
  1870. schedule_work(&udc->pullup_job);
  1871. }
  1872. }
  1873. }
  1874. static int udc_get_status(struct lpc32xx_udc *udc, u16 reqtype, u16 wIndex)
  1875. {
  1876. struct lpc32xx_ep *ep;
  1877. u32 ep0buff = 0, tmp;
  1878. switch (reqtype & USB_RECIP_MASK) {
  1879. case USB_RECIP_INTERFACE:
  1880. break; /* Not supported */
  1881. case USB_RECIP_DEVICE:
  1882. ep0buff = (udc->selfpowered << USB_DEVICE_SELF_POWERED);
  1883. if (udc->dev_status & (1 << USB_DEVICE_REMOTE_WAKEUP))
  1884. ep0buff |= (1 << USB_DEVICE_REMOTE_WAKEUP);
  1885. break;
  1886. case USB_RECIP_ENDPOINT:
  1887. tmp = wIndex & USB_ENDPOINT_NUMBER_MASK;
  1888. ep = &udc->ep[tmp];
  1889. if ((tmp == 0) || (tmp >= NUM_ENDPOINTS))
  1890. return -EOPNOTSUPP;
  1891. if (wIndex & USB_DIR_IN) {
  1892. if (!ep->is_in)
  1893. return -EOPNOTSUPP; /* Something's wrong */
  1894. } else if (ep->is_in)
  1895. return -EOPNOTSUPP; /* Not an IN endpoint */
  1896. /* Get status of the endpoint */
  1897. udc_protocol_cmd_w(udc, CMD_SEL_EP(ep->hwep_num));
  1898. tmp = udc_protocol_cmd_r(udc, DAT_SEL_EP(ep->hwep_num));
  1899. if (tmp & EP_SEL_ST)
  1900. ep0buff = (1 << USB_ENDPOINT_HALT);
  1901. else
  1902. ep0buff = 0;
  1903. break;
  1904. default:
  1905. break;
  1906. }
  1907. /* Return data */
  1908. udc_write_hwep(udc, EP_IN, &ep0buff, 2);
  1909. return 0;
  1910. }
  1911. static void udc_handle_ep0_setup(struct lpc32xx_udc *udc)
  1912. {
  1913. struct lpc32xx_ep *ep, *ep0 = &udc->ep[0];
  1914. struct usb_ctrlrequest ctrlpkt;
  1915. int i, bytes;
  1916. u16 wIndex, wValue, wLength, reqtype, req, tmp;
  1917. /* Nuke previous transfers */
  1918. nuke(ep0, -EPROTO);
  1919. /* Get setup packet */
  1920. bytes = udc_read_hwep(udc, EP_OUT, (u32 *) &ctrlpkt, 8);
  1921. if (bytes != 8) {
  1922. ep_warn(ep0, "Incorrectly sized setup packet (s/b 8, is %d)!\n",
  1923. bytes);
  1924. return;
  1925. }
  1926. /* Native endianness */
  1927. wIndex = le16_to_cpu(ctrlpkt.wIndex);
  1928. wValue = le16_to_cpu(ctrlpkt.wValue);
  1929. wLength = le16_to_cpu(ctrlpkt.wLength);
  1930. reqtype = le16_to_cpu(ctrlpkt.bRequestType);
  1931. /* Set direction of EP0 */
  1932. if (likely(reqtype & USB_DIR_IN))
  1933. ep0->is_in = 1;
  1934. else
  1935. ep0->is_in = 0;
  1936. /* Handle SETUP packet */
  1937. req = le16_to_cpu(ctrlpkt.bRequest);
  1938. switch (req) {
  1939. case USB_REQ_CLEAR_FEATURE:
  1940. case USB_REQ_SET_FEATURE:
  1941. switch (reqtype) {
  1942. case (USB_TYPE_STANDARD | USB_RECIP_DEVICE):
  1943. if (wValue != USB_DEVICE_REMOTE_WAKEUP)
  1944. goto stall; /* Nothing else handled */
  1945. /* Tell board about event */
  1946. if (req == USB_REQ_CLEAR_FEATURE)
  1947. udc->dev_status &=
  1948. ~(1 << USB_DEVICE_REMOTE_WAKEUP);
  1949. else
  1950. udc->dev_status |=
  1951. (1 << USB_DEVICE_REMOTE_WAKEUP);
  1952. uda_remwkp_cgh(udc);
  1953. goto zlp_send;
  1954. case (USB_TYPE_STANDARD | USB_RECIP_ENDPOINT):
  1955. tmp = wIndex & USB_ENDPOINT_NUMBER_MASK;
  1956. if ((wValue != USB_ENDPOINT_HALT) ||
  1957. (tmp >= NUM_ENDPOINTS))
  1958. break;
  1959. /* Find hardware endpoint from logical endpoint */
  1960. ep = &udc->ep[tmp];
  1961. tmp = ep->hwep_num;
  1962. if (tmp == 0)
  1963. break;
  1964. if (req == USB_REQ_SET_FEATURE)
  1965. udc_stall_hwep(udc, tmp);
  1966. else if (!ep->wedge)
  1967. udc_clrstall_hwep(udc, tmp);
  1968. goto zlp_send;
  1969. default:
  1970. break;
  1971. }
  1972. case USB_REQ_SET_ADDRESS:
  1973. if (reqtype == (USB_TYPE_STANDARD | USB_RECIP_DEVICE)) {
  1974. udc_set_address(udc, wValue);
  1975. goto zlp_send;
  1976. }
  1977. break;
  1978. case USB_REQ_GET_STATUS:
  1979. udc_get_status(udc, reqtype, wIndex);
  1980. return;
  1981. default:
  1982. break; /* Let GadgetFS handle the descriptor instead */
  1983. }
  1984. if (likely(udc->driver)) {
  1985. /* device-2-host (IN) or no data setup command, process
  1986. * immediately */
  1987. spin_unlock(&udc->lock);
  1988. i = udc->driver->setup(&udc->gadget, &ctrlpkt);
  1989. spin_lock(&udc->lock);
  1990. if (req == USB_REQ_SET_CONFIGURATION) {
  1991. /* Configuration is set after endpoints are realized */
  1992. if (wValue) {
  1993. /* Set configuration */
  1994. udc_set_device_configured(udc);
  1995. udc_protocol_cmd_data_w(udc, CMD_SET_MODE,
  1996. DAT_WR_BYTE(AP_CLK |
  1997. INAK_BI | INAK_II));
  1998. } else {
  1999. /* Clear configuration */
  2000. udc_set_device_unconfigured(udc);
  2001. /* Disable NAK interrupts */
  2002. udc_protocol_cmd_data_w(udc, CMD_SET_MODE,
  2003. DAT_WR_BYTE(AP_CLK));
  2004. }
  2005. }
  2006. if (i < 0) {
  2007. /* setup processing failed, force stall */
  2008. dev_dbg(udc->dev,
  2009. "req %02x.%02x protocol STALL; stat %d\n",
  2010. reqtype, req, i);
  2011. udc->ep0state = WAIT_FOR_SETUP;
  2012. goto stall;
  2013. }
  2014. }
  2015. if (!ep0->is_in)
  2016. udc_ep0_send_zlp(udc); /* ZLP IN packet on data phase */
  2017. return;
  2018. stall:
  2019. udc_stall_hwep(udc, EP_IN);
  2020. return;
  2021. zlp_send:
  2022. udc_ep0_send_zlp(udc);
  2023. return;
  2024. }
  2025. /* IN endpoint 0 transfer */
  2026. static void udc_handle_ep0_in(struct lpc32xx_udc *udc)
  2027. {
  2028. struct lpc32xx_ep *ep0 = &udc->ep[0];
  2029. u32 epstatus;
  2030. /* Clear EP interrupt */
  2031. epstatus = udc_clearep_getsts(udc, EP_IN);
  2032. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  2033. ep0->totalints++;
  2034. #endif
  2035. /* Stalled? Clear stall and reset buffers */
  2036. if (epstatus & EP_SEL_ST) {
  2037. udc_clrstall_hwep(udc, EP_IN);
  2038. nuke(ep0, -ECONNABORTED);
  2039. udc->ep0state = WAIT_FOR_SETUP;
  2040. return;
  2041. }
  2042. /* Is a buffer available? */
  2043. if (!(epstatus & EP_SEL_F)) {
  2044. /* Handle based on current state */
  2045. if (udc->ep0state == DATA_IN)
  2046. udc_ep0_in_req(udc);
  2047. else {
  2048. /* Unknown state for EP0 oe end of DATA IN phase */
  2049. nuke(ep0, -ECONNABORTED);
  2050. udc->ep0state = WAIT_FOR_SETUP;
  2051. }
  2052. }
  2053. }
  2054. /* OUT endpoint 0 transfer */
  2055. static void udc_handle_ep0_out(struct lpc32xx_udc *udc)
  2056. {
  2057. struct lpc32xx_ep *ep0 = &udc->ep[0];
  2058. u32 epstatus;
  2059. /* Clear EP interrupt */
  2060. epstatus = udc_clearep_getsts(udc, EP_OUT);
  2061. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  2062. ep0->totalints++;
  2063. #endif
  2064. /* Stalled? */
  2065. if (epstatus & EP_SEL_ST) {
  2066. udc_clrstall_hwep(udc, EP_OUT);
  2067. nuke(ep0, -ECONNABORTED);
  2068. udc->ep0state = WAIT_FOR_SETUP;
  2069. return;
  2070. }
  2071. /* A NAK may occur if a packet couldn't be received yet */
  2072. if (epstatus & EP_SEL_EPN)
  2073. return;
  2074. /* Setup packet incoming? */
  2075. if (epstatus & EP_SEL_STP) {
  2076. nuke(ep0, 0);
  2077. udc->ep0state = WAIT_FOR_SETUP;
  2078. }
  2079. /* Data available? */
  2080. if (epstatus & EP_SEL_F)
  2081. /* Handle based on current state */
  2082. switch (udc->ep0state) {
  2083. case WAIT_FOR_SETUP:
  2084. udc_handle_ep0_setup(udc);
  2085. break;
  2086. case DATA_OUT:
  2087. udc_ep0_out_req(udc);
  2088. break;
  2089. default:
  2090. /* Unknown state for EP0 */
  2091. nuke(ep0, -ECONNABORTED);
  2092. udc->ep0state = WAIT_FOR_SETUP;
  2093. }
  2094. }
  2095. /* Must be called without lock */
  2096. static int lpc32xx_get_frame(struct usb_gadget *gadget)
  2097. {
  2098. int frame;
  2099. unsigned long flags;
  2100. struct lpc32xx_udc *udc = to_udc(gadget);
  2101. if (!udc->clocked)
  2102. return -EINVAL;
  2103. spin_lock_irqsave(&udc->lock, flags);
  2104. frame = (int) udc_get_current_frame(udc);
  2105. spin_unlock_irqrestore(&udc->lock, flags);
  2106. return frame;
  2107. }
  2108. static int lpc32xx_wakeup(struct usb_gadget *gadget)
  2109. {
  2110. return -ENOTSUPP;
  2111. }
  2112. static int lpc32xx_set_selfpowered(struct usb_gadget *gadget, int is_on)
  2113. {
  2114. struct lpc32xx_udc *udc = to_udc(gadget);
  2115. /* Always self-powered */
  2116. udc->selfpowered = (is_on != 0);
  2117. return 0;
  2118. }
  2119. /*
  2120. * vbus is here! turn everything on that's ready
  2121. * Must be called without lock
  2122. */
  2123. static int lpc32xx_vbus_session(struct usb_gadget *gadget, int is_active)
  2124. {
  2125. unsigned long flags;
  2126. struct lpc32xx_udc *udc = to_udc(gadget);
  2127. spin_lock_irqsave(&udc->lock, flags);
  2128. /* Doesn't need lock */
  2129. if (udc->driver) {
  2130. udc_clk_set(udc, 1);
  2131. udc_enable(udc);
  2132. pullup(udc, is_active);
  2133. } else {
  2134. stop_activity(udc);
  2135. pullup(udc, 0);
  2136. spin_unlock_irqrestore(&udc->lock, flags);
  2137. /*
  2138. * Wait for all the endpoints to disable,
  2139. * before disabling clocks. Don't wait if
  2140. * endpoints are not enabled.
  2141. */
  2142. if (atomic_read(&udc->enabled_ep_cnt))
  2143. wait_event_interruptible(udc->ep_disable_wait_queue,
  2144. (atomic_read(&udc->enabled_ep_cnt) == 0));
  2145. spin_lock_irqsave(&udc->lock, flags);
  2146. udc_clk_set(udc, 0);
  2147. }
  2148. spin_unlock_irqrestore(&udc->lock, flags);
  2149. return 0;
  2150. }
  2151. /* Can be called with or without lock */
  2152. static int lpc32xx_pullup(struct usb_gadget *gadget, int is_on)
  2153. {
  2154. struct lpc32xx_udc *udc = to_udc(gadget);
  2155. /* Doesn't need lock */
  2156. pullup(udc, is_on);
  2157. return 0;
  2158. }
  2159. static int lpc32xx_start(struct usb_gadget *, struct usb_gadget_driver *);
  2160. static int lpc32xx_stop(struct usb_gadget *, struct usb_gadget_driver *);
  2161. static const struct usb_gadget_ops lpc32xx_udc_ops = {
  2162. .get_frame = lpc32xx_get_frame,
  2163. .wakeup = lpc32xx_wakeup,
  2164. .set_selfpowered = lpc32xx_set_selfpowered,
  2165. .vbus_session = lpc32xx_vbus_session,
  2166. .pullup = lpc32xx_pullup,
  2167. .udc_start = lpc32xx_start,
  2168. .udc_stop = lpc32xx_stop,
  2169. };
  2170. static void nop_release(struct device *dev)
  2171. {
  2172. /* nothing to free */
  2173. }
  2174. static const struct lpc32xx_udc controller_template = {
  2175. .gadget = {
  2176. .ops = &lpc32xx_udc_ops,
  2177. .name = driver_name,
  2178. .dev = {
  2179. .init_name = "gadget",
  2180. .release = nop_release,
  2181. }
  2182. },
  2183. .ep[0] = {
  2184. .ep = {
  2185. .name = "ep0",
  2186. .ops = &lpc32xx_ep_ops,
  2187. },
  2188. .maxpacket = 64,
  2189. .hwep_num_base = 0,
  2190. .hwep_num = 0, /* Can be 0 or 1, has special handling */
  2191. .lep = 0,
  2192. .eptype = EP_CTL_TYPE,
  2193. },
  2194. .ep[1] = {
  2195. .ep = {
  2196. .name = "ep1-int",
  2197. .ops = &lpc32xx_ep_ops,
  2198. },
  2199. .maxpacket = 64,
  2200. .hwep_num_base = 2,
  2201. .hwep_num = 0, /* 2 or 3, will be set later */
  2202. .lep = 1,
  2203. .eptype = EP_INT_TYPE,
  2204. },
  2205. .ep[2] = {
  2206. .ep = {
  2207. .name = "ep2-bulk",
  2208. .ops = &lpc32xx_ep_ops,
  2209. },
  2210. .maxpacket = 64,
  2211. .hwep_num_base = 4,
  2212. .hwep_num = 0, /* 4 or 5, will be set later */
  2213. .lep = 2,
  2214. .eptype = EP_BLK_TYPE,
  2215. },
  2216. .ep[3] = {
  2217. .ep = {
  2218. .name = "ep3-iso",
  2219. .ops = &lpc32xx_ep_ops,
  2220. },
  2221. .maxpacket = 1023,
  2222. .hwep_num_base = 6,
  2223. .hwep_num = 0, /* 6 or 7, will be set later */
  2224. .lep = 3,
  2225. .eptype = EP_ISO_TYPE,
  2226. },
  2227. .ep[4] = {
  2228. .ep = {
  2229. .name = "ep4-int",
  2230. .ops = &lpc32xx_ep_ops,
  2231. },
  2232. .maxpacket = 64,
  2233. .hwep_num_base = 8,
  2234. .hwep_num = 0, /* 8 or 9, will be set later */
  2235. .lep = 4,
  2236. .eptype = EP_INT_TYPE,
  2237. },
  2238. .ep[5] = {
  2239. .ep = {
  2240. .name = "ep5-bulk",
  2241. .ops = &lpc32xx_ep_ops,
  2242. },
  2243. .maxpacket = 64,
  2244. .hwep_num_base = 10,
  2245. .hwep_num = 0, /* 10 or 11, will be set later */
  2246. .lep = 5,
  2247. .eptype = EP_BLK_TYPE,
  2248. },
  2249. .ep[6] = {
  2250. .ep = {
  2251. .name = "ep6-iso",
  2252. .ops = &lpc32xx_ep_ops,
  2253. },
  2254. .maxpacket = 1023,
  2255. .hwep_num_base = 12,
  2256. .hwep_num = 0, /* 12 or 13, will be set later */
  2257. .lep = 6,
  2258. .eptype = EP_ISO_TYPE,
  2259. },
  2260. .ep[7] = {
  2261. .ep = {
  2262. .name = "ep7-int",
  2263. .ops = &lpc32xx_ep_ops,
  2264. },
  2265. .maxpacket = 64,
  2266. .hwep_num_base = 14,
  2267. .hwep_num = 0,
  2268. .lep = 7,
  2269. .eptype = EP_INT_TYPE,
  2270. },
  2271. .ep[8] = {
  2272. .ep = {
  2273. .name = "ep8-bulk",
  2274. .ops = &lpc32xx_ep_ops,
  2275. },
  2276. .maxpacket = 64,
  2277. .hwep_num_base = 16,
  2278. .hwep_num = 0,
  2279. .lep = 8,
  2280. .eptype = EP_BLK_TYPE,
  2281. },
  2282. .ep[9] = {
  2283. .ep = {
  2284. .name = "ep9-iso",
  2285. .ops = &lpc32xx_ep_ops,
  2286. },
  2287. .maxpacket = 1023,
  2288. .hwep_num_base = 18,
  2289. .hwep_num = 0,
  2290. .lep = 9,
  2291. .eptype = EP_ISO_TYPE,
  2292. },
  2293. .ep[10] = {
  2294. .ep = {
  2295. .name = "ep10-int",
  2296. .ops = &lpc32xx_ep_ops,
  2297. },
  2298. .maxpacket = 64,
  2299. .hwep_num_base = 20,
  2300. .hwep_num = 0,
  2301. .lep = 10,
  2302. .eptype = EP_INT_TYPE,
  2303. },
  2304. .ep[11] = {
  2305. .ep = {
  2306. .name = "ep11-bulk",
  2307. .ops = &lpc32xx_ep_ops,
  2308. },
  2309. .maxpacket = 64,
  2310. .hwep_num_base = 22,
  2311. .hwep_num = 0,
  2312. .lep = 11,
  2313. .eptype = EP_BLK_TYPE,
  2314. },
  2315. .ep[12] = {
  2316. .ep = {
  2317. .name = "ep12-iso",
  2318. .ops = &lpc32xx_ep_ops,
  2319. },
  2320. .maxpacket = 1023,
  2321. .hwep_num_base = 24,
  2322. .hwep_num = 0,
  2323. .lep = 12,
  2324. .eptype = EP_ISO_TYPE,
  2325. },
  2326. .ep[13] = {
  2327. .ep = {
  2328. .name = "ep13-int",
  2329. .ops = &lpc32xx_ep_ops,
  2330. },
  2331. .maxpacket = 64,
  2332. .hwep_num_base = 26,
  2333. .hwep_num = 0,
  2334. .lep = 13,
  2335. .eptype = EP_INT_TYPE,
  2336. },
  2337. .ep[14] = {
  2338. .ep = {
  2339. .name = "ep14-bulk",
  2340. .ops = &lpc32xx_ep_ops,
  2341. },
  2342. .maxpacket = 64,
  2343. .hwep_num_base = 28,
  2344. .hwep_num = 0,
  2345. .lep = 14,
  2346. .eptype = EP_BLK_TYPE,
  2347. },
  2348. .ep[15] = {
  2349. .ep = {
  2350. .name = "ep15-bulk",
  2351. .ops = &lpc32xx_ep_ops,
  2352. },
  2353. .maxpacket = 1023,
  2354. .hwep_num_base = 30,
  2355. .hwep_num = 0,
  2356. .lep = 15,
  2357. .eptype = EP_BLK_TYPE,
  2358. },
  2359. };
  2360. /* ISO and status interrupts */
  2361. static irqreturn_t lpc32xx_usb_lp_irq(int irq, void *_udc)
  2362. {
  2363. u32 tmp, devstat;
  2364. struct lpc32xx_udc *udc = _udc;
  2365. spin_lock(&udc->lock);
  2366. /* Read the device status register */
  2367. devstat = readl(USBD_DEVINTST(udc->udp_baseaddr));
  2368. devstat &= ~USBD_EP_FAST;
  2369. writel(devstat, USBD_DEVINTCLR(udc->udp_baseaddr));
  2370. devstat = devstat & udc->enabled_devints;
  2371. /* Device specific handling needed? */
  2372. if (devstat & USBD_DEV_STAT)
  2373. udc_handle_dev(udc);
  2374. /* Start of frame? (devstat & FRAME_INT):
  2375. * The frame interrupt isn't really needed for ISO support,
  2376. * as the driver will queue the necessary packets */
  2377. /* Error? */
  2378. if (devstat & ERR_INT) {
  2379. /* All types of errors, from cable removal during transfer to
  2380. * misc protocol and bit errors. These are mostly for just info,
  2381. * as the USB hardware will work around these. If these errors
  2382. * happen alot, something is wrong. */
  2383. udc_protocol_cmd_w(udc, CMD_RD_ERR_STAT);
  2384. tmp = udc_protocol_cmd_r(udc, DAT_RD_ERR_STAT);
  2385. dev_dbg(udc->dev, "Device error (0x%x)!\n", tmp);
  2386. }
  2387. spin_unlock(&udc->lock);
  2388. return IRQ_HANDLED;
  2389. }
  2390. /* EP interrupts */
  2391. static irqreturn_t lpc32xx_usb_hp_irq(int irq, void *_udc)
  2392. {
  2393. u32 tmp;
  2394. struct lpc32xx_udc *udc = _udc;
  2395. spin_lock(&udc->lock);
  2396. /* Read the device status register */
  2397. writel(USBD_EP_FAST, USBD_DEVINTCLR(udc->udp_baseaddr));
  2398. /* Endpoints */
  2399. tmp = readl(USBD_EPINTST(udc->udp_baseaddr));
  2400. /* Special handling for EP0 */
  2401. if (tmp & (EP_MASK_SEL(0, EP_OUT) | EP_MASK_SEL(0, EP_IN))) {
  2402. /* Handle EP0 IN */
  2403. if (tmp & (EP_MASK_SEL(0, EP_IN)))
  2404. udc_handle_ep0_in(udc);
  2405. /* Handle EP0 OUT */
  2406. if (tmp & (EP_MASK_SEL(0, EP_OUT)))
  2407. udc_handle_ep0_out(udc);
  2408. }
  2409. /* All other EPs */
  2410. if (tmp & ~(EP_MASK_SEL(0, EP_OUT) | EP_MASK_SEL(0, EP_IN))) {
  2411. int i;
  2412. /* Handle other EP interrupts */
  2413. for (i = 1; i < NUM_ENDPOINTS; i++) {
  2414. if (tmp & (1 << udc->ep[i].hwep_num))
  2415. udc_handle_eps(udc, &udc->ep[i]);
  2416. }
  2417. }
  2418. spin_unlock(&udc->lock);
  2419. return IRQ_HANDLED;
  2420. }
  2421. static irqreturn_t lpc32xx_usb_devdma_irq(int irq, void *_udc)
  2422. {
  2423. struct lpc32xx_udc *udc = _udc;
  2424. int i;
  2425. u32 tmp;
  2426. spin_lock(&udc->lock);
  2427. /* Handle EP DMA EOT interrupts */
  2428. tmp = readl(USBD_EOTINTST(udc->udp_baseaddr)) |
  2429. (readl(USBD_EPDMAST(udc->udp_baseaddr)) &
  2430. readl(USBD_NDDRTINTST(udc->udp_baseaddr))) |
  2431. readl(USBD_SYSERRTINTST(udc->udp_baseaddr));
  2432. for (i = 1; i < NUM_ENDPOINTS; i++) {
  2433. if (tmp & (1 << udc->ep[i].hwep_num))
  2434. udc_handle_dma_ep(udc, &udc->ep[i]);
  2435. }
  2436. spin_unlock(&udc->lock);
  2437. return IRQ_HANDLED;
  2438. }
  2439. /*
  2440. *
  2441. * VBUS detection, pullup handler, and Gadget cable state notification
  2442. *
  2443. */
  2444. static void vbus_work(struct work_struct *work)
  2445. {
  2446. u8 value;
  2447. struct lpc32xx_udc *udc = container_of(work, struct lpc32xx_udc,
  2448. vbus_job);
  2449. if (udc->enabled != 0) {
  2450. /* Discharge VBUS real quick */
  2451. i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
  2452. ISP1301_I2C_OTG_CONTROL_1, OTG1_VBUS_DISCHRG);
  2453. /* Give VBUS some time (100mS) to discharge */
  2454. msleep(100);
  2455. /* Disable VBUS discharge resistor */
  2456. i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
  2457. ISP1301_I2C_OTG_CONTROL_1 | ISP1301_I2C_REG_CLEAR_ADDR,
  2458. OTG1_VBUS_DISCHRG);
  2459. /* Clear interrupt */
  2460. i2c_smbus_write_byte_data(udc->isp1301_i2c_client,
  2461. ISP1301_I2C_INTERRUPT_LATCH |
  2462. ISP1301_I2C_REG_CLEAR_ADDR, ~0);
  2463. /* Get the VBUS status from the transceiver */
  2464. value = i2c_smbus_read_byte_data(udc->isp1301_i2c_client,
  2465. ISP1301_I2C_INTERRUPT_SOURCE);
  2466. /* VBUS on or off? */
  2467. if (value & INT_SESS_VLD)
  2468. udc->vbus = 1;
  2469. else
  2470. udc->vbus = 0;
  2471. /* VBUS changed? */
  2472. if (udc->last_vbus != udc->vbus) {
  2473. udc->last_vbus = udc->vbus;
  2474. lpc32xx_vbus_session(&udc->gadget, udc->vbus);
  2475. }
  2476. }
  2477. /* Re-enable after completion */
  2478. enable_irq(udc->udp_irq[IRQ_USB_ATX]);
  2479. }
  2480. static irqreturn_t lpc32xx_usb_vbus_irq(int irq, void *_udc)
  2481. {
  2482. struct lpc32xx_udc *udc = _udc;
  2483. /* Defer handling of VBUS IRQ to work queue */
  2484. disable_irq_nosync(udc->udp_irq[IRQ_USB_ATX]);
  2485. schedule_work(&udc->vbus_job);
  2486. return IRQ_HANDLED;
  2487. }
  2488. static int lpc32xx_start(struct usb_gadget *gadget,
  2489. struct usb_gadget_driver *driver)
  2490. {
  2491. struct lpc32xx_udc *udc = to_udc(gadget);
  2492. int i;
  2493. if (!driver || driver->max_speed < USB_SPEED_FULL || !driver->setup) {
  2494. dev_err(udc->dev, "bad parameter.\n");
  2495. return -EINVAL;
  2496. }
  2497. if (udc->driver) {
  2498. dev_err(udc->dev, "UDC already has a gadget driver\n");
  2499. return -EBUSY;
  2500. }
  2501. udc->driver = driver;
  2502. udc->gadget.dev.driver = &driver->driver;
  2503. udc->gadget.dev.of_node = udc->dev->of_node;
  2504. udc->enabled = 1;
  2505. udc->selfpowered = 1;
  2506. udc->vbus = 0;
  2507. /* Force VBUS process once to check for cable insertion */
  2508. udc->last_vbus = udc->vbus = 0;
  2509. schedule_work(&udc->vbus_job);
  2510. /* Do not re-enable ATX IRQ (3) */
  2511. for (i = IRQ_USB_LP; i < IRQ_USB_ATX; i++)
  2512. enable_irq(udc->udp_irq[i]);
  2513. return 0;
  2514. }
  2515. static int lpc32xx_stop(struct usb_gadget *gadget,
  2516. struct usb_gadget_driver *driver)
  2517. {
  2518. int i;
  2519. struct lpc32xx_udc *udc = to_udc(gadget);
  2520. if (!driver || driver != udc->driver)
  2521. return -EINVAL;
  2522. for (i = IRQ_USB_LP; i <= IRQ_USB_ATX; i++)
  2523. disable_irq(udc->udp_irq[i]);
  2524. if (udc->clocked) {
  2525. spin_lock(&udc->lock);
  2526. stop_activity(udc);
  2527. spin_unlock(&udc->lock);
  2528. /*
  2529. * Wait for all the endpoints to disable,
  2530. * before disabling clocks. Don't wait if
  2531. * endpoints are not enabled.
  2532. */
  2533. if (atomic_read(&udc->enabled_ep_cnt))
  2534. wait_event_interruptible(udc->ep_disable_wait_queue,
  2535. (atomic_read(&udc->enabled_ep_cnt) == 0));
  2536. spin_lock(&udc->lock);
  2537. udc_clk_set(udc, 0);
  2538. spin_unlock(&udc->lock);
  2539. }
  2540. udc->enabled = 0;
  2541. udc->gadget.dev.driver = NULL;
  2542. udc->driver = NULL;
  2543. return 0;
  2544. }
  2545. static void lpc32xx_udc_shutdown(struct platform_device *dev)
  2546. {
  2547. /* Force disconnect on reboot */
  2548. struct lpc32xx_udc *udc = platform_get_drvdata(dev);
  2549. pullup(udc, 0);
  2550. }
  2551. /*
  2552. * Callbacks to be overridden by options passed via OF (TODO)
  2553. */
  2554. static void lpc32xx_usbd_conn_chg(int conn)
  2555. {
  2556. /* Do nothing, it might be nice to enable an LED
  2557. * based on conn state being !0 */
  2558. }
  2559. static void lpc32xx_usbd_susp_chg(int susp)
  2560. {
  2561. /* Device suspend if susp != 0 */
  2562. }
  2563. static void lpc32xx_rmwkup_chg(int remote_wakup_enable)
  2564. {
  2565. /* Enable or disable USB remote wakeup */
  2566. }
  2567. struct lpc32xx_usbd_cfg lpc32xx_usbddata = {
  2568. .vbus_drv_pol = 0,
  2569. .conn_chgb = &lpc32xx_usbd_conn_chg,
  2570. .susp_chgb = &lpc32xx_usbd_susp_chg,
  2571. .rmwk_chgb = &lpc32xx_rmwkup_chg,
  2572. };
  2573. static u64 lpc32xx_usbd_dmamask = ~(u32) 0x7F;
  2574. static int __init lpc32xx_udc_probe(struct platform_device *pdev)
  2575. {
  2576. struct device *dev = &pdev->dev;
  2577. struct lpc32xx_udc *udc;
  2578. int retval, i;
  2579. struct resource *res;
  2580. dma_addr_t dma_handle;
  2581. struct device_node *isp1301_node;
  2582. udc = kzalloc(sizeof(*udc), GFP_KERNEL);
  2583. if (!udc)
  2584. return -ENOMEM;
  2585. memcpy(udc, &controller_template, sizeof(*udc));
  2586. for (i = 0; i <= 15; i++)
  2587. udc->ep[i].udc = udc;
  2588. udc->gadget.ep0 = &udc->ep[0].ep;
  2589. /* init software state */
  2590. udc->gadget.dev.parent = dev;
  2591. udc->pdev = pdev;
  2592. udc->dev = &pdev->dev;
  2593. udc->enabled = 0;
  2594. if (pdev->dev.of_node) {
  2595. isp1301_node = of_parse_phandle(pdev->dev.of_node,
  2596. "transceiver", 0);
  2597. } else {
  2598. isp1301_node = NULL;
  2599. }
  2600. udc->isp1301_i2c_client = isp1301_get_client(isp1301_node);
  2601. if (!udc->isp1301_i2c_client) {
  2602. retval = -EPROBE_DEFER;
  2603. goto phy_fail;
  2604. }
  2605. dev_info(udc->dev, "ISP1301 I2C device at address 0x%x\n",
  2606. udc->isp1301_i2c_client->addr);
  2607. pdev->dev.dma_mask = &lpc32xx_usbd_dmamask;
  2608. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  2609. udc->board = &lpc32xx_usbddata;
  2610. /*
  2611. * Resources are mapped as follows:
  2612. * IORESOURCE_MEM, base address and size of USB space
  2613. * IORESOURCE_IRQ, USB device low priority interrupt number
  2614. * IORESOURCE_IRQ, USB device high priority interrupt number
  2615. * IORESOURCE_IRQ, USB device interrupt number
  2616. * IORESOURCE_IRQ, USB transceiver interrupt number
  2617. */
  2618. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2619. if (!res) {
  2620. retval = -ENXIO;
  2621. goto resource_fail;
  2622. }
  2623. spin_lock_init(&udc->lock);
  2624. /* Get IRQs */
  2625. for (i = 0; i < 4; i++) {
  2626. udc->udp_irq[i] = platform_get_irq(pdev, i);
  2627. if (udc->udp_irq[i] < 0) {
  2628. dev_err(udc->dev,
  2629. "irq resource %d not available!\n", i);
  2630. retval = udc->udp_irq[i];
  2631. goto irq_fail;
  2632. }
  2633. }
  2634. udc->io_p_start = res->start;
  2635. udc->io_p_size = resource_size(res);
  2636. if (!request_mem_region(udc->io_p_start, udc->io_p_size, driver_name)) {
  2637. dev_err(udc->dev, "someone's using UDC memory\n");
  2638. retval = -EBUSY;
  2639. goto request_mem_region_fail;
  2640. }
  2641. udc->udp_baseaddr = ioremap(udc->io_p_start, udc->io_p_size);
  2642. if (!udc->udp_baseaddr) {
  2643. retval = -ENOMEM;
  2644. dev_err(udc->dev, "IO map failure\n");
  2645. goto io_map_fail;
  2646. }
  2647. /* Enable AHB slave USB clock, needed for further USB clock control */
  2648. writel(USB_SLAVE_HCLK_EN | (1 << 19), USB_CTRL);
  2649. /* Get required clocks */
  2650. udc->usb_pll_clk = clk_get(&pdev->dev, "ck_pll5");
  2651. if (IS_ERR(udc->usb_pll_clk)) {
  2652. dev_err(udc->dev, "failed to acquire USB PLL\n");
  2653. retval = PTR_ERR(udc->usb_pll_clk);
  2654. goto pll_get_fail;
  2655. }
  2656. udc->usb_slv_clk = clk_get(&pdev->dev, "ck_usbd");
  2657. if (IS_ERR(udc->usb_slv_clk)) {
  2658. dev_err(udc->dev, "failed to acquire USB device clock\n");
  2659. retval = PTR_ERR(udc->usb_slv_clk);
  2660. goto usb_clk_get_fail;
  2661. }
  2662. udc->usb_otg_clk = clk_get(&pdev->dev, "ck_usb_otg");
  2663. if (IS_ERR(udc->usb_otg_clk)) {
  2664. dev_err(udc->dev, "failed to acquire USB otg clock\n");
  2665. retval = PTR_ERR(udc->usb_otg_clk);
  2666. goto usb_otg_clk_get_fail;
  2667. }
  2668. /* Setup PLL clock to 48MHz */
  2669. retval = clk_enable(udc->usb_pll_clk);
  2670. if (retval < 0) {
  2671. dev_err(udc->dev, "failed to start USB PLL\n");
  2672. goto pll_enable_fail;
  2673. }
  2674. retval = clk_set_rate(udc->usb_pll_clk, 48000);
  2675. if (retval < 0) {
  2676. dev_err(udc->dev, "failed to set USB clock rate\n");
  2677. goto pll_set_fail;
  2678. }
  2679. writel(readl(USB_CTRL) | USB_DEV_NEED_CLK_EN, USB_CTRL);
  2680. /* Enable USB device clock */
  2681. retval = clk_enable(udc->usb_slv_clk);
  2682. if (retval < 0) {
  2683. dev_err(udc->dev, "failed to start USB device clock\n");
  2684. goto usb_clk_enable_fail;
  2685. }
  2686. /* Enable USB OTG clock */
  2687. retval = clk_enable(udc->usb_otg_clk);
  2688. if (retval < 0) {
  2689. dev_err(udc->dev, "failed to start USB otg clock\n");
  2690. goto usb_otg_clk_enable_fail;
  2691. }
  2692. /* Setup deferred workqueue data */
  2693. udc->poweron = udc->pullup = 0;
  2694. INIT_WORK(&udc->pullup_job, pullup_work);
  2695. INIT_WORK(&udc->vbus_job, vbus_work);
  2696. #ifdef CONFIG_PM
  2697. INIT_WORK(&udc->power_job, power_work);
  2698. #endif
  2699. /* All clocks are now on */
  2700. udc->clocked = 1;
  2701. isp1301_udc_configure(udc);
  2702. /* Allocate memory for the UDCA */
  2703. udc->udca_v_base = dma_alloc_coherent(&pdev->dev, UDCA_BUFF_SIZE,
  2704. &dma_handle,
  2705. (GFP_KERNEL | GFP_DMA));
  2706. if (!udc->udca_v_base) {
  2707. dev_err(udc->dev, "error getting UDCA region\n");
  2708. retval = -ENOMEM;
  2709. goto i2c_fail;
  2710. }
  2711. udc->udca_p_base = dma_handle;
  2712. dev_dbg(udc->dev, "DMA buffer(0x%x bytes), P:0x%08x, V:0x%p\n",
  2713. UDCA_BUFF_SIZE, udc->udca_p_base, udc->udca_v_base);
  2714. /* Setup the DD DMA memory pool */
  2715. udc->dd_cache = dma_pool_create("udc_dd", udc->dev,
  2716. sizeof(struct lpc32xx_usbd_dd_gad),
  2717. sizeof(u32), 0);
  2718. if (!udc->dd_cache) {
  2719. dev_err(udc->dev, "error getting DD DMA region\n");
  2720. retval = -ENOMEM;
  2721. goto dma_alloc_fail;
  2722. }
  2723. /* Clear USB peripheral and initialize gadget endpoints */
  2724. udc_disable(udc);
  2725. udc_reinit(udc);
  2726. retval = device_register(&udc->gadget.dev);
  2727. if (retval < 0) {
  2728. dev_err(udc->dev, "Device registration failure\n");
  2729. goto dev_register_fail;
  2730. }
  2731. /* Request IRQs - low and high priority USB device IRQs are routed to
  2732. * the same handler, while the DMA interrupt is routed elsewhere */
  2733. retval = request_irq(udc->udp_irq[IRQ_USB_LP], lpc32xx_usb_lp_irq,
  2734. 0, "udc_lp", udc);
  2735. if (retval < 0) {
  2736. dev_err(udc->dev, "LP request irq %d failed\n",
  2737. udc->udp_irq[IRQ_USB_LP]);
  2738. goto irq_lp_fail;
  2739. }
  2740. retval = request_irq(udc->udp_irq[IRQ_USB_HP], lpc32xx_usb_hp_irq,
  2741. 0, "udc_hp", udc);
  2742. if (retval < 0) {
  2743. dev_err(udc->dev, "HP request irq %d failed\n",
  2744. udc->udp_irq[IRQ_USB_HP]);
  2745. goto irq_hp_fail;
  2746. }
  2747. retval = request_irq(udc->udp_irq[IRQ_USB_DEVDMA],
  2748. lpc32xx_usb_devdma_irq, 0, "udc_dma", udc);
  2749. if (retval < 0) {
  2750. dev_err(udc->dev, "DEV request irq %d failed\n",
  2751. udc->udp_irq[IRQ_USB_DEVDMA]);
  2752. goto irq_dev_fail;
  2753. }
  2754. /* The transceiver interrupt is used for VBUS detection and will
  2755. kick off the VBUS handler function */
  2756. retval = request_irq(udc->udp_irq[IRQ_USB_ATX], lpc32xx_usb_vbus_irq,
  2757. 0, "udc_otg", udc);
  2758. if (retval < 0) {
  2759. dev_err(udc->dev, "VBUS request irq %d failed\n",
  2760. udc->udp_irq[IRQ_USB_ATX]);
  2761. goto irq_xcvr_fail;
  2762. }
  2763. /* Initialize wait queue */
  2764. init_waitqueue_head(&udc->ep_disable_wait_queue);
  2765. atomic_set(&udc->enabled_ep_cnt, 0);
  2766. /* Keep all IRQs disabled until GadgetFS starts up */
  2767. for (i = IRQ_USB_LP; i <= IRQ_USB_ATX; i++)
  2768. disable_irq(udc->udp_irq[i]);
  2769. retval = usb_add_gadget_udc(dev, &udc->gadget);
  2770. if (retval < 0)
  2771. goto add_gadget_fail;
  2772. dev_set_drvdata(dev, udc);
  2773. device_init_wakeup(dev, 1);
  2774. create_debug_file(udc);
  2775. /* Disable clocks for now */
  2776. udc_clk_set(udc, 0);
  2777. dev_info(udc->dev, "%s version %s\n", driver_name, DRIVER_VERSION);
  2778. return 0;
  2779. add_gadget_fail:
  2780. free_irq(udc->udp_irq[IRQ_USB_ATX], udc);
  2781. irq_xcvr_fail:
  2782. free_irq(udc->udp_irq[IRQ_USB_DEVDMA], udc);
  2783. irq_dev_fail:
  2784. free_irq(udc->udp_irq[IRQ_USB_HP], udc);
  2785. irq_hp_fail:
  2786. free_irq(udc->udp_irq[IRQ_USB_LP], udc);
  2787. irq_lp_fail:
  2788. device_unregister(&udc->gadget.dev);
  2789. dev_register_fail:
  2790. dma_pool_destroy(udc->dd_cache);
  2791. dma_alloc_fail:
  2792. dma_free_coherent(&pdev->dev, UDCA_BUFF_SIZE,
  2793. udc->udca_v_base, udc->udca_p_base);
  2794. i2c_fail:
  2795. clk_disable(udc->usb_otg_clk);
  2796. usb_otg_clk_enable_fail:
  2797. clk_disable(udc->usb_slv_clk);
  2798. usb_clk_enable_fail:
  2799. pll_set_fail:
  2800. clk_disable(udc->usb_pll_clk);
  2801. pll_enable_fail:
  2802. clk_put(udc->usb_slv_clk);
  2803. usb_otg_clk_get_fail:
  2804. clk_put(udc->usb_otg_clk);
  2805. usb_clk_get_fail:
  2806. clk_put(udc->usb_pll_clk);
  2807. pll_get_fail:
  2808. iounmap(udc->udp_baseaddr);
  2809. io_map_fail:
  2810. release_mem_region(udc->io_p_start, udc->io_p_size);
  2811. dev_err(udc->dev, "%s probe failed, %d\n", driver_name, retval);
  2812. request_mem_region_fail:
  2813. irq_fail:
  2814. resource_fail:
  2815. phy_fail:
  2816. kfree(udc);
  2817. return retval;
  2818. }
  2819. static int lpc32xx_udc_remove(struct platform_device *pdev)
  2820. {
  2821. struct lpc32xx_udc *udc = platform_get_drvdata(pdev);
  2822. usb_del_gadget_udc(&udc->gadget);
  2823. if (udc->driver)
  2824. return -EBUSY;
  2825. udc_clk_set(udc, 1);
  2826. udc_disable(udc);
  2827. pullup(udc, 0);
  2828. free_irq(udc->udp_irq[IRQ_USB_ATX], udc);
  2829. device_init_wakeup(&pdev->dev, 0);
  2830. remove_debug_file(udc);
  2831. dma_pool_destroy(udc->dd_cache);
  2832. dma_free_coherent(&pdev->dev, UDCA_BUFF_SIZE,
  2833. udc->udca_v_base, udc->udca_p_base);
  2834. free_irq(udc->udp_irq[IRQ_USB_DEVDMA], udc);
  2835. free_irq(udc->udp_irq[IRQ_USB_HP], udc);
  2836. free_irq(udc->udp_irq[IRQ_USB_LP], udc);
  2837. device_unregister(&udc->gadget.dev);
  2838. clk_disable(udc->usb_otg_clk);
  2839. clk_put(udc->usb_otg_clk);
  2840. clk_disable(udc->usb_slv_clk);
  2841. clk_put(udc->usb_slv_clk);
  2842. clk_disable(udc->usb_pll_clk);
  2843. clk_put(udc->usb_pll_clk);
  2844. iounmap(udc->udp_baseaddr);
  2845. release_mem_region(udc->io_p_start, udc->io_p_size);
  2846. kfree(udc);
  2847. return 0;
  2848. }
  2849. #ifdef CONFIG_PM
  2850. static int lpc32xx_udc_suspend(struct platform_device *pdev, pm_message_t mesg)
  2851. {
  2852. struct lpc32xx_udc *udc = platform_get_drvdata(pdev);
  2853. if (udc->clocked) {
  2854. /* Power down ISP */
  2855. udc->poweron = 0;
  2856. isp1301_set_powerstate(udc, 0);
  2857. /* Disable clocking */
  2858. udc_clk_set(udc, 0);
  2859. /* Keep clock flag on, so we know to re-enable clocks
  2860. on resume */
  2861. udc->clocked = 1;
  2862. /* Kill global USB clock */
  2863. clk_disable(udc->usb_slv_clk);
  2864. }
  2865. return 0;
  2866. }
  2867. static int lpc32xx_udc_resume(struct platform_device *pdev)
  2868. {
  2869. struct lpc32xx_udc *udc = platform_get_drvdata(pdev);
  2870. if (udc->clocked) {
  2871. /* Enable global USB clock */
  2872. clk_enable(udc->usb_slv_clk);
  2873. /* Enable clocking */
  2874. udc_clk_set(udc, 1);
  2875. /* ISP back to normal power mode */
  2876. udc->poweron = 1;
  2877. isp1301_set_powerstate(udc, 1);
  2878. }
  2879. return 0;
  2880. }
  2881. #else
  2882. #define lpc32xx_udc_suspend NULL
  2883. #define lpc32xx_udc_resume NULL
  2884. #endif
  2885. #ifdef CONFIG_OF
  2886. static struct of_device_id lpc32xx_udc_of_match[] = {
  2887. { .compatible = "nxp,lpc3220-udc", },
  2888. { },
  2889. };
  2890. MODULE_DEVICE_TABLE(of, lpc32xx_udc_of_match);
  2891. #endif
  2892. static struct platform_driver lpc32xx_udc_driver = {
  2893. .remove = lpc32xx_udc_remove,
  2894. .shutdown = lpc32xx_udc_shutdown,
  2895. .suspend = lpc32xx_udc_suspend,
  2896. .resume = lpc32xx_udc_resume,
  2897. .driver = {
  2898. .name = (char *) driver_name,
  2899. .owner = THIS_MODULE,
  2900. .of_match_table = of_match_ptr(lpc32xx_udc_of_match),
  2901. },
  2902. };
  2903. static int __init udc_init_module(void)
  2904. {
  2905. return platform_driver_probe(&lpc32xx_udc_driver, lpc32xx_udc_probe);
  2906. }
  2907. module_init(udc_init_module);
  2908. static void __exit udc_exit_module(void)
  2909. {
  2910. platform_driver_unregister(&lpc32xx_udc_driver);
  2911. }
  2912. module_exit(udc_exit_module);
  2913. MODULE_DESCRIPTION("LPC32XX udc driver");
  2914. MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com>");
  2915. MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
  2916. MODULE_LICENSE("GPL");
  2917. MODULE_ALIAS("platform:lpc32xx_udc");