lpc32xx_udc.c 88 KB

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