lpc32xx_udc.c 84 KB

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