lpc32xx_udc.c 81 KB

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