lpc32xx_udc.c 86 KB

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