lpc32xx_udc.c 87 KB

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