pxa25x_udc.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Intel PXA25x and IXP4xx on-chip full speed USB device controllers
  4. *
  5. * Copyright (C) 2002 Intrinsyc, Inc. (Frank Becker)
  6. * Copyright (C) 2003 Robert Schwebel, Pengutronix
  7. * Copyright (C) 2003 Benedikt Spranger, Pengutronix
  8. * Copyright (C) 2003 David Brownell
  9. * Copyright (C) 2003 Joshua Wise
  10. */
  11. /* #define VERBOSE_DEBUG */
  12. #include <linux/device.h>
  13. #include <linux/gpio.h>
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/ioport.h>
  17. #include <linux/types.h>
  18. #include <linux/errno.h>
  19. #include <linux/err.h>
  20. #include <linux/delay.h>
  21. #include <linux/slab.h>
  22. #include <linux/timer.h>
  23. #include <linux/list.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/mm.h>
  26. #include <linux/platform_data/pxa2xx_udc.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/irq.h>
  30. #include <linux/clk.h>
  31. #include <linux/seq_file.h>
  32. #include <linux/debugfs.h>
  33. #include <linux/io.h>
  34. #include <linux/prefetch.h>
  35. #include <asm/byteorder.h>
  36. #include <asm/dma.h>
  37. #include <asm/mach-types.h>
  38. #include <asm/unaligned.h>
  39. #include <linux/usb/ch9.h>
  40. #include <linux/usb/gadget.h>
  41. #include <linux/usb/otg.h>
  42. #ifdef CONFIG_ARCH_LUBBOCK
  43. #include <mach/lubbock.h>
  44. #endif
  45. #define UDCCR 0x0000 /* UDC Control Register */
  46. #define UDC_RES1 0x0004 /* UDC Undocumented - Reserved1 */
  47. #define UDC_RES2 0x0008 /* UDC Undocumented - Reserved2 */
  48. #define UDC_RES3 0x000C /* UDC Undocumented - Reserved3 */
  49. #define UDCCS0 0x0010 /* UDC Endpoint 0 Control/Status Register */
  50. #define UDCCS1 0x0014 /* UDC Endpoint 1 (IN) Control/Status Register */
  51. #define UDCCS2 0x0018 /* UDC Endpoint 2 (OUT) Control/Status Register */
  52. #define UDCCS3 0x001C /* UDC Endpoint 3 (IN) Control/Status Register */
  53. #define UDCCS4 0x0020 /* UDC Endpoint 4 (OUT) Control/Status Register */
  54. #define UDCCS5 0x0024 /* UDC Endpoint 5 (Interrupt) Control/Status Register */
  55. #define UDCCS6 0x0028 /* UDC Endpoint 6 (IN) Control/Status Register */
  56. #define UDCCS7 0x002C /* UDC Endpoint 7 (OUT) Control/Status Register */
  57. #define UDCCS8 0x0030 /* UDC Endpoint 8 (IN) Control/Status Register */
  58. #define UDCCS9 0x0034 /* UDC Endpoint 9 (OUT) Control/Status Register */
  59. #define UDCCS10 0x0038 /* UDC Endpoint 10 (Interrupt) Control/Status Register */
  60. #define UDCCS11 0x003C /* UDC Endpoint 11 (IN) Control/Status Register */
  61. #define UDCCS12 0x0040 /* UDC Endpoint 12 (OUT) Control/Status Register */
  62. #define UDCCS13 0x0044 /* UDC Endpoint 13 (IN) Control/Status Register */
  63. #define UDCCS14 0x0048 /* UDC Endpoint 14 (OUT) Control/Status Register */
  64. #define UDCCS15 0x004C /* UDC Endpoint 15 (Interrupt) Control/Status Register */
  65. #define UFNRH 0x0060 /* UDC Frame Number Register High */
  66. #define UFNRL 0x0064 /* UDC Frame Number Register Low */
  67. #define UBCR2 0x0068 /* UDC Byte Count Reg 2 */
  68. #define UBCR4 0x006c /* UDC Byte Count Reg 4 */
  69. #define UBCR7 0x0070 /* UDC Byte Count Reg 7 */
  70. #define UBCR9 0x0074 /* UDC Byte Count Reg 9 */
  71. #define UBCR12 0x0078 /* UDC Byte Count Reg 12 */
  72. #define UBCR14 0x007c /* UDC Byte Count Reg 14 */
  73. #define UDDR0 0x0080 /* UDC Endpoint 0 Data Register */
  74. #define UDDR1 0x0100 /* UDC Endpoint 1 Data Register */
  75. #define UDDR2 0x0180 /* UDC Endpoint 2 Data Register */
  76. #define UDDR3 0x0200 /* UDC Endpoint 3 Data Register */
  77. #define UDDR4 0x0400 /* UDC Endpoint 4 Data Register */
  78. #define UDDR5 0x00A0 /* UDC Endpoint 5 Data Register */
  79. #define UDDR6 0x0600 /* UDC Endpoint 6 Data Register */
  80. #define UDDR7 0x0680 /* UDC Endpoint 7 Data Register */
  81. #define UDDR8 0x0700 /* UDC Endpoint 8 Data Register */
  82. #define UDDR9 0x0900 /* UDC Endpoint 9 Data Register */
  83. #define UDDR10 0x00C0 /* UDC Endpoint 10 Data Register */
  84. #define UDDR11 0x0B00 /* UDC Endpoint 11 Data Register */
  85. #define UDDR12 0x0B80 /* UDC Endpoint 12 Data Register */
  86. #define UDDR13 0x0C00 /* UDC Endpoint 13 Data Register */
  87. #define UDDR14 0x0E00 /* UDC Endpoint 14 Data Register */
  88. #define UDDR15 0x00E0 /* UDC Endpoint 15 Data Register */
  89. #define UICR0 0x0050 /* UDC Interrupt Control Register 0 */
  90. #define UICR1 0x0054 /* UDC Interrupt Control Register 1 */
  91. #define USIR0 0x0058 /* UDC Status Interrupt Register 0 */
  92. #define USIR1 0x005C /* UDC Status Interrupt Register 1 */
  93. #define UDCCR_UDE (1 << 0) /* UDC enable */
  94. #define UDCCR_UDA (1 << 1) /* UDC active */
  95. #define UDCCR_RSM (1 << 2) /* Device resume */
  96. #define UDCCR_RESIR (1 << 3) /* Resume interrupt request */
  97. #define UDCCR_SUSIR (1 << 4) /* Suspend interrupt request */
  98. #define UDCCR_SRM (1 << 5) /* Suspend/resume interrupt mask */
  99. #define UDCCR_RSTIR (1 << 6) /* Reset interrupt request */
  100. #define UDCCR_REM (1 << 7) /* Reset interrupt mask */
  101. #define UDCCS0_OPR (1 << 0) /* OUT packet ready */
  102. #define UDCCS0_IPR (1 << 1) /* IN packet ready */
  103. #define UDCCS0_FTF (1 << 2) /* Flush Tx FIFO */
  104. #define UDCCS0_DRWF (1 << 3) /* Device remote wakeup feature */
  105. #define UDCCS0_SST (1 << 4) /* Sent stall */
  106. #define UDCCS0_FST (1 << 5) /* Force stall */
  107. #define UDCCS0_RNE (1 << 6) /* Receive FIFO no empty */
  108. #define UDCCS0_SA (1 << 7) /* Setup active */
  109. #define UDCCS_BI_TFS (1 << 0) /* Transmit FIFO service */
  110. #define UDCCS_BI_TPC (1 << 1) /* Transmit packet complete */
  111. #define UDCCS_BI_FTF (1 << 2) /* Flush Tx FIFO */
  112. #define UDCCS_BI_TUR (1 << 3) /* Transmit FIFO underrun */
  113. #define UDCCS_BI_SST (1 << 4) /* Sent stall */
  114. #define UDCCS_BI_FST (1 << 5) /* Force stall */
  115. #define UDCCS_BI_TSP (1 << 7) /* Transmit short packet */
  116. #define UDCCS_BO_RFS (1 << 0) /* Receive FIFO service */
  117. #define UDCCS_BO_RPC (1 << 1) /* Receive packet complete */
  118. #define UDCCS_BO_DME (1 << 3) /* DMA enable */
  119. #define UDCCS_BO_SST (1 << 4) /* Sent stall */
  120. #define UDCCS_BO_FST (1 << 5) /* Force stall */
  121. #define UDCCS_BO_RNE (1 << 6) /* Receive FIFO not empty */
  122. #define UDCCS_BO_RSP (1 << 7) /* Receive short packet */
  123. #define UDCCS_II_TFS (1 << 0) /* Transmit FIFO service */
  124. #define UDCCS_II_TPC (1 << 1) /* Transmit packet complete */
  125. #define UDCCS_II_FTF (1 << 2) /* Flush Tx FIFO */
  126. #define UDCCS_II_TUR (1 << 3) /* Transmit FIFO underrun */
  127. #define UDCCS_II_TSP (1 << 7) /* Transmit short packet */
  128. #define UDCCS_IO_RFS (1 << 0) /* Receive FIFO service */
  129. #define UDCCS_IO_RPC (1 << 1) /* Receive packet complete */
  130. #ifdef CONFIG_ARCH_IXP4XX /* FIXME: is this right?, datasheed says '2' */
  131. #define UDCCS_IO_ROF (1 << 3) /* Receive overflow */
  132. #endif
  133. #ifdef CONFIG_ARCH_PXA
  134. #define UDCCS_IO_ROF (1 << 2) /* Receive overflow */
  135. #endif
  136. #define UDCCS_IO_DME (1 << 3) /* DMA enable */
  137. #define UDCCS_IO_RNE (1 << 6) /* Receive FIFO not empty */
  138. #define UDCCS_IO_RSP (1 << 7) /* Receive short packet */
  139. #define UDCCS_INT_TFS (1 << 0) /* Transmit FIFO service */
  140. #define UDCCS_INT_TPC (1 << 1) /* Transmit packet complete */
  141. #define UDCCS_INT_FTF (1 << 2) /* Flush Tx FIFO */
  142. #define UDCCS_INT_TUR (1 << 3) /* Transmit FIFO underrun */
  143. #define UDCCS_INT_SST (1 << 4) /* Sent stall */
  144. #define UDCCS_INT_FST (1 << 5) /* Force stall */
  145. #define UDCCS_INT_TSP (1 << 7) /* Transmit short packet */
  146. #define UICR0_IM0 (1 << 0) /* Interrupt mask ep 0 */
  147. #define UICR0_IM1 (1 << 1) /* Interrupt mask ep 1 */
  148. #define UICR0_IM2 (1 << 2) /* Interrupt mask ep 2 */
  149. #define UICR0_IM3 (1 << 3) /* Interrupt mask ep 3 */
  150. #define UICR0_IM4 (1 << 4) /* Interrupt mask ep 4 */
  151. #define UICR0_IM5 (1 << 5) /* Interrupt mask ep 5 */
  152. #define UICR0_IM6 (1 << 6) /* Interrupt mask ep 6 */
  153. #define UICR0_IM7 (1 << 7) /* Interrupt mask ep 7 */
  154. #define UICR1_IM8 (1 << 0) /* Interrupt mask ep 8 */
  155. #define UICR1_IM9 (1 << 1) /* Interrupt mask ep 9 */
  156. #define UICR1_IM10 (1 << 2) /* Interrupt mask ep 10 */
  157. #define UICR1_IM11 (1 << 3) /* Interrupt mask ep 11 */
  158. #define UICR1_IM12 (1 << 4) /* Interrupt mask ep 12 */
  159. #define UICR1_IM13 (1 << 5) /* Interrupt mask ep 13 */
  160. #define UICR1_IM14 (1 << 6) /* Interrupt mask ep 14 */
  161. #define UICR1_IM15 (1 << 7) /* Interrupt mask ep 15 */
  162. #define USIR0_IR0 (1 << 0) /* Interrupt request ep 0 */
  163. #define USIR0_IR1 (1 << 1) /* Interrupt request ep 1 */
  164. #define USIR0_IR2 (1 << 2) /* Interrupt request ep 2 */
  165. #define USIR0_IR3 (1 << 3) /* Interrupt request ep 3 */
  166. #define USIR0_IR4 (1 << 4) /* Interrupt request ep 4 */
  167. #define USIR0_IR5 (1 << 5) /* Interrupt request ep 5 */
  168. #define USIR0_IR6 (1 << 6) /* Interrupt request ep 6 */
  169. #define USIR0_IR7 (1 << 7) /* Interrupt request ep 7 */
  170. #define USIR1_IR8 (1 << 0) /* Interrupt request ep 8 */
  171. #define USIR1_IR9 (1 << 1) /* Interrupt request ep 9 */
  172. #define USIR1_IR10 (1 << 2) /* Interrupt request ep 10 */
  173. #define USIR1_IR11 (1 << 3) /* Interrupt request ep 11 */
  174. #define USIR1_IR12 (1 << 4) /* Interrupt request ep 12 */
  175. #define USIR1_IR13 (1 << 5) /* Interrupt request ep 13 */
  176. #define USIR1_IR14 (1 << 6) /* Interrupt request ep 14 */
  177. #define USIR1_IR15 (1 << 7) /* Interrupt request ep 15 */
  178. /*
  179. * This driver handles the USB Device Controller (UDC) in Intel's PXA 25x
  180. * series processors. The UDC for the IXP 4xx series is very similar.
  181. * There are fifteen endpoints, in addition to ep0.
  182. *
  183. * Such controller drivers work with a gadget driver. The gadget driver
  184. * returns descriptors, implements configuration and data protocols used
  185. * by the host to interact with this device, and allocates endpoints to
  186. * the different protocol interfaces. The controller driver virtualizes
  187. * usb hardware so that the gadget drivers will be more portable.
  188. *
  189. * This UDC hardware wants to implement a bit too much USB protocol, so
  190. * it constrains the sorts of USB configuration change events that work.
  191. * The errata for these chips are misleading; some "fixed" bugs from
  192. * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
  193. *
  194. * Note that the UDC hardware supports DMA (except on IXP) but that's
  195. * not used here. IN-DMA (to host) is simple enough, when the data is
  196. * suitably aligned (16 bytes) ... the network stack doesn't do that,
  197. * other software can. OUT-DMA is buggy in most chip versions, as well
  198. * as poorly designed (data toggle not automatic). So this driver won't
  199. * bother using DMA. (Mostly-working IN-DMA support was available in
  200. * kernels before 2.6.23, but was never enabled or well tested.)
  201. */
  202. #define DRIVER_VERSION "30-June-2007"
  203. #define DRIVER_DESC "PXA 25x USB Device Controller driver"
  204. static const char driver_name [] = "pxa25x_udc";
  205. static const char ep0name [] = "ep0";
  206. #ifdef CONFIG_ARCH_IXP4XX
  207. /* cpu-specific register addresses are compiled in to this code */
  208. #ifdef CONFIG_ARCH_PXA
  209. #error "Can't configure both IXP and PXA"
  210. #endif
  211. /* IXP doesn't yet support <linux/clk.h> */
  212. #define clk_get(dev,name) NULL
  213. #define clk_enable(clk) do { } while (0)
  214. #define clk_disable(clk) do { } while (0)
  215. #define clk_put(clk) do { } while (0)
  216. #endif
  217. #include "pxa25x_udc.h"
  218. #ifdef CONFIG_USB_PXA25X_SMALL
  219. #define SIZE_STR " (small)"
  220. #else
  221. #define SIZE_STR ""
  222. #endif
  223. /* ---------------------------------------------------------------------------
  224. * endpoint related parts of the api to the usb controller hardware,
  225. * used by gadget driver; and the inner talker-to-hardware core.
  226. * ---------------------------------------------------------------------------
  227. */
  228. static void pxa25x_ep_fifo_flush (struct usb_ep *ep);
  229. static void nuke (struct pxa25x_ep *, int status);
  230. /* one GPIO should control a D+ pullup, so host sees this device (or not) */
  231. static void pullup_off(void)
  232. {
  233. struct pxa2xx_udc_mach_info *mach = the_controller->mach;
  234. int off_level = mach->gpio_pullup_inverted;
  235. if (gpio_is_valid(mach->gpio_pullup))
  236. gpio_set_value(mach->gpio_pullup, off_level);
  237. else if (mach->udc_command)
  238. mach->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
  239. }
  240. static void pullup_on(void)
  241. {
  242. struct pxa2xx_udc_mach_info *mach = the_controller->mach;
  243. int on_level = !mach->gpio_pullup_inverted;
  244. if (gpio_is_valid(mach->gpio_pullup))
  245. gpio_set_value(mach->gpio_pullup, on_level);
  246. else if (mach->udc_command)
  247. mach->udc_command(PXA2XX_UDC_CMD_CONNECT);
  248. }
  249. #if defined(CONFIG_CPU_BIG_ENDIAN)
  250. /*
  251. * IXP4xx has its buses wired up in a way that relies on never doing any
  252. * byte swaps, independent of whether it runs in big-endian or little-endian
  253. * mode, as explained by Krzysztof Hałasa.
  254. *
  255. * We only support pxa25x in little-endian mode, but it is very likely
  256. * that it works the same way.
  257. */
  258. static inline void udc_set_reg(struct pxa25x_udc *dev, u32 reg, u32 val)
  259. {
  260. iowrite32be(val, dev->regs + reg);
  261. }
  262. static inline u32 udc_get_reg(struct pxa25x_udc *dev, u32 reg)
  263. {
  264. return ioread32be(dev->regs + reg);
  265. }
  266. #else
  267. static inline void udc_set_reg(struct pxa25x_udc *dev, u32 reg, u32 val)
  268. {
  269. writel(val, dev->regs + reg);
  270. }
  271. static inline u32 udc_get_reg(struct pxa25x_udc *dev, u32 reg)
  272. {
  273. return readl(dev->regs + reg);
  274. }
  275. #endif
  276. static void pio_irq_enable(struct pxa25x_ep *ep)
  277. {
  278. u32 bEndpointAddress = ep->bEndpointAddress & 0xf;
  279. if (bEndpointAddress < 8)
  280. udc_set_reg(ep->dev, UICR0, udc_get_reg(ep->dev, UICR0) &
  281. ~(1 << bEndpointAddress));
  282. else {
  283. bEndpointAddress -= 8;
  284. udc_set_reg(ep->dev, UICR1, udc_get_reg(ep->dev, UICR1) &
  285. ~(1 << bEndpointAddress));
  286. }
  287. }
  288. static void pio_irq_disable(struct pxa25x_ep *ep)
  289. {
  290. u32 bEndpointAddress = ep->bEndpointAddress & 0xf;
  291. if (bEndpointAddress < 8)
  292. udc_set_reg(ep->dev, UICR0, udc_get_reg(ep->dev, UICR0) |
  293. (1 << bEndpointAddress));
  294. else {
  295. bEndpointAddress -= 8;
  296. udc_set_reg(ep->dev, UICR1, udc_get_reg(ep->dev, UICR1) |
  297. (1 << bEndpointAddress));
  298. }
  299. }
  300. /* The UDCCR reg contains mask and interrupt status bits,
  301. * so using '|=' isn't safe as it may ack an interrupt.
  302. */
  303. #define UDCCR_MASK_BITS (UDCCR_REM | UDCCR_SRM | UDCCR_UDE)
  304. static inline void udc_set_mask_UDCCR(struct pxa25x_udc *dev, int mask)
  305. {
  306. u32 udccr = udc_get_reg(dev, UDCCR);
  307. udc_set_reg(dev, (udccr & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS), UDCCR);
  308. }
  309. static inline void udc_clear_mask_UDCCR(struct pxa25x_udc *dev, int mask)
  310. {
  311. u32 udccr = udc_get_reg(dev, UDCCR);
  312. udc_set_reg(dev, (udccr & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS), UDCCR);
  313. }
  314. static inline void udc_ack_int_UDCCR(struct pxa25x_udc *dev, int mask)
  315. {
  316. /* udccr contains the bits we dont want to change */
  317. u32 udccr = udc_get_reg(dev, UDCCR) & UDCCR_MASK_BITS;
  318. udc_set_reg(dev, udccr | (mask & ~UDCCR_MASK_BITS), UDCCR);
  319. }
  320. static inline u32 udc_ep_get_UDCCS(struct pxa25x_ep *ep)
  321. {
  322. return udc_get_reg(ep->dev, ep->regoff_udccs);
  323. }
  324. static inline void udc_ep_set_UDCCS(struct pxa25x_ep *ep, u32 data)
  325. {
  326. udc_set_reg(ep->dev, data, ep->regoff_udccs);
  327. }
  328. static inline u32 udc_ep0_get_UDCCS(struct pxa25x_udc *dev)
  329. {
  330. return udc_get_reg(dev, UDCCS0);
  331. }
  332. static inline void udc_ep0_set_UDCCS(struct pxa25x_udc *dev, u32 data)
  333. {
  334. udc_set_reg(dev, data, UDCCS0);
  335. }
  336. static inline u32 udc_ep_get_UDDR(struct pxa25x_ep *ep)
  337. {
  338. return udc_get_reg(ep->dev, ep->regoff_uddr);
  339. }
  340. static inline void udc_ep_set_UDDR(struct pxa25x_ep *ep, u32 data)
  341. {
  342. udc_set_reg(ep->dev, data, ep->regoff_uddr);
  343. }
  344. static inline u32 udc_ep_get_UBCR(struct pxa25x_ep *ep)
  345. {
  346. return udc_get_reg(ep->dev, ep->regoff_ubcr);
  347. }
  348. /*
  349. * endpoint enable/disable
  350. *
  351. * we need to verify the descriptors used to enable endpoints. since pxa25x
  352. * endpoint configurations are fixed, and are pretty much always enabled,
  353. * there's not a lot to manage here.
  354. *
  355. * because pxa25x can't selectively initialize bulk (or interrupt) endpoints,
  356. * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
  357. * for a single interface (with only the default altsetting) and for gadget
  358. * drivers that don't halt endpoints (not reset by set_interface). that also
  359. * means that if you use ISO, you must violate the USB spec rule that all
  360. * iso endpoints must be in non-default altsettings.
  361. */
  362. static int pxa25x_ep_enable (struct usb_ep *_ep,
  363. const struct usb_endpoint_descriptor *desc)
  364. {
  365. struct pxa25x_ep *ep;
  366. struct pxa25x_udc *dev;
  367. ep = container_of (_ep, struct pxa25x_ep, ep);
  368. if (!_ep || !desc || _ep->name == ep0name
  369. || desc->bDescriptorType != USB_DT_ENDPOINT
  370. || ep->bEndpointAddress != desc->bEndpointAddress
  371. || ep->fifo_size < usb_endpoint_maxp (desc)) {
  372. DMSG("%s, bad ep or descriptor\n", __func__);
  373. return -EINVAL;
  374. }
  375. /* xfer types must match, except that interrupt ~= bulk */
  376. if (ep->bmAttributes != desc->bmAttributes
  377. && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
  378. && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
  379. DMSG("%s, %s type mismatch\n", __func__, _ep->name);
  380. return -EINVAL;
  381. }
  382. /* hardware _could_ do smaller, but driver doesn't */
  383. if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
  384. && usb_endpoint_maxp (desc)
  385. != BULK_FIFO_SIZE)
  386. || !desc->wMaxPacketSize) {
  387. DMSG("%s, bad %s maxpacket\n", __func__, _ep->name);
  388. return -ERANGE;
  389. }
  390. dev = ep->dev;
  391. if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
  392. DMSG("%s, bogus device state\n", __func__);
  393. return -ESHUTDOWN;
  394. }
  395. ep->ep.desc = desc;
  396. ep->stopped = 0;
  397. ep->pio_irqs = 0;
  398. ep->ep.maxpacket = usb_endpoint_maxp (desc);
  399. /* flush fifo (mostly for OUT buffers) */
  400. pxa25x_ep_fifo_flush (_ep);
  401. /* ... reset halt state too, if we could ... */
  402. DBG(DBG_VERBOSE, "enabled %s\n", _ep->name);
  403. return 0;
  404. }
  405. static int pxa25x_ep_disable (struct usb_ep *_ep)
  406. {
  407. struct pxa25x_ep *ep;
  408. unsigned long flags;
  409. ep = container_of (_ep, struct pxa25x_ep, ep);
  410. if (!_ep || !ep->ep.desc) {
  411. DMSG("%s, %s not enabled\n", __func__,
  412. _ep ? ep->ep.name : NULL);
  413. return -EINVAL;
  414. }
  415. local_irq_save(flags);
  416. nuke (ep, -ESHUTDOWN);
  417. /* flush fifo (mostly for IN buffers) */
  418. pxa25x_ep_fifo_flush (_ep);
  419. ep->ep.desc = NULL;
  420. ep->stopped = 1;
  421. local_irq_restore(flags);
  422. DBG(DBG_VERBOSE, "%s disabled\n", _ep->name);
  423. return 0;
  424. }
  425. /*-------------------------------------------------------------------------*/
  426. /* for the pxa25x, these can just wrap kmalloc/kfree. gadget drivers
  427. * must still pass correctly initialized endpoints, since other controller
  428. * drivers may care about how it's currently set up (dma issues etc).
  429. */
  430. /*
  431. * pxa25x_ep_alloc_request - allocate a request data structure
  432. */
  433. static struct usb_request *
  434. pxa25x_ep_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
  435. {
  436. struct pxa25x_request *req;
  437. req = kzalloc(sizeof(*req), gfp_flags);
  438. if (!req)
  439. return NULL;
  440. INIT_LIST_HEAD (&req->queue);
  441. return &req->req;
  442. }
  443. /*
  444. * pxa25x_ep_free_request - deallocate a request data structure
  445. */
  446. static void
  447. pxa25x_ep_free_request (struct usb_ep *_ep, struct usb_request *_req)
  448. {
  449. struct pxa25x_request *req;
  450. req = container_of (_req, struct pxa25x_request, req);
  451. WARN_ON(!list_empty (&req->queue));
  452. kfree(req);
  453. }
  454. /*-------------------------------------------------------------------------*/
  455. /*
  456. * done - retire a request; caller blocked irqs
  457. */
  458. static void done(struct pxa25x_ep *ep, struct pxa25x_request *req, int status)
  459. {
  460. unsigned stopped = ep->stopped;
  461. list_del_init(&req->queue);
  462. if (likely (req->req.status == -EINPROGRESS))
  463. req->req.status = status;
  464. else
  465. status = req->req.status;
  466. if (status && status != -ESHUTDOWN)
  467. DBG(DBG_VERBOSE, "complete %s req %p stat %d len %u/%u\n",
  468. ep->ep.name, &req->req, status,
  469. req->req.actual, req->req.length);
  470. /* don't modify queue heads during completion callback */
  471. ep->stopped = 1;
  472. usb_gadget_giveback_request(&ep->ep, &req->req);
  473. ep->stopped = stopped;
  474. }
  475. static inline void ep0_idle (struct pxa25x_udc *dev)
  476. {
  477. dev->ep0state = EP0_IDLE;
  478. }
  479. static int
  480. write_packet(struct pxa25x_ep *ep, struct pxa25x_request *req, unsigned max)
  481. {
  482. u8 *buf;
  483. unsigned length, count;
  484. buf = req->req.buf + req->req.actual;
  485. prefetch(buf);
  486. /* how big will this packet be? */
  487. length = min(req->req.length - req->req.actual, max);
  488. req->req.actual += length;
  489. count = length;
  490. while (likely(count--))
  491. udc_ep_set_UDDR(ep, *buf++);
  492. return length;
  493. }
  494. /*
  495. * write to an IN endpoint fifo, as many packets as possible.
  496. * irqs will use this to write the rest later.
  497. * caller guarantees at least one packet buffer is ready (or a zlp).
  498. */
  499. static int
  500. write_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
  501. {
  502. unsigned max;
  503. max = usb_endpoint_maxp(ep->ep.desc);
  504. do {
  505. unsigned count;
  506. int is_last, is_short;
  507. count = write_packet(ep, req, max);
  508. /* last packet is usually short (or a zlp) */
  509. if (unlikely (count != max))
  510. is_last = is_short = 1;
  511. else {
  512. if (likely(req->req.length != req->req.actual)
  513. || req->req.zero)
  514. is_last = 0;
  515. else
  516. is_last = 1;
  517. /* interrupt/iso maxpacket may not fill the fifo */
  518. is_short = unlikely (max < ep->fifo_size);
  519. }
  520. DBG(DBG_VERY_NOISY, "wrote %s %d bytes%s%s %d left %p\n",
  521. ep->ep.name, count,
  522. is_last ? "/L" : "", is_short ? "/S" : "",
  523. req->req.length - req->req.actual, req);
  524. /* let loose that packet. maybe try writing another one,
  525. * double buffering might work. TSP, TPC, and TFS
  526. * bit values are the same for all normal IN endpoints.
  527. */
  528. udc_ep_set_UDCCS(ep, UDCCS_BI_TPC);
  529. if (is_short)
  530. udc_ep_set_UDCCS(ep, UDCCS_BI_TSP);
  531. /* requests complete when all IN data is in the FIFO */
  532. if (is_last) {
  533. done (ep, req, 0);
  534. if (list_empty(&ep->queue))
  535. pio_irq_disable(ep);
  536. return 1;
  537. }
  538. // TODO experiment: how robust can fifo mode tweaking be?
  539. // double buffering is off in the default fifo mode, which
  540. // prevents TFS from being set here.
  541. } while (udc_ep_get_UDCCS(ep) & UDCCS_BI_TFS);
  542. return 0;
  543. }
  544. /* caller asserts req->pending (ep0 irq status nyet cleared); starts
  545. * ep0 data stage. these chips want very simple state transitions.
  546. */
  547. static inline
  548. void ep0start(struct pxa25x_udc *dev, u32 flags, const char *tag)
  549. {
  550. udc_ep0_set_UDCCS(dev, flags|UDCCS0_SA|UDCCS0_OPR);
  551. udc_set_reg(dev, USIR0, USIR0_IR0);
  552. dev->req_pending = 0;
  553. DBG(DBG_VERY_NOISY, "%s %s, %02x/%02x\n",
  554. __func__, tag, udc_ep0_get_UDCCS(dev), flags);
  555. }
  556. static int
  557. write_ep0_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
  558. {
  559. struct pxa25x_udc *dev = ep->dev;
  560. unsigned count;
  561. int is_short;
  562. count = write_packet(&dev->ep[0], req, EP0_FIFO_SIZE);
  563. ep->dev->stats.write.bytes += count;
  564. /* last packet "must be" short (or a zlp) */
  565. is_short = (count != EP0_FIFO_SIZE);
  566. DBG(DBG_VERY_NOISY, "ep0in %d bytes %d left %p\n", count,
  567. req->req.length - req->req.actual, req);
  568. if (unlikely (is_short)) {
  569. if (ep->dev->req_pending)
  570. ep0start(ep->dev, UDCCS0_IPR, "short IN");
  571. else
  572. udc_ep0_set_UDCCS(dev, UDCCS0_IPR);
  573. count = req->req.length;
  574. done (ep, req, 0);
  575. ep0_idle(ep->dev);
  576. #ifndef CONFIG_ARCH_IXP4XX
  577. #if 1
  578. /* This seems to get rid of lost status irqs in some cases:
  579. * host responds quickly, or next request involves config
  580. * change automagic, or should have been hidden, or ...
  581. *
  582. * FIXME get rid of all udelays possible...
  583. */
  584. if (count >= EP0_FIFO_SIZE) {
  585. count = 100;
  586. do {
  587. if ((udc_ep0_get_UDCCS(dev) & UDCCS0_OPR) != 0) {
  588. /* clear OPR, generate ack */
  589. udc_ep0_set_UDCCS(dev, UDCCS0_OPR);
  590. break;
  591. }
  592. count--;
  593. udelay(1);
  594. } while (count);
  595. }
  596. #endif
  597. #endif
  598. } else if (ep->dev->req_pending)
  599. ep0start(ep->dev, 0, "IN");
  600. return is_short;
  601. }
  602. /*
  603. * read_fifo - unload packet(s) from the fifo we use for usb OUT
  604. * transfers and put them into the request. caller should have made
  605. * sure there's at least one packet ready.
  606. *
  607. * returns true if the request completed because of short packet or the
  608. * request buffer having filled (and maybe overran till end-of-packet).
  609. */
  610. static int
  611. read_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
  612. {
  613. for (;;) {
  614. u32 udccs;
  615. u8 *buf;
  616. unsigned bufferspace, count, is_short;
  617. /* make sure there's a packet in the FIFO.
  618. * UDCCS_{BO,IO}_RPC are all the same bit value.
  619. * UDCCS_{BO,IO}_RNE are all the same bit value.
  620. */
  621. udccs = udc_ep_get_UDCCS(ep);
  622. if (unlikely ((udccs & UDCCS_BO_RPC) == 0))
  623. break;
  624. buf = req->req.buf + req->req.actual;
  625. prefetchw(buf);
  626. bufferspace = req->req.length - req->req.actual;
  627. /* read all bytes from this packet */
  628. if (likely (udccs & UDCCS_BO_RNE)) {
  629. count = 1 + (0x0ff & udc_ep_get_UBCR(ep));
  630. req->req.actual += min (count, bufferspace);
  631. } else /* zlp */
  632. count = 0;
  633. is_short = (count < ep->ep.maxpacket);
  634. DBG(DBG_VERY_NOISY, "read %s %02x, %d bytes%s req %p %d/%d\n",
  635. ep->ep.name, udccs, count,
  636. is_short ? "/S" : "",
  637. req, req->req.actual, req->req.length);
  638. while (likely (count-- != 0)) {
  639. u8 byte = (u8) udc_ep_get_UDDR(ep);
  640. if (unlikely (bufferspace == 0)) {
  641. /* this happens when the driver's buffer
  642. * is smaller than what the host sent.
  643. * discard the extra data.
  644. */
  645. if (req->req.status != -EOVERFLOW)
  646. DMSG("%s overflow %d\n",
  647. ep->ep.name, count);
  648. req->req.status = -EOVERFLOW;
  649. } else {
  650. *buf++ = byte;
  651. bufferspace--;
  652. }
  653. }
  654. udc_ep_set_UDCCS(ep, UDCCS_BO_RPC);
  655. /* RPC/RSP/RNE could now reflect the other packet buffer */
  656. /* iso is one request per packet */
  657. if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
  658. if (udccs & UDCCS_IO_ROF)
  659. req->req.status = -EHOSTUNREACH;
  660. /* more like "is_done" */
  661. is_short = 1;
  662. }
  663. /* completion */
  664. if (is_short || req->req.actual == req->req.length) {
  665. done (ep, req, 0);
  666. if (list_empty(&ep->queue))
  667. pio_irq_disable(ep);
  668. return 1;
  669. }
  670. /* finished that packet. the next one may be waiting... */
  671. }
  672. return 0;
  673. }
  674. /*
  675. * special ep0 version of the above. no UBCR0 or double buffering; status
  676. * handshaking is magic. most device protocols don't need control-OUT.
  677. * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
  678. * protocols do use them.
  679. */
  680. static int
  681. read_ep0_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
  682. {
  683. u8 *buf, byte;
  684. unsigned bufferspace;
  685. buf = req->req.buf + req->req.actual;
  686. bufferspace = req->req.length - req->req.actual;
  687. while (udc_ep_get_UDCCS(ep) & UDCCS0_RNE) {
  688. byte = (u8) UDDR0;
  689. if (unlikely (bufferspace == 0)) {
  690. /* this happens when the driver's buffer
  691. * is smaller than what the host sent.
  692. * discard the extra data.
  693. */
  694. if (req->req.status != -EOVERFLOW)
  695. DMSG("%s overflow\n", ep->ep.name);
  696. req->req.status = -EOVERFLOW;
  697. } else {
  698. *buf++ = byte;
  699. req->req.actual++;
  700. bufferspace--;
  701. }
  702. }
  703. udc_ep_set_UDCCS(ep, UDCCS0_OPR | UDCCS0_IPR);
  704. /* completion */
  705. if (req->req.actual >= req->req.length)
  706. return 1;
  707. /* finished that packet. the next one may be waiting... */
  708. return 0;
  709. }
  710. /*-------------------------------------------------------------------------*/
  711. static int
  712. pxa25x_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
  713. {
  714. struct pxa25x_request *req;
  715. struct pxa25x_ep *ep;
  716. struct pxa25x_udc *dev;
  717. unsigned long flags;
  718. req = container_of(_req, struct pxa25x_request, req);
  719. if (unlikely (!_req || !_req->complete || !_req->buf
  720. || !list_empty(&req->queue))) {
  721. DMSG("%s, bad params\n", __func__);
  722. return -EINVAL;
  723. }
  724. ep = container_of(_ep, struct pxa25x_ep, ep);
  725. if (unlikely(!_ep || (!ep->ep.desc && ep->ep.name != ep0name))) {
  726. DMSG("%s, bad ep\n", __func__);
  727. return -EINVAL;
  728. }
  729. dev = ep->dev;
  730. if (unlikely (!dev->driver
  731. || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
  732. DMSG("%s, bogus device state\n", __func__);
  733. return -ESHUTDOWN;
  734. }
  735. /* iso is always one packet per request, that's the only way
  736. * we can report per-packet status. that also helps with dma.
  737. */
  738. if (unlikely (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
  739. && req->req.length > usb_endpoint_maxp(ep->ep.desc)))
  740. return -EMSGSIZE;
  741. DBG(DBG_NOISY, "%s queue req %p, len %d buf %p\n",
  742. _ep->name, _req, _req->length, _req->buf);
  743. local_irq_save(flags);
  744. _req->status = -EINPROGRESS;
  745. _req->actual = 0;
  746. /* kickstart this i/o queue? */
  747. if (list_empty(&ep->queue) && !ep->stopped) {
  748. if (ep->ep.desc == NULL/* ep0 */) {
  749. unsigned length = _req->length;
  750. switch (dev->ep0state) {
  751. case EP0_IN_DATA_PHASE:
  752. dev->stats.write.ops++;
  753. if (write_ep0_fifo(ep, req))
  754. req = NULL;
  755. break;
  756. case EP0_OUT_DATA_PHASE:
  757. dev->stats.read.ops++;
  758. /* messy ... */
  759. if (dev->req_config) {
  760. DBG(DBG_VERBOSE, "ep0 config ack%s\n",
  761. dev->has_cfr ? "" : " raced");
  762. if (dev->has_cfr)
  763. udc_set_reg(dev, UDCCFR, UDCCFR_AREN |
  764. UDCCFR_ACM | UDCCFR_MB1);
  765. done(ep, req, 0);
  766. dev->ep0state = EP0_END_XFER;
  767. local_irq_restore (flags);
  768. return 0;
  769. }
  770. if (dev->req_pending)
  771. ep0start(dev, UDCCS0_IPR, "OUT");
  772. if (length == 0 || ((udc_ep0_get_UDCCS(dev) & UDCCS0_RNE) != 0
  773. && read_ep0_fifo(ep, req))) {
  774. ep0_idle(dev);
  775. done(ep, req, 0);
  776. req = NULL;
  777. }
  778. break;
  779. default:
  780. DMSG("ep0 i/o, odd state %d\n", dev->ep0state);
  781. local_irq_restore (flags);
  782. return -EL2HLT;
  783. }
  784. /* can the FIFO can satisfy the request immediately? */
  785. } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
  786. if ((udc_ep_get_UDCCS(ep) & UDCCS_BI_TFS) != 0
  787. && write_fifo(ep, req))
  788. req = NULL;
  789. } else if ((udc_ep_get_UDCCS(ep) & UDCCS_BO_RFS) != 0
  790. && read_fifo(ep, req)) {
  791. req = NULL;
  792. }
  793. if (likely(req && ep->ep.desc))
  794. pio_irq_enable(ep);
  795. }
  796. /* pio or dma irq handler advances the queue. */
  797. if (likely(req != NULL))
  798. list_add_tail(&req->queue, &ep->queue);
  799. local_irq_restore(flags);
  800. return 0;
  801. }
  802. /*
  803. * nuke - dequeue ALL requests
  804. */
  805. static void nuke(struct pxa25x_ep *ep, int status)
  806. {
  807. struct pxa25x_request *req;
  808. /* called with irqs blocked */
  809. while (!list_empty(&ep->queue)) {
  810. req = list_entry(ep->queue.next,
  811. struct pxa25x_request,
  812. queue);
  813. done(ep, req, status);
  814. }
  815. if (ep->ep.desc)
  816. pio_irq_disable(ep);
  817. }
  818. /* dequeue JUST ONE request */
  819. static int pxa25x_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
  820. {
  821. struct pxa25x_ep *ep;
  822. struct pxa25x_request *req;
  823. unsigned long flags;
  824. ep = container_of(_ep, struct pxa25x_ep, ep);
  825. if (!_ep || ep->ep.name == ep0name)
  826. return -EINVAL;
  827. local_irq_save(flags);
  828. /* make sure it's actually queued on this endpoint */
  829. list_for_each_entry (req, &ep->queue, queue) {
  830. if (&req->req == _req)
  831. break;
  832. }
  833. if (&req->req != _req) {
  834. local_irq_restore(flags);
  835. return -EINVAL;
  836. }
  837. done(ep, req, -ECONNRESET);
  838. local_irq_restore(flags);
  839. return 0;
  840. }
  841. /*-------------------------------------------------------------------------*/
  842. static int pxa25x_ep_set_halt(struct usb_ep *_ep, int value)
  843. {
  844. struct pxa25x_ep *ep;
  845. unsigned long flags;
  846. ep = container_of(_ep, struct pxa25x_ep, ep);
  847. if (unlikely (!_ep
  848. || (!ep->ep.desc && ep->ep.name != ep0name))
  849. || ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
  850. DMSG("%s, bad ep\n", __func__);
  851. return -EINVAL;
  852. }
  853. if (value == 0) {
  854. /* this path (reset toggle+halt) is needed to implement
  855. * SET_INTERFACE on normal hardware. but it can't be
  856. * done from software on the PXA UDC, and the hardware
  857. * forgets to do it as part of SET_INTERFACE automagic.
  858. */
  859. DMSG("only host can clear %s halt\n", _ep->name);
  860. return -EROFS;
  861. }
  862. local_irq_save(flags);
  863. if ((ep->bEndpointAddress & USB_DIR_IN) != 0
  864. && ((udc_ep_get_UDCCS(ep) & UDCCS_BI_TFS) == 0
  865. || !list_empty(&ep->queue))) {
  866. local_irq_restore(flags);
  867. return -EAGAIN;
  868. }
  869. /* FST bit is the same for control, bulk in, bulk out, interrupt in */
  870. udc_ep_set_UDCCS(ep, UDCCS_BI_FST|UDCCS_BI_FTF);
  871. /* ep0 needs special care */
  872. if (!ep->ep.desc) {
  873. start_watchdog(ep->dev);
  874. ep->dev->req_pending = 0;
  875. ep->dev->ep0state = EP0_STALL;
  876. /* and bulk/intr endpoints like dropping stalls too */
  877. } else {
  878. unsigned i;
  879. for (i = 0; i < 1000; i += 20) {
  880. if (udc_ep_get_UDCCS(ep) & UDCCS_BI_SST)
  881. break;
  882. udelay(20);
  883. }
  884. }
  885. local_irq_restore(flags);
  886. DBG(DBG_VERBOSE, "%s halt\n", _ep->name);
  887. return 0;
  888. }
  889. static int pxa25x_ep_fifo_status(struct usb_ep *_ep)
  890. {
  891. struct pxa25x_ep *ep;
  892. ep = container_of(_ep, struct pxa25x_ep, ep);
  893. if (!_ep) {
  894. DMSG("%s, bad ep\n", __func__);
  895. return -ENODEV;
  896. }
  897. /* pxa can't report unclaimed bytes from IN fifos */
  898. if ((ep->bEndpointAddress & USB_DIR_IN) != 0)
  899. return -EOPNOTSUPP;
  900. if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN
  901. || (udc_ep_get_UDCCS(ep) & UDCCS_BO_RFS) == 0)
  902. return 0;
  903. else
  904. return (udc_ep_get_UBCR(ep) & 0xfff) + 1;
  905. }
  906. static void pxa25x_ep_fifo_flush(struct usb_ep *_ep)
  907. {
  908. struct pxa25x_ep *ep;
  909. ep = container_of(_ep, struct pxa25x_ep, ep);
  910. if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) {
  911. DMSG("%s, bad ep\n", __func__);
  912. return;
  913. }
  914. /* toggle and halt bits stay unchanged */
  915. /* for OUT, just read and discard the FIFO contents. */
  916. if ((ep->bEndpointAddress & USB_DIR_IN) == 0) {
  917. while (((udc_ep_get_UDCCS(ep)) & UDCCS_BO_RNE) != 0)
  918. (void)udc_ep_get_UDDR(ep);
  919. return;
  920. }
  921. /* most IN status is the same, but ISO can't stall */
  922. udc_ep_set_UDCCS(ep, UDCCS_BI_TPC|UDCCS_BI_FTF|UDCCS_BI_TUR
  923. | (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
  924. ? 0 : UDCCS_BI_SST));
  925. }
  926. static struct usb_ep_ops pxa25x_ep_ops = {
  927. .enable = pxa25x_ep_enable,
  928. .disable = pxa25x_ep_disable,
  929. .alloc_request = pxa25x_ep_alloc_request,
  930. .free_request = pxa25x_ep_free_request,
  931. .queue = pxa25x_ep_queue,
  932. .dequeue = pxa25x_ep_dequeue,
  933. .set_halt = pxa25x_ep_set_halt,
  934. .fifo_status = pxa25x_ep_fifo_status,
  935. .fifo_flush = pxa25x_ep_fifo_flush,
  936. };
  937. /* ---------------------------------------------------------------------------
  938. * device-scoped parts of the api to the usb controller hardware
  939. * ---------------------------------------------------------------------------
  940. */
  941. static int pxa25x_udc_get_frame(struct usb_gadget *_gadget)
  942. {
  943. struct pxa25x_udc *dev;
  944. dev = container_of(_gadget, struct pxa25x_udc, gadget);
  945. return ((udc_get_reg(dev, UFNRH) & 0x07) << 8) |
  946. (udc_get_reg(dev, UFNRL) & 0xff);
  947. }
  948. static int pxa25x_udc_wakeup(struct usb_gadget *_gadget)
  949. {
  950. struct pxa25x_udc *udc;
  951. udc = container_of(_gadget, struct pxa25x_udc, gadget);
  952. /* host may not have enabled remote wakeup */
  953. if ((udc_ep0_get_UDCCS(udc) & UDCCS0_DRWF) == 0)
  954. return -EHOSTUNREACH;
  955. udc_set_mask_UDCCR(udc, UDCCR_RSM);
  956. return 0;
  957. }
  958. static void stop_activity(struct pxa25x_udc *, struct usb_gadget_driver *);
  959. static void udc_enable (struct pxa25x_udc *);
  960. static void udc_disable(struct pxa25x_udc *);
  961. /* We disable the UDC -- and its 48 MHz clock -- whenever it's not
  962. * in active use.
  963. */
  964. static int pullup(struct pxa25x_udc *udc)
  965. {
  966. int is_active = udc->vbus && udc->pullup && !udc->suspended;
  967. DMSG("%s\n", is_active ? "active" : "inactive");
  968. if (is_active) {
  969. if (!udc->active) {
  970. udc->active = 1;
  971. /* Enable clock for USB device */
  972. clk_enable(udc->clk);
  973. udc_enable(udc);
  974. }
  975. } else {
  976. if (udc->active) {
  977. if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
  978. DMSG("disconnect %s\n", udc->driver
  979. ? udc->driver->driver.name
  980. : "(no driver)");
  981. stop_activity(udc, udc->driver);
  982. }
  983. udc_disable(udc);
  984. /* Disable clock for USB device */
  985. clk_disable(udc->clk);
  986. udc->active = 0;
  987. }
  988. }
  989. return 0;
  990. }
  991. /* VBUS reporting logically comes from a transceiver */
  992. static int pxa25x_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
  993. {
  994. struct pxa25x_udc *udc;
  995. udc = container_of(_gadget, struct pxa25x_udc, gadget);
  996. udc->vbus = is_active;
  997. DMSG("vbus %s\n", is_active ? "supplied" : "inactive");
  998. pullup(udc);
  999. return 0;
  1000. }
  1001. /* drivers may have software control over D+ pullup */
  1002. static int pxa25x_udc_pullup(struct usb_gadget *_gadget, int is_active)
  1003. {
  1004. struct pxa25x_udc *udc;
  1005. udc = container_of(_gadget, struct pxa25x_udc, gadget);
  1006. /* not all boards support pullup control */
  1007. if (!gpio_is_valid(udc->mach->gpio_pullup) && !udc->mach->udc_command)
  1008. return -EOPNOTSUPP;
  1009. udc->pullup = (is_active != 0);
  1010. pullup(udc);
  1011. return 0;
  1012. }
  1013. /* boards may consume current from VBUS, up to 100-500mA based on config.
  1014. * the 500uA suspend ceiling means that exclusively vbus-powered PXA designs
  1015. * violate USB specs.
  1016. */
  1017. static int pxa25x_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
  1018. {
  1019. struct pxa25x_udc *udc;
  1020. udc = container_of(_gadget, struct pxa25x_udc, gadget);
  1021. if (!IS_ERR_OR_NULL(udc->transceiver))
  1022. return usb_phy_set_power(udc->transceiver, mA);
  1023. return -EOPNOTSUPP;
  1024. }
  1025. static int pxa25x_udc_start(struct usb_gadget *g,
  1026. struct usb_gadget_driver *driver);
  1027. static int pxa25x_udc_stop(struct usb_gadget *g);
  1028. static const struct usb_gadget_ops pxa25x_udc_ops = {
  1029. .get_frame = pxa25x_udc_get_frame,
  1030. .wakeup = pxa25x_udc_wakeup,
  1031. .vbus_session = pxa25x_udc_vbus_session,
  1032. .pullup = pxa25x_udc_pullup,
  1033. .vbus_draw = pxa25x_udc_vbus_draw,
  1034. .udc_start = pxa25x_udc_start,
  1035. .udc_stop = pxa25x_udc_stop,
  1036. };
  1037. /*-------------------------------------------------------------------------*/
  1038. #ifdef CONFIG_USB_GADGET_DEBUG_FS
  1039. static int udc_debug_show(struct seq_file *m, void *_d)
  1040. {
  1041. struct pxa25x_udc *dev = m->private;
  1042. unsigned long flags;
  1043. int i;
  1044. u32 tmp;
  1045. local_irq_save(flags);
  1046. /* basic device status */
  1047. seq_printf(m, DRIVER_DESC "\n"
  1048. "%s version: %s\nGadget driver: %s\nHost %s\n\n",
  1049. driver_name, DRIVER_VERSION SIZE_STR "(pio)",
  1050. dev->driver ? dev->driver->driver.name : "(none)",
  1051. dev->gadget.speed == USB_SPEED_FULL ? "full speed" : "disconnected");
  1052. /* registers for device and ep0 */
  1053. seq_printf(m,
  1054. "uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
  1055. udc_get_reg(dev, UICR1), udc_get_reg(dev, UICR0),
  1056. udc_get_reg(dev, USIR1), udc_get_reg(dev, USIR0),
  1057. udc_get_reg(dev, UFNRH), udc_get_reg(dev, UFNRL));
  1058. tmp = udc_get_reg(dev, UDCCR);
  1059. seq_printf(m,
  1060. "udccr %02X =%s%s%s%s%s%s%s%s\n", tmp,
  1061. (tmp & UDCCR_REM) ? " rem" : "",
  1062. (tmp & UDCCR_RSTIR) ? " rstir" : "",
  1063. (tmp & UDCCR_SRM) ? " srm" : "",
  1064. (tmp & UDCCR_SUSIR) ? " susir" : "",
  1065. (tmp & UDCCR_RESIR) ? " resir" : "",
  1066. (tmp & UDCCR_RSM) ? " rsm" : "",
  1067. (tmp & UDCCR_UDA) ? " uda" : "",
  1068. (tmp & UDCCR_UDE) ? " ude" : "");
  1069. tmp = udc_ep0_get_UDCCS(dev);
  1070. seq_printf(m,
  1071. "udccs0 %02X =%s%s%s%s%s%s%s%s\n", tmp,
  1072. (tmp & UDCCS0_SA) ? " sa" : "",
  1073. (tmp & UDCCS0_RNE) ? " rne" : "",
  1074. (tmp & UDCCS0_FST) ? " fst" : "",
  1075. (tmp & UDCCS0_SST) ? " sst" : "",
  1076. (tmp & UDCCS0_DRWF) ? " dwrf" : "",
  1077. (tmp & UDCCS0_FTF) ? " ftf" : "",
  1078. (tmp & UDCCS0_IPR) ? " ipr" : "",
  1079. (tmp & UDCCS0_OPR) ? " opr" : "");
  1080. if (dev->has_cfr) {
  1081. tmp = udc_get_reg(dev, UDCCFR);
  1082. seq_printf(m,
  1083. "udccfr %02X =%s%s\n", tmp,
  1084. (tmp & UDCCFR_AREN) ? " aren" : "",
  1085. (tmp & UDCCFR_ACM) ? " acm" : "");
  1086. }
  1087. if (dev->gadget.speed != USB_SPEED_FULL || !dev->driver)
  1088. goto done;
  1089. seq_printf(m, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n",
  1090. dev->stats.write.bytes, dev->stats.write.ops,
  1091. dev->stats.read.bytes, dev->stats.read.ops,
  1092. dev->stats.irqs);
  1093. /* dump endpoint queues */
  1094. for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
  1095. struct pxa25x_ep *ep = &dev->ep [i];
  1096. struct pxa25x_request *req;
  1097. if (i != 0) {
  1098. const struct usb_endpoint_descriptor *desc;
  1099. desc = ep->ep.desc;
  1100. if (!desc)
  1101. continue;
  1102. tmp = udc_ep_get_UDCCS(&dev->ep[i]);
  1103. seq_printf(m,
  1104. "%s max %d %s udccs %02x irqs %lu\n",
  1105. ep->ep.name, usb_endpoint_maxp(desc),
  1106. "pio", tmp, ep->pio_irqs);
  1107. /* TODO translate all five groups of udccs bits! */
  1108. } else /* ep0 should only have one transfer queued */
  1109. seq_printf(m, "ep0 max 16 pio irqs %lu\n",
  1110. ep->pio_irqs);
  1111. if (list_empty(&ep->queue)) {
  1112. seq_printf(m, "\t(nothing queued)\n");
  1113. continue;
  1114. }
  1115. list_for_each_entry(req, &ep->queue, queue) {
  1116. seq_printf(m,
  1117. "\treq %p len %d/%d buf %p\n",
  1118. &req->req, req->req.actual,
  1119. req->req.length, req->req.buf);
  1120. }
  1121. }
  1122. done:
  1123. local_irq_restore(flags);
  1124. return 0;
  1125. }
  1126. DEFINE_SHOW_ATTRIBUTE(udc_debug);
  1127. #define create_debug_files(dev) \
  1128. do { \
  1129. dev->debugfs_udc = debugfs_create_file(dev->gadget.name, \
  1130. S_IRUGO, NULL, dev, &udc_debug_fops); \
  1131. } while (0)
  1132. #define remove_debug_files(dev) debugfs_remove(dev->debugfs_udc)
  1133. #else /* !CONFIG_USB_GADGET_DEBUG_FILES */
  1134. #define create_debug_files(dev) do {} while (0)
  1135. #define remove_debug_files(dev) do {} while (0)
  1136. #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
  1137. /*-------------------------------------------------------------------------*/
  1138. /*
  1139. * udc_disable - disable USB device controller
  1140. */
  1141. static void udc_disable(struct pxa25x_udc *dev)
  1142. {
  1143. /* block all irqs */
  1144. udc_set_mask_UDCCR(dev, UDCCR_SRM|UDCCR_REM);
  1145. udc_set_reg(dev, UICR0, 0xff);
  1146. udc_set_reg(dev, UICR1, 0xff);
  1147. udc_set_reg(dev, UFNRH, UFNRH_SIM);
  1148. /* if hardware supports it, disconnect from usb */
  1149. pullup_off();
  1150. udc_clear_mask_UDCCR(dev, UDCCR_UDE);
  1151. ep0_idle (dev);
  1152. dev->gadget.speed = USB_SPEED_UNKNOWN;
  1153. }
  1154. /*
  1155. * udc_reinit - initialize software state
  1156. */
  1157. static void udc_reinit(struct pxa25x_udc *dev)
  1158. {
  1159. u32 i;
  1160. /* device/ep0 records init */
  1161. INIT_LIST_HEAD (&dev->gadget.ep_list);
  1162. INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
  1163. dev->ep0state = EP0_IDLE;
  1164. dev->gadget.quirk_altset_not_supp = 1;
  1165. /* basic endpoint records init */
  1166. for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
  1167. struct pxa25x_ep *ep = &dev->ep[i];
  1168. if (i != 0)
  1169. list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
  1170. ep->ep.desc = NULL;
  1171. ep->stopped = 0;
  1172. INIT_LIST_HEAD (&ep->queue);
  1173. ep->pio_irqs = 0;
  1174. usb_ep_set_maxpacket_limit(&ep->ep, ep->ep.maxpacket);
  1175. }
  1176. /* the rest was statically initialized, and is read-only */
  1177. }
  1178. /* until it's enabled, this UDC should be completely invisible
  1179. * to any USB host.
  1180. */
  1181. static void udc_enable (struct pxa25x_udc *dev)
  1182. {
  1183. udc_clear_mask_UDCCR(dev, UDCCR_UDE);
  1184. /* try to clear these bits before we enable the udc */
  1185. udc_ack_int_UDCCR(dev, UDCCR_SUSIR|/*UDCCR_RSTIR|*/UDCCR_RESIR);
  1186. ep0_idle(dev);
  1187. dev->gadget.speed = USB_SPEED_UNKNOWN;
  1188. dev->stats.irqs = 0;
  1189. /*
  1190. * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
  1191. * - enable UDC
  1192. * - if RESET is already in progress, ack interrupt
  1193. * - unmask reset interrupt
  1194. */
  1195. udc_set_mask_UDCCR(dev, UDCCR_UDE);
  1196. if (!(udc_get_reg(dev, UDCCR) & UDCCR_UDA))
  1197. udc_ack_int_UDCCR(dev, UDCCR_RSTIR);
  1198. if (dev->has_cfr /* UDC_RES2 is defined */) {
  1199. /* pxa255 (a0+) can avoid a set_config race that could
  1200. * prevent gadget drivers from configuring correctly
  1201. */
  1202. udc_set_reg(dev, UDCCFR, UDCCFR_ACM | UDCCFR_MB1);
  1203. } else {
  1204. /* "USB test mode" for pxa250 errata 40-42 (stepping a0, a1)
  1205. * which could result in missing packets and interrupts.
  1206. * supposedly one bit per endpoint, controlling whether it
  1207. * double buffers or not; ACM/AREN bits fit into the holes.
  1208. * zero bits (like USIR0_IRx) disable double buffering.
  1209. */
  1210. udc_set_reg(dev, UDC_RES1, 0x00);
  1211. udc_set_reg(dev, UDC_RES2, 0x00);
  1212. }
  1213. /* enable suspend/resume and reset irqs */
  1214. udc_clear_mask_UDCCR(dev, UDCCR_SRM | UDCCR_REM);
  1215. /* enable ep0 irqs */
  1216. udc_set_reg(dev, UICR0, udc_get_reg(dev, UICR0) & ~UICR0_IM0);
  1217. /* if hardware supports it, pullup D+ and wait for reset */
  1218. pullup_on();
  1219. }
  1220. /* when a driver is successfully registered, it will receive
  1221. * control requests including set_configuration(), which enables
  1222. * non-control requests. then usb traffic follows until a
  1223. * disconnect is reported. then a host may connect again, or
  1224. * the driver might get unbound.
  1225. */
  1226. static int pxa25x_udc_start(struct usb_gadget *g,
  1227. struct usb_gadget_driver *driver)
  1228. {
  1229. struct pxa25x_udc *dev = to_pxa25x(g);
  1230. int retval;
  1231. /* first hook up the driver ... */
  1232. dev->driver = driver;
  1233. dev->pullup = 1;
  1234. /* ... then enable host detection and ep0; and we're ready
  1235. * for set_configuration as well as eventual disconnect.
  1236. */
  1237. /* connect to bus through transceiver */
  1238. if (!IS_ERR_OR_NULL(dev->transceiver)) {
  1239. retval = otg_set_peripheral(dev->transceiver->otg,
  1240. &dev->gadget);
  1241. if (retval)
  1242. goto bind_fail;
  1243. }
  1244. dump_state(dev);
  1245. return 0;
  1246. bind_fail:
  1247. return retval;
  1248. }
  1249. static void
  1250. reset_gadget(struct pxa25x_udc *dev, struct usb_gadget_driver *driver)
  1251. {
  1252. int i;
  1253. /* don't disconnect drivers more than once */
  1254. if (dev->gadget.speed == USB_SPEED_UNKNOWN)
  1255. driver = NULL;
  1256. dev->gadget.speed = USB_SPEED_UNKNOWN;
  1257. /* prevent new request submissions, kill any outstanding requests */
  1258. for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
  1259. struct pxa25x_ep *ep = &dev->ep[i];
  1260. ep->stopped = 1;
  1261. nuke(ep, -ESHUTDOWN);
  1262. }
  1263. del_timer_sync(&dev->timer);
  1264. /* report reset; the driver is already quiesced */
  1265. if (driver)
  1266. usb_gadget_udc_reset(&dev->gadget, driver);
  1267. /* re-init driver-visible data structures */
  1268. udc_reinit(dev);
  1269. }
  1270. static void
  1271. stop_activity(struct pxa25x_udc *dev, struct usb_gadget_driver *driver)
  1272. {
  1273. int i;
  1274. /* don't disconnect drivers more than once */
  1275. if (dev->gadget.speed == USB_SPEED_UNKNOWN)
  1276. driver = NULL;
  1277. dev->gadget.speed = USB_SPEED_UNKNOWN;
  1278. /* prevent new request submissions, kill any outstanding requests */
  1279. for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
  1280. struct pxa25x_ep *ep = &dev->ep[i];
  1281. ep->stopped = 1;
  1282. nuke(ep, -ESHUTDOWN);
  1283. }
  1284. del_timer_sync(&dev->timer);
  1285. /* report disconnect; the driver is already quiesced */
  1286. if (driver)
  1287. driver->disconnect(&dev->gadget);
  1288. /* re-init driver-visible data structures */
  1289. udc_reinit(dev);
  1290. }
  1291. static int pxa25x_udc_stop(struct usb_gadget*g)
  1292. {
  1293. struct pxa25x_udc *dev = to_pxa25x(g);
  1294. local_irq_disable();
  1295. dev->pullup = 0;
  1296. stop_activity(dev, NULL);
  1297. local_irq_enable();
  1298. if (!IS_ERR_OR_NULL(dev->transceiver))
  1299. (void) otg_set_peripheral(dev->transceiver->otg, NULL);
  1300. dev->driver = NULL;
  1301. dump_state(dev);
  1302. return 0;
  1303. }
  1304. /*-------------------------------------------------------------------------*/
  1305. #ifdef CONFIG_ARCH_LUBBOCK
  1306. /* Lubbock has separate connect and disconnect irqs. More typical designs
  1307. * use one GPIO as the VBUS IRQ, and another to control the D+ pullup.
  1308. */
  1309. static irqreturn_t
  1310. lubbock_vbus_irq(int irq, void *_dev)
  1311. {
  1312. struct pxa25x_udc *dev = _dev;
  1313. int vbus;
  1314. dev->stats.irqs++;
  1315. switch (irq) {
  1316. case LUBBOCK_USB_IRQ:
  1317. vbus = 1;
  1318. disable_irq(LUBBOCK_USB_IRQ);
  1319. enable_irq(LUBBOCK_USB_DISC_IRQ);
  1320. break;
  1321. case LUBBOCK_USB_DISC_IRQ:
  1322. vbus = 0;
  1323. disable_irq(LUBBOCK_USB_DISC_IRQ);
  1324. enable_irq(LUBBOCK_USB_IRQ);
  1325. break;
  1326. default:
  1327. return IRQ_NONE;
  1328. }
  1329. pxa25x_udc_vbus_session(&dev->gadget, vbus);
  1330. return IRQ_HANDLED;
  1331. }
  1332. #endif
  1333. /*-------------------------------------------------------------------------*/
  1334. static inline void clear_ep_state (struct pxa25x_udc *dev)
  1335. {
  1336. unsigned i;
  1337. /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
  1338. * fifos, and pending transactions mustn't be continued in any case.
  1339. */
  1340. for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++)
  1341. nuke(&dev->ep[i], -ECONNABORTED);
  1342. }
  1343. static void udc_watchdog(struct timer_list *t)
  1344. {
  1345. struct pxa25x_udc *dev = from_timer(dev, t, timer);
  1346. local_irq_disable();
  1347. if (dev->ep0state == EP0_STALL
  1348. && (udc_ep0_get_UDCCS(dev) & UDCCS0_FST) == 0
  1349. && (udc_ep0_get_UDCCS(dev) & UDCCS0_SST) == 0) {
  1350. udc_ep0_set_UDCCS(dev, UDCCS0_FST|UDCCS0_FTF);
  1351. DBG(DBG_VERBOSE, "ep0 re-stall\n");
  1352. start_watchdog(dev);
  1353. }
  1354. local_irq_enable();
  1355. }
  1356. static void handle_ep0 (struct pxa25x_udc *dev)
  1357. {
  1358. u32 udccs0 = udc_ep0_get_UDCCS(dev);
  1359. struct pxa25x_ep *ep = &dev->ep [0];
  1360. struct pxa25x_request *req;
  1361. union {
  1362. struct usb_ctrlrequest r;
  1363. u8 raw [8];
  1364. u32 word [2];
  1365. } u;
  1366. if (list_empty(&ep->queue))
  1367. req = NULL;
  1368. else
  1369. req = list_entry(ep->queue.next, struct pxa25x_request, queue);
  1370. /* clear stall status */
  1371. if (udccs0 & UDCCS0_SST) {
  1372. nuke(ep, -EPIPE);
  1373. udc_ep0_set_UDCCS(dev, UDCCS0_SST);
  1374. del_timer(&dev->timer);
  1375. ep0_idle(dev);
  1376. }
  1377. /* previous request unfinished? non-error iff back-to-back ... */
  1378. if ((udccs0 & UDCCS0_SA) != 0 && dev->ep0state != EP0_IDLE) {
  1379. nuke(ep, 0);
  1380. del_timer(&dev->timer);
  1381. ep0_idle(dev);
  1382. }
  1383. switch (dev->ep0state) {
  1384. case EP0_IDLE:
  1385. /* late-breaking status? */
  1386. udccs0 = udc_ep0_get_UDCCS(dev);
  1387. /* start control request? */
  1388. if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))
  1389. == (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))) {
  1390. int i;
  1391. nuke (ep, -EPROTO);
  1392. /* read SETUP packet */
  1393. for (i = 0; i < 8; i++) {
  1394. if (unlikely(!(udc_ep0_get_UDCCS(dev) & UDCCS0_RNE))) {
  1395. bad_setup:
  1396. DMSG("SETUP %d!\n", i);
  1397. goto stall;
  1398. }
  1399. u.raw [i] = (u8) UDDR0;
  1400. }
  1401. if (unlikely((udc_ep0_get_UDCCS(dev) & UDCCS0_RNE) != 0))
  1402. goto bad_setup;
  1403. got_setup:
  1404. DBG(DBG_VERBOSE, "SETUP %02x.%02x v%04x i%04x l%04x\n",
  1405. u.r.bRequestType, u.r.bRequest,
  1406. le16_to_cpu(u.r.wValue),
  1407. le16_to_cpu(u.r.wIndex),
  1408. le16_to_cpu(u.r.wLength));
  1409. /* cope with automagic for some standard requests. */
  1410. dev->req_std = (u.r.bRequestType & USB_TYPE_MASK)
  1411. == USB_TYPE_STANDARD;
  1412. dev->req_config = 0;
  1413. dev->req_pending = 1;
  1414. switch (u.r.bRequest) {
  1415. /* hardware restricts gadget drivers here! */
  1416. case USB_REQ_SET_CONFIGURATION:
  1417. if (u.r.bRequestType == USB_RECIP_DEVICE) {
  1418. /* reflect hardware's automagic
  1419. * up to the gadget driver.
  1420. */
  1421. config_change:
  1422. dev->req_config = 1;
  1423. clear_ep_state(dev);
  1424. /* if !has_cfr, there's no synch
  1425. * else use AREN (later) not SA|OPR
  1426. * USIR0_IR0 acts edge sensitive
  1427. */
  1428. }
  1429. break;
  1430. /* ... and here, even more ... */
  1431. case USB_REQ_SET_INTERFACE:
  1432. if (u.r.bRequestType == USB_RECIP_INTERFACE) {
  1433. /* udc hardware is broken by design:
  1434. * - altsetting may only be zero;
  1435. * - hw resets all interfaces' eps;
  1436. * - ep reset doesn't include halt(?).
  1437. */
  1438. DMSG("broken set_interface (%d/%d)\n",
  1439. le16_to_cpu(u.r.wIndex),
  1440. le16_to_cpu(u.r.wValue));
  1441. goto config_change;
  1442. }
  1443. break;
  1444. /* hardware was supposed to hide this */
  1445. case USB_REQ_SET_ADDRESS:
  1446. if (u.r.bRequestType == USB_RECIP_DEVICE) {
  1447. ep0start(dev, 0, "address");
  1448. return;
  1449. }
  1450. break;
  1451. }
  1452. if (u.r.bRequestType & USB_DIR_IN)
  1453. dev->ep0state = EP0_IN_DATA_PHASE;
  1454. else
  1455. dev->ep0state = EP0_OUT_DATA_PHASE;
  1456. i = dev->driver->setup(&dev->gadget, &u.r);
  1457. if (i < 0) {
  1458. /* hardware automagic preventing STALL... */
  1459. if (dev->req_config) {
  1460. /* hardware sometimes neglects to tell
  1461. * tell us about config change events,
  1462. * so later ones may fail...
  1463. */
  1464. WARNING("config change %02x fail %d?\n",
  1465. u.r.bRequest, i);
  1466. return;
  1467. /* TODO experiment: if has_cfr,
  1468. * hardware didn't ACK; maybe we
  1469. * could actually STALL!
  1470. */
  1471. }
  1472. DBG(DBG_VERBOSE, "protocol STALL, "
  1473. "%02x err %d\n", udc_ep0_get_UDCCS(dev), i);
  1474. stall:
  1475. /* the watchdog timer helps deal with cases
  1476. * where udc seems to clear FST wrongly, and
  1477. * then NAKs instead of STALLing.
  1478. */
  1479. ep0start(dev, UDCCS0_FST|UDCCS0_FTF, "stall");
  1480. start_watchdog(dev);
  1481. dev->ep0state = EP0_STALL;
  1482. /* deferred i/o == no response yet */
  1483. } else if (dev->req_pending) {
  1484. if (likely(dev->ep0state == EP0_IN_DATA_PHASE
  1485. || dev->req_std || u.r.wLength))
  1486. ep0start(dev, 0, "defer");
  1487. else
  1488. ep0start(dev, UDCCS0_IPR, "defer/IPR");
  1489. }
  1490. /* expect at least one data or status stage irq */
  1491. return;
  1492. } else if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA))
  1493. == (UDCCS0_OPR|UDCCS0_SA))) {
  1494. unsigned i;
  1495. /* pxa210/250 erratum 131 for B0/B1 says RNE lies.
  1496. * still observed on a pxa255 a0.
  1497. */
  1498. DBG(DBG_VERBOSE, "e131\n");
  1499. nuke(ep, -EPROTO);
  1500. /* read SETUP data, but don't trust it too much */
  1501. for (i = 0; i < 8; i++)
  1502. u.raw [i] = (u8) UDDR0;
  1503. if ((u.r.bRequestType & USB_RECIP_MASK)
  1504. > USB_RECIP_OTHER)
  1505. goto stall;
  1506. if (u.word [0] == 0 && u.word [1] == 0)
  1507. goto stall;
  1508. goto got_setup;
  1509. } else {
  1510. /* some random early IRQ:
  1511. * - we acked FST
  1512. * - IPR cleared
  1513. * - OPR got set, without SA (likely status stage)
  1514. */
  1515. udc_ep0_set_UDCCS(dev, udccs0 & (UDCCS0_SA|UDCCS0_OPR));
  1516. }
  1517. break;
  1518. case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
  1519. if (udccs0 & UDCCS0_OPR) {
  1520. udc_ep0_set_UDCCS(dev, UDCCS0_OPR|UDCCS0_FTF);
  1521. DBG(DBG_VERBOSE, "ep0in premature status\n");
  1522. if (req)
  1523. done(ep, req, 0);
  1524. ep0_idle(dev);
  1525. } else /* irq was IPR clearing */ {
  1526. if (req) {
  1527. /* this IN packet might finish the request */
  1528. (void) write_ep0_fifo(ep, req);
  1529. } /* else IN token before response was written */
  1530. }
  1531. break;
  1532. case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
  1533. if (udccs0 & UDCCS0_OPR) {
  1534. if (req) {
  1535. /* this OUT packet might finish the request */
  1536. if (read_ep0_fifo(ep, req))
  1537. done(ep, req, 0);
  1538. /* else more OUT packets expected */
  1539. } /* else OUT token before read was issued */
  1540. } else /* irq was IPR clearing */ {
  1541. DBG(DBG_VERBOSE, "ep0out premature status\n");
  1542. if (req)
  1543. done(ep, req, 0);
  1544. ep0_idle(dev);
  1545. }
  1546. break;
  1547. case EP0_END_XFER:
  1548. if (req)
  1549. done(ep, req, 0);
  1550. /* ack control-IN status (maybe in-zlp was skipped)
  1551. * also appears after some config change events.
  1552. */
  1553. if (udccs0 & UDCCS0_OPR)
  1554. udc_ep0_set_UDCCS(dev, UDCCS0_OPR);
  1555. ep0_idle(dev);
  1556. break;
  1557. case EP0_STALL:
  1558. udc_ep0_set_UDCCS(dev, UDCCS0_FST);
  1559. break;
  1560. }
  1561. udc_set_reg(dev, USIR0, USIR0_IR0);
  1562. }
  1563. static void handle_ep(struct pxa25x_ep *ep)
  1564. {
  1565. struct pxa25x_request *req;
  1566. int is_in = ep->bEndpointAddress & USB_DIR_IN;
  1567. int completed;
  1568. u32 udccs, tmp;
  1569. do {
  1570. completed = 0;
  1571. if (likely (!list_empty(&ep->queue)))
  1572. req = list_entry(ep->queue.next,
  1573. struct pxa25x_request, queue);
  1574. else
  1575. req = NULL;
  1576. // TODO check FST handling
  1577. udccs = udc_ep_get_UDCCS(ep);
  1578. if (unlikely(is_in)) { /* irq from TPC, SST, or (ISO) TUR */
  1579. tmp = UDCCS_BI_TUR;
  1580. if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
  1581. tmp |= UDCCS_BI_SST;
  1582. tmp &= udccs;
  1583. if (likely (tmp))
  1584. udc_ep_set_UDCCS(ep, tmp);
  1585. if (req && likely ((udccs & UDCCS_BI_TFS) != 0))
  1586. completed = write_fifo(ep, req);
  1587. } else { /* irq from RPC (or for ISO, ROF) */
  1588. if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
  1589. tmp = UDCCS_BO_SST | UDCCS_BO_DME;
  1590. else
  1591. tmp = UDCCS_IO_ROF | UDCCS_IO_DME;
  1592. tmp &= udccs;
  1593. if (likely(tmp))
  1594. udc_ep_set_UDCCS(ep, tmp);
  1595. /* fifos can hold packets, ready for reading... */
  1596. if (likely(req)) {
  1597. completed = read_fifo(ep, req);
  1598. } else
  1599. pio_irq_disable(ep);
  1600. }
  1601. ep->pio_irqs++;
  1602. } while (completed);
  1603. }
  1604. /*
  1605. * pxa25x_udc_irq - interrupt handler
  1606. *
  1607. * avoid delays in ep0 processing. the control handshaking isn't always
  1608. * under software control (pxa250c0 and the pxa255 are better), and delays
  1609. * could cause usb protocol errors.
  1610. */
  1611. static irqreturn_t
  1612. pxa25x_udc_irq(int irq, void *_dev)
  1613. {
  1614. struct pxa25x_udc *dev = _dev;
  1615. int handled;
  1616. dev->stats.irqs++;
  1617. do {
  1618. u32 udccr = udc_get_reg(dev, UDCCR);
  1619. handled = 0;
  1620. /* SUSpend Interrupt Request */
  1621. if (unlikely(udccr & UDCCR_SUSIR)) {
  1622. udc_ack_int_UDCCR(dev, UDCCR_SUSIR);
  1623. handled = 1;
  1624. DBG(DBG_VERBOSE, "USB suspend\n");
  1625. if (dev->gadget.speed != USB_SPEED_UNKNOWN
  1626. && dev->driver
  1627. && dev->driver->suspend)
  1628. dev->driver->suspend(&dev->gadget);
  1629. ep0_idle (dev);
  1630. }
  1631. /* RESume Interrupt Request */
  1632. if (unlikely(udccr & UDCCR_RESIR)) {
  1633. udc_ack_int_UDCCR(dev, UDCCR_RESIR);
  1634. handled = 1;
  1635. DBG(DBG_VERBOSE, "USB resume\n");
  1636. if (dev->gadget.speed != USB_SPEED_UNKNOWN
  1637. && dev->driver
  1638. && dev->driver->resume)
  1639. dev->driver->resume(&dev->gadget);
  1640. }
  1641. /* ReSeT Interrupt Request - USB reset */
  1642. if (unlikely(udccr & UDCCR_RSTIR)) {
  1643. udc_ack_int_UDCCR(dev, UDCCR_RSTIR);
  1644. handled = 1;
  1645. if ((udc_get_reg(dev, UDCCR) & UDCCR_UDA) == 0) {
  1646. DBG(DBG_VERBOSE, "USB reset start\n");
  1647. /* reset driver and endpoints,
  1648. * in case that's not yet done
  1649. */
  1650. reset_gadget(dev, dev->driver);
  1651. } else {
  1652. DBG(DBG_VERBOSE, "USB reset end\n");
  1653. dev->gadget.speed = USB_SPEED_FULL;
  1654. memset(&dev->stats, 0, sizeof dev->stats);
  1655. /* driver and endpoints are still reset */
  1656. }
  1657. } else {
  1658. u32 usir0 = udc_get_reg(dev, USIR0) &
  1659. ~udc_get_reg(dev, UICR0);
  1660. u32 usir1 = udc_get_reg(dev, USIR1) &
  1661. ~udc_get_reg(dev, UICR1);
  1662. int i;
  1663. if (unlikely (!usir0 && !usir1))
  1664. continue;
  1665. DBG(DBG_VERY_NOISY, "irq %02x.%02x\n", usir1, usir0);
  1666. /* control traffic */
  1667. if (usir0 & USIR0_IR0) {
  1668. dev->ep[0].pio_irqs++;
  1669. handle_ep0(dev);
  1670. handled = 1;
  1671. }
  1672. /* endpoint data transfers */
  1673. for (i = 0; i < 8; i++) {
  1674. u32 tmp = 1 << i;
  1675. if (i && (usir0 & tmp)) {
  1676. handle_ep(&dev->ep[i]);
  1677. udc_set_reg(dev, USIR0,
  1678. udc_get_reg(dev, USIR0) | tmp);
  1679. handled = 1;
  1680. }
  1681. #ifndef CONFIG_USB_PXA25X_SMALL
  1682. if (usir1 & tmp) {
  1683. handle_ep(&dev->ep[i+8]);
  1684. udc_set_reg(dev, USIR1,
  1685. udc_get_reg(dev, USIR1) | tmp);
  1686. handled = 1;
  1687. }
  1688. #endif
  1689. }
  1690. }
  1691. /* we could also ask for 1 msec SOF (SIR) interrupts */
  1692. } while (handled);
  1693. return IRQ_HANDLED;
  1694. }
  1695. /*-------------------------------------------------------------------------*/
  1696. static void nop_release (struct device *dev)
  1697. {
  1698. DMSG("%s %s\n", __func__, dev_name(dev));
  1699. }
  1700. /* this uses load-time allocation and initialization (instead of
  1701. * doing it at run-time) to save code, eliminate fault paths, and
  1702. * be more obviously correct.
  1703. */
  1704. static struct pxa25x_udc memory = {
  1705. .gadget = {
  1706. .ops = &pxa25x_udc_ops,
  1707. .ep0 = &memory.ep[0].ep,
  1708. .name = driver_name,
  1709. .dev = {
  1710. .init_name = "gadget",
  1711. .release = nop_release,
  1712. },
  1713. },
  1714. /* control endpoint */
  1715. .ep[0] = {
  1716. .ep = {
  1717. .name = ep0name,
  1718. .ops = &pxa25x_ep_ops,
  1719. .maxpacket = EP0_FIFO_SIZE,
  1720. .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL,
  1721. USB_EP_CAPS_DIR_ALL),
  1722. },
  1723. .dev = &memory,
  1724. .regoff_udccs = UDCCS0,
  1725. .regoff_uddr = UDDR0,
  1726. },
  1727. /* first group of endpoints */
  1728. .ep[1] = {
  1729. .ep = {
  1730. .name = "ep1in-bulk",
  1731. .ops = &pxa25x_ep_ops,
  1732. .maxpacket = BULK_FIFO_SIZE,
  1733. .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
  1734. USB_EP_CAPS_DIR_IN),
  1735. },
  1736. .dev = &memory,
  1737. .fifo_size = BULK_FIFO_SIZE,
  1738. .bEndpointAddress = USB_DIR_IN | 1,
  1739. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1740. .regoff_udccs = UDCCS1,
  1741. .regoff_uddr = UDDR1,
  1742. },
  1743. .ep[2] = {
  1744. .ep = {
  1745. .name = "ep2out-bulk",
  1746. .ops = &pxa25x_ep_ops,
  1747. .maxpacket = BULK_FIFO_SIZE,
  1748. .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
  1749. USB_EP_CAPS_DIR_OUT),
  1750. },
  1751. .dev = &memory,
  1752. .fifo_size = BULK_FIFO_SIZE,
  1753. .bEndpointAddress = 2,
  1754. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1755. .regoff_udccs = UDCCS2,
  1756. .regoff_ubcr = UBCR2,
  1757. .regoff_uddr = UDDR2,
  1758. },
  1759. #ifndef CONFIG_USB_PXA25X_SMALL
  1760. .ep[3] = {
  1761. .ep = {
  1762. .name = "ep3in-iso",
  1763. .ops = &pxa25x_ep_ops,
  1764. .maxpacket = ISO_FIFO_SIZE,
  1765. .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
  1766. USB_EP_CAPS_DIR_IN),
  1767. },
  1768. .dev = &memory,
  1769. .fifo_size = ISO_FIFO_SIZE,
  1770. .bEndpointAddress = USB_DIR_IN | 3,
  1771. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1772. .regoff_udccs = UDCCS3,
  1773. .regoff_uddr = UDDR3,
  1774. },
  1775. .ep[4] = {
  1776. .ep = {
  1777. .name = "ep4out-iso",
  1778. .ops = &pxa25x_ep_ops,
  1779. .maxpacket = ISO_FIFO_SIZE,
  1780. .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
  1781. USB_EP_CAPS_DIR_OUT),
  1782. },
  1783. .dev = &memory,
  1784. .fifo_size = ISO_FIFO_SIZE,
  1785. .bEndpointAddress = 4,
  1786. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1787. .regoff_udccs = UDCCS4,
  1788. .regoff_ubcr = UBCR4,
  1789. .regoff_uddr = UDDR4,
  1790. },
  1791. .ep[5] = {
  1792. .ep = {
  1793. .name = "ep5in-int",
  1794. .ops = &pxa25x_ep_ops,
  1795. .maxpacket = INT_FIFO_SIZE,
  1796. .caps = USB_EP_CAPS(0, 0),
  1797. },
  1798. .dev = &memory,
  1799. .fifo_size = INT_FIFO_SIZE,
  1800. .bEndpointAddress = USB_DIR_IN | 5,
  1801. .bmAttributes = USB_ENDPOINT_XFER_INT,
  1802. .regoff_udccs = UDCCS5,
  1803. .regoff_uddr = UDDR5,
  1804. },
  1805. /* second group of endpoints */
  1806. .ep[6] = {
  1807. .ep = {
  1808. .name = "ep6in-bulk",
  1809. .ops = &pxa25x_ep_ops,
  1810. .maxpacket = BULK_FIFO_SIZE,
  1811. .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
  1812. USB_EP_CAPS_DIR_IN),
  1813. },
  1814. .dev = &memory,
  1815. .fifo_size = BULK_FIFO_SIZE,
  1816. .bEndpointAddress = USB_DIR_IN | 6,
  1817. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1818. .regoff_udccs = UDCCS6,
  1819. .regoff_uddr = UDDR6,
  1820. },
  1821. .ep[7] = {
  1822. .ep = {
  1823. .name = "ep7out-bulk",
  1824. .ops = &pxa25x_ep_ops,
  1825. .maxpacket = BULK_FIFO_SIZE,
  1826. .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
  1827. USB_EP_CAPS_DIR_OUT),
  1828. },
  1829. .dev = &memory,
  1830. .fifo_size = BULK_FIFO_SIZE,
  1831. .bEndpointAddress = 7,
  1832. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1833. .regoff_udccs = UDCCS7,
  1834. .regoff_ubcr = UBCR7,
  1835. .regoff_uddr = UDDR7,
  1836. },
  1837. .ep[8] = {
  1838. .ep = {
  1839. .name = "ep8in-iso",
  1840. .ops = &pxa25x_ep_ops,
  1841. .maxpacket = ISO_FIFO_SIZE,
  1842. .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
  1843. USB_EP_CAPS_DIR_IN),
  1844. },
  1845. .dev = &memory,
  1846. .fifo_size = ISO_FIFO_SIZE,
  1847. .bEndpointAddress = USB_DIR_IN | 8,
  1848. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1849. .regoff_udccs = UDCCS8,
  1850. .regoff_uddr = UDDR8,
  1851. },
  1852. .ep[9] = {
  1853. .ep = {
  1854. .name = "ep9out-iso",
  1855. .ops = &pxa25x_ep_ops,
  1856. .maxpacket = ISO_FIFO_SIZE,
  1857. .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
  1858. USB_EP_CAPS_DIR_OUT),
  1859. },
  1860. .dev = &memory,
  1861. .fifo_size = ISO_FIFO_SIZE,
  1862. .bEndpointAddress = 9,
  1863. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1864. .regoff_udccs = UDCCS9,
  1865. .regoff_ubcr = UBCR9,
  1866. .regoff_uddr = UDDR9,
  1867. },
  1868. .ep[10] = {
  1869. .ep = {
  1870. .name = "ep10in-int",
  1871. .ops = &pxa25x_ep_ops,
  1872. .maxpacket = INT_FIFO_SIZE,
  1873. .caps = USB_EP_CAPS(0, 0),
  1874. },
  1875. .dev = &memory,
  1876. .fifo_size = INT_FIFO_SIZE,
  1877. .bEndpointAddress = USB_DIR_IN | 10,
  1878. .bmAttributes = USB_ENDPOINT_XFER_INT,
  1879. .regoff_udccs = UDCCS10,
  1880. .regoff_uddr = UDDR10,
  1881. },
  1882. /* third group of endpoints */
  1883. .ep[11] = {
  1884. .ep = {
  1885. .name = "ep11in-bulk",
  1886. .ops = &pxa25x_ep_ops,
  1887. .maxpacket = BULK_FIFO_SIZE,
  1888. .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
  1889. USB_EP_CAPS_DIR_IN),
  1890. },
  1891. .dev = &memory,
  1892. .fifo_size = BULK_FIFO_SIZE,
  1893. .bEndpointAddress = USB_DIR_IN | 11,
  1894. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1895. .regoff_udccs = UDCCS11,
  1896. .regoff_uddr = UDDR11,
  1897. },
  1898. .ep[12] = {
  1899. .ep = {
  1900. .name = "ep12out-bulk",
  1901. .ops = &pxa25x_ep_ops,
  1902. .maxpacket = BULK_FIFO_SIZE,
  1903. .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_BULK,
  1904. USB_EP_CAPS_DIR_OUT),
  1905. },
  1906. .dev = &memory,
  1907. .fifo_size = BULK_FIFO_SIZE,
  1908. .bEndpointAddress = 12,
  1909. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  1910. .regoff_udccs = UDCCS12,
  1911. .regoff_ubcr = UBCR12,
  1912. .regoff_uddr = UDDR12,
  1913. },
  1914. .ep[13] = {
  1915. .ep = {
  1916. .name = "ep13in-iso",
  1917. .ops = &pxa25x_ep_ops,
  1918. .maxpacket = ISO_FIFO_SIZE,
  1919. .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
  1920. USB_EP_CAPS_DIR_IN),
  1921. },
  1922. .dev = &memory,
  1923. .fifo_size = ISO_FIFO_SIZE,
  1924. .bEndpointAddress = USB_DIR_IN | 13,
  1925. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1926. .regoff_udccs = UDCCS13,
  1927. .regoff_uddr = UDDR13,
  1928. },
  1929. .ep[14] = {
  1930. .ep = {
  1931. .name = "ep14out-iso",
  1932. .ops = &pxa25x_ep_ops,
  1933. .maxpacket = ISO_FIFO_SIZE,
  1934. .caps = USB_EP_CAPS(USB_EP_CAPS_TYPE_ISO,
  1935. USB_EP_CAPS_DIR_OUT),
  1936. },
  1937. .dev = &memory,
  1938. .fifo_size = ISO_FIFO_SIZE,
  1939. .bEndpointAddress = 14,
  1940. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  1941. .regoff_udccs = UDCCS14,
  1942. .regoff_ubcr = UBCR14,
  1943. .regoff_uddr = UDDR14,
  1944. },
  1945. .ep[15] = {
  1946. .ep = {
  1947. .name = "ep15in-int",
  1948. .ops = &pxa25x_ep_ops,
  1949. .maxpacket = INT_FIFO_SIZE,
  1950. .caps = USB_EP_CAPS(0, 0),
  1951. },
  1952. .dev = &memory,
  1953. .fifo_size = INT_FIFO_SIZE,
  1954. .bEndpointAddress = USB_DIR_IN | 15,
  1955. .bmAttributes = USB_ENDPOINT_XFER_INT,
  1956. .regoff_udccs = UDCCS15,
  1957. .regoff_uddr = UDDR15,
  1958. },
  1959. #endif /* !CONFIG_USB_PXA25X_SMALL */
  1960. };
  1961. #define CP15R0_VENDOR_MASK 0xffffe000
  1962. #if defined(CONFIG_ARCH_PXA)
  1963. #define CP15R0_XSCALE_VALUE 0x69052000 /* intel/arm/xscale */
  1964. #elif defined(CONFIG_ARCH_IXP4XX)
  1965. #define CP15R0_XSCALE_VALUE 0x69054000 /* intel/arm/ixp4xx */
  1966. #endif
  1967. #define CP15R0_PROD_MASK 0x000003f0
  1968. #define PXA25x 0x00000100 /* and PXA26x */
  1969. #define PXA210 0x00000120
  1970. #define CP15R0_REV_MASK 0x0000000f
  1971. #define CP15R0_PRODREV_MASK (CP15R0_PROD_MASK | CP15R0_REV_MASK)
  1972. #define PXA255_A0 0x00000106 /* or PXA260_B1 */
  1973. #define PXA250_C0 0x00000105 /* or PXA26x_B0 */
  1974. #define PXA250_B2 0x00000104
  1975. #define PXA250_B1 0x00000103 /* or PXA260_A0 */
  1976. #define PXA250_B0 0x00000102
  1977. #define PXA250_A1 0x00000101
  1978. #define PXA250_A0 0x00000100
  1979. #define PXA210_C0 0x00000125
  1980. #define PXA210_B2 0x00000124
  1981. #define PXA210_B1 0x00000123
  1982. #define PXA210_B0 0x00000122
  1983. #define IXP425_A0 0x000001c1
  1984. #define IXP425_B0 0x000001f1
  1985. #define IXP465_AD 0x00000200
  1986. /*
  1987. * probe - binds to the platform device
  1988. */
  1989. static int pxa25x_udc_probe(struct platform_device *pdev)
  1990. {
  1991. struct pxa25x_udc *dev = &memory;
  1992. int retval, irq;
  1993. u32 chiprev;
  1994. struct resource *res;
  1995. pr_info("%s: version %s\n", driver_name, DRIVER_VERSION);
  1996. /* insist on Intel/ARM/XScale */
  1997. asm("mrc%? p15, 0, %0, c0, c0" : "=r" (chiprev));
  1998. if ((chiprev & CP15R0_VENDOR_MASK) != CP15R0_XSCALE_VALUE) {
  1999. pr_err("%s: not XScale!\n", driver_name);
  2000. return -ENODEV;
  2001. }
  2002. /* trigger chiprev-specific logic */
  2003. switch (chiprev & CP15R0_PRODREV_MASK) {
  2004. #if defined(CONFIG_ARCH_PXA)
  2005. case PXA255_A0:
  2006. dev->has_cfr = 1;
  2007. break;
  2008. case PXA250_A0:
  2009. case PXA250_A1:
  2010. /* A0/A1 "not released"; ep 13, 15 unusable */
  2011. /* fall through */
  2012. case PXA250_B2: case PXA210_B2:
  2013. case PXA250_B1: case PXA210_B1:
  2014. case PXA250_B0: case PXA210_B0:
  2015. /* OUT-DMA is broken ... */
  2016. /* fall through */
  2017. case PXA250_C0: case PXA210_C0:
  2018. break;
  2019. #elif defined(CONFIG_ARCH_IXP4XX)
  2020. case IXP425_A0:
  2021. case IXP425_B0:
  2022. case IXP465_AD:
  2023. dev->has_cfr = 1;
  2024. break;
  2025. #endif
  2026. default:
  2027. pr_err("%s: unrecognized processor: %08x\n",
  2028. driver_name, chiprev);
  2029. /* iop3xx, ixp4xx, ... */
  2030. return -ENODEV;
  2031. }
  2032. irq = platform_get_irq(pdev, 0);
  2033. if (irq < 0)
  2034. return -ENODEV;
  2035. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2036. dev->regs = devm_ioremap_resource(&pdev->dev, res);
  2037. if (IS_ERR(dev->regs))
  2038. return PTR_ERR(dev->regs);
  2039. dev->clk = devm_clk_get(&pdev->dev, NULL);
  2040. if (IS_ERR(dev->clk))
  2041. return PTR_ERR(dev->clk);
  2042. pr_debug("%s: IRQ %d%s%s\n", driver_name, irq,
  2043. dev->has_cfr ? "" : " (!cfr)",
  2044. SIZE_STR "(pio)"
  2045. );
  2046. /* other non-static parts of init */
  2047. dev->dev = &pdev->dev;
  2048. dev->mach = dev_get_platdata(&pdev->dev);
  2049. dev->transceiver = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
  2050. if (gpio_is_valid(dev->mach->gpio_pullup)) {
  2051. retval = devm_gpio_request(&pdev->dev, dev->mach->gpio_pullup,
  2052. "pca25x_udc GPIO PULLUP");
  2053. if (retval) {
  2054. dev_dbg(&pdev->dev,
  2055. "can't get pullup gpio %d, err: %d\n",
  2056. dev->mach->gpio_pullup, retval);
  2057. goto err;
  2058. }
  2059. gpio_direction_output(dev->mach->gpio_pullup, 0);
  2060. }
  2061. timer_setup(&dev->timer, udc_watchdog, 0);
  2062. the_controller = dev;
  2063. platform_set_drvdata(pdev, dev);
  2064. udc_disable(dev);
  2065. udc_reinit(dev);
  2066. dev->vbus = 0;
  2067. /* irq setup after old hardware state is cleaned up */
  2068. retval = devm_request_irq(&pdev->dev, irq, pxa25x_udc_irq, 0,
  2069. driver_name, dev);
  2070. if (retval != 0) {
  2071. pr_err("%s: can't get irq %d, err %d\n",
  2072. driver_name, irq, retval);
  2073. goto err;
  2074. }
  2075. dev->got_irq = 1;
  2076. #ifdef CONFIG_ARCH_LUBBOCK
  2077. if (machine_is_lubbock()) {
  2078. retval = devm_request_irq(&pdev->dev, LUBBOCK_USB_DISC_IRQ,
  2079. lubbock_vbus_irq, 0, driver_name,
  2080. dev);
  2081. if (retval != 0) {
  2082. pr_err("%s: can't get irq %i, err %d\n",
  2083. driver_name, LUBBOCK_USB_DISC_IRQ, retval);
  2084. goto err;
  2085. }
  2086. retval = devm_request_irq(&pdev->dev, LUBBOCK_USB_IRQ,
  2087. lubbock_vbus_irq, 0, driver_name,
  2088. dev);
  2089. if (retval != 0) {
  2090. pr_err("%s: can't get irq %i, err %d\n",
  2091. driver_name, LUBBOCK_USB_IRQ, retval);
  2092. goto err;
  2093. }
  2094. } else
  2095. #endif
  2096. create_debug_files(dev);
  2097. retval = usb_add_gadget_udc(&pdev->dev, &dev->gadget);
  2098. if (!retval)
  2099. return retval;
  2100. remove_debug_files(dev);
  2101. err:
  2102. if (!IS_ERR_OR_NULL(dev->transceiver))
  2103. dev->transceiver = NULL;
  2104. return retval;
  2105. }
  2106. static void pxa25x_udc_shutdown(struct platform_device *_dev)
  2107. {
  2108. pullup_off();
  2109. }
  2110. static int pxa25x_udc_remove(struct platform_device *pdev)
  2111. {
  2112. struct pxa25x_udc *dev = platform_get_drvdata(pdev);
  2113. if (dev->driver)
  2114. return -EBUSY;
  2115. usb_del_gadget_udc(&dev->gadget);
  2116. dev->pullup = 0;
  2117. pullup(dev);
  2118. remove_debug_files(dev);
  2119. if (!IS_ERR_OR_NULL(dev->transceiver))
  2120. dev->transceiver = NULL;
  2121. the_controller = NULL;
  2122. return 0;
  2123. }
  2124. /*-------------------------------------------------------------------------*/
  2125. #ifdef CONFIG_PM
  2126. /* USB suspend (controlled by the host) and system suspend (controlled
  2127. * by the PXA) don't necessarily work well together. If USB is active,
  2128. * the 48 MHz clock is required; so the system can't enter 33 MHz idle
  2129. * mode, or any deeper PM saving state.
  2130. *
  2131. * For now, we punt and forcibly disconnect from the USB host when PXA
  2132. * enters any suspend state. While we're disconnected, we always disable
  2133. * the 48MHz USB clock ... allowing PXA sleep and/or 33 MHz idle states.
  2134. * Boards without software pullup control shouldn't use those states.
  2135. * VBUS IRQs should probably be ignored so that the PXA device just acts
  2136. * "dead" to USB hosts until system resume.
  2137. */
  2138. static int pxa25x_udc_suspend(struct platform_device *dev, pm_message_t state)
  2139. {
  2140. struct pxa25x_udc *udc = platform_get_drvdata(dev);
  2141. unsigned long flags;
  2142. if (!gpio_is_valid(udc->mach->gpio_pullup) && !udc->mach->udc_command)
  2143. WARNING("USB host won't detect disconnect!\n");
  2144. udc->suspended = 1;
  2145. local_irq_save(flags);
  2146. pullup(udc);
  2147. local_irq_restore(flags);
  2148. return 0;
  2149. }
  2150. static int pxa25x_udc_resume(struct platform_device *dev)
  2151. {
  2152. struct pxa25x_udc *udc = platform_get_drvdata(dev);
  2153. unsigned long flags;
  2154. udc->suspended = 0;
  2155. local_irq_save(flags);
  2156. pullup(udc);
  2157. local_irq_restore(flags);
  2158. return 0;
  2159. }
  2160. #else
  2161. #define pxa25x_udc_suspend NULL
  2162. #define pxa25x_udc_resume NULL
  2163. #endif
  2164. /*-------------------------------------------------------------------------*/
  2165. static struct platform_driver udc_driver = {
  2166. .shutdown = pxa25x_udc_shutdown,
  2167. .probe = pxa25x_udc_probe,
  2168. .remove = pxa25x_udc_remove,
  2169. .suspend = pxa25x_udc_suspend,
  2170. .resume = pxa25x_udc_resume,
  2171. .driver = {
  2172. .name = "pxa25x-udc",
  2173. },
  2174. };
  2175. module_platform_driver(udc_driver);
  2176. MODULE_DESCRIPTION(DRIVER_DESC);
  2177. MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
  2178. MODULE_LICENSE("GPL");
  2179. MODULE_ALIAS("platform:pxa25x-udc");