core.h 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  1. /*
  2. * core.h - DesignWare USB3 DRD Core Header
  3. *
  4. * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
  5. *
  6. * Authors: Felipe Balbi <balbi@ti.com>,
  7. * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 of
  11. * the License as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #ifndef __DRIVERS_USB_DWC3_CORE_H
  19. #define __DRIVERS_USB_DWC3_CORE_H
  20. #include <linux/device.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/ioport.h>
  23. #include <linux/list.h>
  24. #include <linux/bitops.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/mm.h>
  27. #include <linux/debugfs.h>
  28. #include <linux/wait.h>
  29. #include <linux/workqueue.h>
  30. #include <linux/usb/ch9.h>
  31. #include <linux/usb/gadget.h>
  32. #include <linux/usb/otg.h>
  33. #include <linux/ulpi/interface.h>
  34. #include <linux/phy/phy.h>
  35. #define DWC3_MSG_MAX 500
  36. /* Global constants */
  37. #define DWC3_PULL_UP_TIMEOUT 500 /* ms */
  38. #define DWC3_BOUNCE_SIZE 1024 /* size of a superspeed bulk */
  39. #define DWC3_EP0_SETUP_SIZE 512
  40. #define DWC3_ENDPOINTS_NUM 32
  41. #define DWC3_XHCI_RESOURCES_NUM 2
  42. #define DWC3_SCRATCHBUF_SIZE 4096 /* each buffer is assumed to be 4KiB */
  43. #define DWC3_EVENT_BUFFERS_SIZE 4096
  44. #define DWC3_EVENT_TYPE_MASK 0xfe
  45. #define DWC3_EVENT_TYPE_DEV 0
  46. #define DWC3_EVENT_TYPE_CARKIT 3
  47. #define DWC3_EVENT_TYPE_I2C 4
  48. #define DWC3_DEVICE_EVENT_DISCONNECT 0
  49. #define DWC3_DEVICE_EVENT_RESET 1
  50. #define DWC3_DEVICE_EVENT_CONNECT_DONE 2
  51. #define DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE 3
  52. #define DWC3_DEVICE_EVENT_WAKEUP 4
  53. #define DWC3_DEVICE_EVENT_HIBER_REQ 5
  54. #define DWC3_DEVICE_EVENT_EOPF 6
  55. #define DWC3_DEVICE_EVENT_SOF 7
  56. #define DWC3_DEVICE_EVENT_ERRATIC_ERROR 9
  57. #define DWC3_DEVICE_EVENT_CMD_CMPL 10
  58. #define DWC3_DEVICE_EVENT_OVERFLOW 11
  59. #define DWC3_GEVNTCOUNT_MASK 0xfffc
  60. #define DWC3_GEVNTCOUNT_EHB BIT(31)
  61. #define DWC3_GSNPSID_MASK 0xffff0000
  62. #define DWC3_GSNPSREV_MASK 0xffff
  63. /* DWC3 registers memory space boundries */
  64. #define DWC3_XHCI_REGS_START 0x0
  65. #define DWC3_XHCI_REGS_END 0x7fff
  66. #define DWC3_GLOBALS_REGS_START 0xc100
  67. #define DWC3_GLOBALS_REGS_END 0xc6ff
  68. #define DWC3_DEVICE_REGS_START 0xc700
  69. #define DWC3_DEVICE_REGS_END 0xcbff
  70. #define DWC3_OTG_REGS_START 0xcc00
  71. #define DWC3_OTG_REGS_END 0xccff
  72. /* Global Registers */
  73. #define DWC3_GSBUSCFG0 0xc100
  74. #define DWC3_GSBUSCFG1 0xc104
  75. #define DWC3_GTXTHRCFG 0xc108
  76. #define DWC3_GRXTHRCFG 0xc10c
  77. #define DWC3_GCTL 0xc110
  78. #define DWC3_GEVTEN 0xc114
  79. #define DWC3_GSTS 0xc118
  80. #define DWC3_GUCTL1 0xc11c
  81. #define DWC3_GSNPSID 0xc120
  82. #define DWC3_GGPIO 0xc124
  83. #define DWC3_GUID 0xc128
  84. #define DWC3_GUCTL 0xc12c
  85. #define DWC3_GBUSERRADDR0 0xc130
  86. #define DWC3_GBUSERRADDR1 0xc134
  87. #define DWC3_GPRTBIMAP0 0xc138
  88. #define DWC3_GPRTBIMAP1 0xc13c
  89. #define DWC3_GHWPARAMS0 0xc140
  90. #define DWC3_GHWPARAMS1 0xc144
  91. #define DWC3_GHWPARAMS2 0xc148
  92. #define DWC3_GHWPARAMS3 0xc14c
  93. #define DWC3_GHWPARAMS4 0xc150
  94. #define DWC3_GHWPARAMS5 0xc154
  95. #define DWC3_GHWPARAMS6 0xc158
  96. #define DWC3_GHWPARAMS7 0xc15c
  97. #define DWC3_GDBGFIFOSPACE 0xc160
  98. #define DWC3_GDBGLTSSM 0xc164
  99. #define DWC3_GPRTBIMAP_HS0 0xc180
  100. #define DWC3_GPRTBIMAP_HS1 0xc184
  101. #define DWC3_GPRTBIMAP_FS0 0xc188
  102. #define DWC3_GPRTBIMAP_FS1 0xc18c
  103. #define DWC3_GUCTL2 0xc19c
  104. #define DWC3_VER_NUMBER 0xc1a0
  105. #define DWC3_VER_TYPE 0xc1a4
  106. #define DWC3_GUSB2PHYCFG(n) (0xc200 + ((n) * 0x04))
  107. #define DWC3_GUSB2I2CCTL(n) (0xc240 + ((n) * 0x04))
  108. #define DWC3_GUSB2PHYACC(n) (0xc280 + ((n) * 0x04))
  109. #define DWC3_GUSB3PIPECTL(n) (0xc2c0 + ((n) * 0x04))
  110. #define DWC3_GTXFIFOSIZ(n) (0xc300 + ((n) * 0x04))
  111. #define DWC3_GRXFIFOSIZ(n) (0xc380 + ((n) * 0x04))
  112. #define DWC3_GEVNTADRLO(n) (0xc400 + ((n) * 0x10))
  113. #define DWC3_GEVNTADRHI(n) (0xc404 + ((n) * 0x10))
  114. #define DWC3_GEVNTSIZ(n) (0xc408 + ((n) * 0x10))
  115. #define DWC3_GEVNTCOUNT(n) (0xc40c + ((n) * 0x10))
  116. #define DWC3_GHWPARAMS8 0xc600
  117. #define DWC3_GFLADJ 0xc630
  118. /* Device Registers */
  119. #define DWC3_DCFG 0xc700
  120. #define DWC3_DCTL 0xc704
  121. #define DWC3_DEVTEN 0xc708
  122. #define DWC3_DSTS 0xc70c
  123. #define DWC3_DGCMDPAR 0xc710
  124. #define DWC3_DGCMD 0xc714
  125. #define DWC3_DALEPENA 0xc720
  126. #define DWC3_DEP_BASE(n) (0xc800 + ((n) * 0x10))
  127. #define DWC3_DEPCMDPAR2 0x00
  128. #define DWC3_DEPCMDPAR1 0x04
  129. #define DWC3_DEPCMDPAR0 0x08
  130. #define DWC3_DEPCMD 0x0c
  131. #define DWC3_DEV_IMOD(n) (0xca00 + ((n) * 0x4))
  132. /* OTG Registers */
  133. #define DWC3_OCFG 0xcc00
  134. #define DWC3_OCTL 0xcc04
  135. #define DWC3_OEVT 0xcc08
  136. #define DWC3_OEVTEN 0xcc0C
  137. #define DWC3_OSTS 0xcc10
  138. /* Bit fields */
  139. /* Global Debug Queue/FIFO Space Available Register */
  140. #define DWC3_GDBGFIFOSPACE_NUM(n) ((n) & 0x1f)
  141. #define DWC3_GDBGFIFOSPACE_TYPE(n) (((n) << 5) & 0x1e0)
  142. #define DWC3_GDBGFIFOSPACE_SPACE_AVAILABLE(n) (((n) >> 16) & 0xffff)
  143. #define DWC3_TXFIFOQ 1
  144. #define DWC3_RXFIFOQ 3
  145. #define DWC3_TXREQQ 5
  146. #define DWC3_RXREQQ 7
  147. #define DWC3_RXINFOQ 9
  148. #define DWC3_DESCFETCHQ 13
  149. #define DWC3_EVENTQ 15
  150. /* Global RX Threshold Configuration Register */
  151. #define DWC3_GRXTHRCFG_MAXRXBURSTSIZE(n) (((n) & 0x1f) << 19)
  152. #define DWC3_GRXTHRCFG_RXPKTCNT(n) (((n) & 0xf) << 24)
  153. #define DWC3_GRXTHRCFG_PKTCNTSEL BIT(29)
  154. /* Global Configuration Register */
  155. #define DWC3_GCTL_PWRDNSCALE(n) ((n) << 19)
  156. #define DWC3_GCTL_U2RSTECN BIT(16)
  157. #define DWC3_GCTL_RAMCLKSEL(x) (((x) & DWC3_GCTL_CLK_MASK) << 6)
  158. #define DWC3_GCTL_CLK_BUS (0)
  159. #define DWC3_GCTL_CLK_PIPE (1)
  160. #define DWC3_GCTL_CLK_PIPEHALF (2)
  161. #define DWC3_GCTL_CLK_MASK (3)
  162. #define DWC3_GCTL_PRTCAP(n) (((n) & (3 << 12)) >> 12)
  163. #define DWC3_GCTL_PRTCAPDIR(n) ((n) << 12)
  164. #define DWC3_GCTL_PRTCAP_HOST 1
  165. #define DWC3_GCTL_PRTCAP_DEVICE 2
  166. #define DWC3_GCTL_PRTCAP_OTG 3
  167. #define DWC3_GCTL_CORESOFTRESET BIT(11)
  168. #define DWC3_GCTL_SOFITPSYNC BIT(10)
  169. #define DWC3_GCTL_SCALEDOWN(n) ((n) << 4)
  170. #define DWC3_GCTL_SCALEDOWN_MASK DWC3_GCTL_SCALEDOWN(3)
  171. #define DWC3_GCTL_DISSCRAMBLE BIT(3)
  172. #define DWC3_GCTL_U2EXIT_LFPS BIT(2)
  173. #define DWC3_GCTL_GBLHIBERNATIONEN BIT(1)
  174. #define DWC3_GCTL_DSBLCLKGTNG BIT(0)
  175. /* Global User Control 1 Register */
  176. #define DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS BIT(28)
  177. #define DWC3_GUCTL1_DEV_L1_EXIT_BY_HW BIT(24)
  178. /* Global USB2 PHY Configuration Register */
  179. #define DWC3_GUSB2PHYCFG_PHYSOFTRST BIT(31)
  180. #define DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS BIT(30)
  181. #define DWC3_GUSB2PHYCFG_SUSPHY BIT(6)
  182. #define DWC3_GUSB2PHYCFG_ULPI_UTMI BIT(4)
  183. #define DWC3_GUSB2PHYCFG_ENBLSLPM BIT(8)
  184. #define DWC3_GUSB2PHYCFG_PHYIF(n) (n << 3)
  185. #define DWC3_GUSB2PHYCFG_PHYIF_MASK DWC3_GUSB2PHYCFG_PHYIF(1)
  186. #define DWC3_GUSB2PHYCFG_USBTRDTIM(n) (n << 10)
  187. #define DWC3_GUSB2PHYCFG_USBTRDTIM_MASK DWC3_GUSB2PHYCFG_USBTRDTIM(0xf)
  188. #define USBTRDTIM_UTMI_8_BIT 9
  189. #define USBTRDTIM_UTMI_16_BIT 5
  190. #define UTMI_PHYIF_16_BIT 1
  191. #define UTMI_PHYIF_8_BIT 0
  192. /* Global USB2 PHY Vendor Control Register */
  193. #define DWC3_GUSB2PHYACC_NEWREGREQ BIT(25)
  194. #define DWC3_GUSB2PHYACC_BUSY BIT(23)
  195. #define DWC3_GUSB2PHYACC_WRITE BIT(22)
  196. #define DWC3_GUSB2PHYACC_ADDR(n) (n << 16)
  197. #define DWC3_GUSB2PHYACC_EXTEND_ADDR(n) (n << 8)
  198. #define DWC3_GUSB2PHYACC_DATA(n) (n & 0xff)
  199. /* Global USB3 PIPE Control Register */
  200. #define DWC3_GUSB3PIPECTL_PHYSOFTRST BIT(31)
  201. #define DWC3_GUSB3PIPECTL_U2SSINP3OK BIT(29)
  202. #define DWC3_GUSB3PIPECTL_DISRXDETINP3 BIT(28)
  203. #define DWC3_GUSB3PIPECTL_UX_EXIT_PX BIT(27)
  204. #define DWC3_GUSB3PIPECTL_REQP1P2P3 BIT(24)
  205. #define DWC3_GUSB3PIPECTL_DEP1P2P3(n) ((n) << 19)
  206. #define DWC3_GUSB3PIPECTL_DEP1P2P3_MASK DWC3_GUSB3PIPECTL_DEP1P2P3(7)
  207. #define DWC3_GUSB3PIPECTL_DEP1P2P3_EN DWC3_GUSB3PIPECTL_DEP1P2P3(1)
  208. #define DWC3_GUSB3PIPECTL_DEPOCHANGE BIT(18)
  209. #define DWC3_GUSB3PIPECTL_SUSPHY BIT(17)
  210. #define DWC3_GUSB3PIPECTL_LFPSFILT BIT(9)
  211. #define DWC3_GUSB3PIPECTL_RX_DETOPOLL BIT(8)
  212. #define DWC3_GUSB3PIPECTL_TX_DEEPH_MASK DWC3_GUSB3PIPECTL_TX_DEEPH(3)
  213. #define DWC3_GUSB3PIPECTL_TX_DEEPH(n) ((n) << 1)
  214. /* Global TX Fifo Size Register */
  215. #define DWC3_GTXFIFOSIZ_TXFDEF(n) ((n) & 0xffff)
  216. #define DWC3_GTXFIFOSIZ_TXFSTADDR(n) ((n) & 0xffff0000)
  217. /* Global Event Size Registers */
  218. #define DWC3_GEVNTSIZ_INTMASK BIT(31)
  219. #define DWC3_GEVNTSIZ_SIZE(n) ((n) & 0xffff)
  220. /* Global HWPARAMS0 Register */
  221. #define DWC3_GHWPARAMS0_MODE(n) ((n) & 0x3)
  222. #define DWC3_GHWPARAMS0_MODE_GADGET 0
  223. #define DWC3_GHWPARAMS0_MODE_HOST 1
  224. #define DWC3_GHWPARAMS0_MODE_DRD 2
  225. #define DWC3_GHWPARAMS0_MBUS_TYPE(n) (((n) >> 3) & 0x7)
  226. #define DWC3_GHWPARAMS0_SBUS_TYPE(n) (((n) >> 6) & 0x3)
  227. #define DWC3_GHWPARAMS0_MDWIDTH(n) (((n) >> 8) & 0xff)
  228. #define DWC3_GHWPARAMS0_SDWIDTH(n) (((n) >> 16) & 0xff)
  229. #define DWC3_GHWPARAMS0_AWIDTH(n) (((n) >> 24) & 0xff)
  230. /* Global HWPARAMS1 Register */
  231. #define DWC3_GHWPARAMS1_EN_PWROPT(n) (((n) & (3 << 24)) >> 24)
  232. #define DWC3_GHWPARAMS1_EN_PWROPT_NO 0
  233. #define DWC3_GHWPARAMS1_EN_PWROPT_CLK 1
  234. #define DWC3_GHWPARAMS1_EN_PWROPT_HIB 2
  235. #define DWC3_GHWPARAMS1_PWROPT(n) ((n) << 24)
  236. #define DWC3_GHWPARAMS1_PWROPT_MASK DWC3_GHWPARAMS1_PWROPT(3)
  237. /* Global HWPARAMS3 Register */
  238. #define DWC3_GHWPARAMS3_SSPHY_IFC(n) ((n) & 3)
  239. #define DWC3_GHWPARAMS3_SSPHY_IFC_DIS 0
  240. #define DWC3_GHWPARAMS3_SSPHY_IFC_GEN1 1
  241. #define DWC3_GHWPARAMS3_SSPHY_IFC_GEN2 2 /* DWC_usb31 only */
  242. #define DWC3_GHWPARAMS3_HSPHY_IFC(n) (((n) & (3 << 2)) >> 2)
  243. #define DWC3_GHWPARAMS3_HSPHY_IFC_DIS 0
  244. #define DWC3_GHWPARAMS3_HSPHY_IFC_UTMI 1
  245. #define DWC3_GHWPARAMS3_HSPHY_IFC_ULPI 2
  246. #define DWC3_GHWPARAMS3_HSPHY_IFC_UTMI_ULPI 3
  247. #define DWC3_GHWPARAMS3_FSPHY_IFC(n) (((n) & (3 << 4)) >> 4)
  248. #define DWC3_GHWPARAMS3_FSPHY_IFC_DIS 0
  249. #define DWC3_GHWPARAMS3_FSPHY_IFC_ENA 1
  250. /* Global HWPARAMS4 Register */
  251. #define DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(n) (((n) & (0x0f << 13)) >> 13)
  252. #define DWC3_MAX_HIBER_SCRATCHBUFS 15
  253. /* Global HWPARAMS6 Register */
  254. #define DWC3_GHWPARAMS6_EN_FPGA BIT(7)
  255. /* Global HWPARAMS7 Register */
  256. #define DWC3_GHWPARAMS7_RAM1_DEPTH(n) ((n) & 0xffff)
  257. #define DWC3_GHWPARAMS7_RAM2_DEPTH(n) (((n) >> 16) & 0xffff)
  258. /* Global Frame Length Adjustment Register */
  259. #define DWC3_GFLADJ_30MHZ_SDBND_SEL BIT(7)
  260. #define DWC3_GFLADJ_30MHZ_MASK 0x3f
  261. /* Global User Control Register 2 */
  262. #define DWC3_GUCTL2_RST_ACTBITLATER BIT(14)
  263. /* Device Configuration Register */
  264. #define DWC3_DCFG_DEVADDR(addr) ((addr) << 3)
  265. #define DWC3_DCFG_DEVADDR_MASK DWC3_DCFG_DEVADDR(0x7f)
  266. #define DWC3_DCFG_SPEED_MASK (7 << 0)
  267. #define DWC3_DCFG_SUPERSPEED_PLUS (5 << 0) /* DWC_usb31 only */
  268. #define DWC3_DCFG_SUPERSPEED (4 << 0)
  269. #define DWC3_DCFG_HIGHSPEED (0 << 0)
  270. #define DWC3_DCFG_FULLSPEED BIT(0)
  271. #define DWC3_DCFG_LOWSPEED (2 << 0)
  272. #define DWC3_DCFG_NUMP_SHIFT 17
  273. #define DWC3_DCFG_NUMP(n) (((n) >> DWC3_DCFG_NUMP_SHIFT) & 0x1f)
  274. #define DWC3_DCFG_NUMP_MASK (0x1f << DWC3_DCFG_NUMP_SHIFT)
  275. #define DWC3_DCFG_LPM_CAP BIT(22)
  276. /* Device Control Register */
  277. #define DWC3_DCTL_RUN_STOP BIT(31)
  278. #define DWC3_DCTL_CSFTRST BIT(30)
  279. #define DWC3_DCTL_LSFTRST BIT(29)
  280. #define DWC3_DCTL_HIRD_THRES_MASK (0x1f << 24)
  281. #define DWC3_DCTL_HIRD_THRES(n) ((n) << 24)
  282. #define DWC3_DCTL_APPL1RES BIT(23)
  283. /* These apply for core versions 1.87a and earlier */
  284. #define DWC3_DCTL_TRGTULST_MASK (0x0f << 17)
  285. #define DWC3_DCTL_TRGTULST(n) ((n) << 17)
  286. #define DWC3_DCTL_TRGTULST_U2 (DWC3_DCTL_TRGTULST(2))
  287. #define DWC3_DCTL_TRGTULST_U3 (DWC3_DCTL_TRGTULST(3))
  288. #define DWC3_DCTL_TRGTULST_SS_DIS (DWC3_DCTL_TRGTULST(4))
  289. #define DWC3_DCTL_TRGTULST_RX_DET (DWC3_DCTL_TRGTULST(5))
  290. #define DWC3_DCTL_TRGTULST_SS_INACT (DWC3_DCTL_TRGTULST(6))
  291. /* These apply for core versions 1.94a and later */
  292. #define DWC3_DCTL_LPM_ERRATA_MASK DWC3_DCTL_LPM_ERRATA(0xf)
  293. #define DWC3_DCTL_LPM_ERRATA(n) ((n) << 20)
  294. #define DWC3_DCTL_KEEP_CONNECT BIT(19)
  295. #define DWC3_DCTL_L1_HIBER_EN BIT(18)
  296. #define DWC3_DCTL_CRS BIT(17)
  297. #define DWC3_DCTL_CSS BIT(16)
  298. #define DWC3_DCTL_INITU2ENA BIT(12)
  299. #define DWC3_DCTL_ACCEPTU2ENA BIT(11)
  300. #define DWC3_DCTL_INITU1ENA BIT(10)
  301. #define DWC3_DCTL_ACCEPTU1ENA BIT(9)
  302. #define DWC3_DCTL_TSTCTRL_MASK (0xf << 1)
  303. #define DWC3_DCTL_ULSTCHNGREQ_MASK (0x0f << 5)
  304. #define DWC3_DCTL_ULSTCHNGREQ(n) (((n) << 5) & DWC3_DCTL_ULSTCHNGREQ_MASK)
  305. #define DWC3_DCTL_ULSTCHNG_NO_ACTION (DWC3_DCTL_ULSTCHNGREQ(0))
  306. #define DWC3_DCTL_ULSTCHNG_SS_DISABLED (DWC3_DCTL_ULSTCHNGREQ(4))
  307. #define DWC3_DCTL_ULSTCHNG_RX_DETECT (DWC3_DCTL_ULSTCHNGREQ(5))
  308. #define DWC3_DCTL_ULSTCHNG_SS_INACTIVE (DWC3_DCTL_ULSTCHNGREQ(6))
  309. #define DWC3_DCTL_ULSTCHNG_RECOVERY (DWC3_DCTL_ULSTCHNGREQ(8))
  310. #define DWC3_DCTL_ULSTCHNG_COMPLIANCE (DWC3_DCTL_ULSTCHNGREQ(10))
  311. #define DWC3_DCTL_ULSTCHNG_LOOPBACK (DWC3_DCTL_ULSTCHNGREQ(11))
  312. /* Device Event Enable Register */
  313. #define DWC3_DEVTEN_VNDRDEVTSTRCVEDEN BIT(12)
  314. #define DWC3_DEVTEN_EVNTOVERFLOWEN BIT(11)
  315. #define DWC3_DEVTEN_CMDCMPLTEN BIT(10)
  316. #define DWC3_DEVTEN_ERRTICERREN BIT(9)
  317. #define DWC3_DEVTEN_SOFEN BIT(7)
  318. #define DWC3_DEVTEN_EOPFEN BIT(6)
  319. #define DWC3_DEVTEN_HIBERNATIONREQEVTEN BIT(5)
  320. #define DWC3_DEVTEN_WKUPEVTEN BIT(4)
  321. #define DWC3_DEVTEN_ULSTCNGEN BIT(3)
  322. #define DWC3_DEVTEN_CONNECTDONEEN BIT(2)
  323. #define DWC3_DEVTEN_USBRSTEN BIT(1)
  324. #define DWC3_DEVTEN_DISCONNEVTEN BIT(0)
  325. /* Device Status Register */
  326. #define DWC3_DSTS_DCNRD BIT(29)
  327. /* This applies for core versions 1.87a and earlier */
  328. #define DWC3_DSTS_PWRUPREQ BIT(24)
  329. /* These apply for core versions 1.94a and later */
  330. #define DWC3_DSTS_RSS BIT(25)
  331. #define DWC3_DSTS_SSS BIT(24)
  332. #define DWC3_DSTS_COREIDLE BIT(23)
  333. #define DWC3_DSTS_DEVCTRLHLT BIT(22)
  334. #define DWC3_DSTS_USBLNKST_MASK (0x0f << 18)
  335. #define DWC3_DSTS_USBLNKST(n) (((n) & DWC3_DSTS_USBLNKST_MASK) >> 18)
  336. #define DWC3_DSTS_RXFIFOEMPTY BIT(17)
  337. #define DWC3_DSTS_SOFFN_MASK (0x3fff << 3)
  338. #define DWC3_DSTS_SOFFN(n) (((n) & DWC3_DSTS_SOFFN_MASK) >> 3)
  339. #define DWC3_DSTS_CONNECTSPD (7 << 0)
  340. #define DWC3_DSTS_SUPERSPEED_PLUS (5 << 0) /* DWC_usb31 only */
  341. #define DWC3_DSTS_SUPERSPEED (4 << 0)
  342. #define DWC3_DSTS_HIGHSPEED (0 << 0)
  343. #define DWC3_DSTS_FULLSPEED BIT(0)
  344. #define DWC3_DSTS_LOWSPEED (2 << 0)
  345. /* Device Generic Command Register */
  346. #define DWC3_DGCMD_SET_LMP 0x01
  347. #define DWC3_DGCMD_SET_PERIODIC_PAR 0x02
  348. #define DWC3_DGCMD_XMIT_FUNCTION 0x03
  349. /* These apply for core versions 1.94a and later */
  350. #define DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO 0x04
  351. #define DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI 0x05
  352. #define DWC3_DGCMD_SELECTED_FIFO_FLUSH 0x09
  353. #define DWC3_DGCMD_ALL_FIFO_FLUSH 0x0a
  354. #define DWC3_DGCMD_SET_ENDPOINT_NRDY 0x0c
  355. #define DWC3_DGCMD_RUN_SOC_BUS_LOOPBACK 0x10
  356. #define DWC3_DGCMD_STATUS(n) (((n) >> 12) & 0x0F)
  357. #define DWC3_DGCMD_CMDACT BIT(10)
  358. #define DWC3_DGCMD_CMDIOC BIT(8)
  359. /* Device Generic Command Parameter Register */
  360. #define DWC3_DGCMDPAR_FORCE_LINKPM_ACCEPT BIT(0)
  361. #define DWC3_DGCMDPAR_FIFO_NUM(n) ((n) << 0)
  362. #define DWC3_DGCMDPAR_RX_FIFO (0 << 5)
  363. #define DWC3_DGCMDPAR_TX_FIFO BIT(5)
  364. #define DWC3_DGCMDPAR_LOOPBACK_DIS (0 << 0)
  365. #define DWC3_DGCMDPAR_LOOPBACK_ENA BIT(0)
  366. /* Device Endpoint Command Register */
  367. #define DWC3_DEPCMD_PARAM_SHIFT 16
  368. #define DWC3_DEPCMD_PARAM(x) ((x) << DWC3_DEPCMD_PARAM_SHIFT)
  369. #define DWC3_DEPCMD_GET_RSC_IDX(x) (((x) >> DWC3_DEPCMD_PARAM_SHIFT) & 0x7f)
  370. #define DWC3_DEPCMD_STATUS(x) (((x) >> 12) & 0x0F)
  371. #define DWC3_DEPCMD_HIPRI_FORCERM BIT(11)
  372. #define DWC3_DEPCMD_CLEARPENDIN BIT(11)
  373. #define DWC3_DEPCMD_CMDACT BIT(10)
  374. #define DWC3_DEPCMD_CMDIOC BIT(8)
  375. #define DWC3_DEPCMD_DEPSTARTCFG (0x09 << 0)
  376. #define DWC3_DEPCMD_ENDTRANSFER (0x08 << 0)
  377. #define DWC3_DEPCMD_UPDATETRANSFER (0x07 << 0)
  378. #define DWC3_DEPCMD_STARTTRANSFER (0x06 << 0)
  379. #define DWC3_DEPCMD_CLEARSTALL (0x05 << 0)
  380. #define DWC3_DEPCMD_SETSTALL (0x04 << 0)
  381. /* This applies for core versions 1.90a and earlier */
  382. #define DWC3_DEPCMD_GETSEQNUMBER (0x03 << 0)
  383. /* This applies for core versions 1.94a and later */
  384. #define DWC3_DEPCMD_GETEPSTATE (0x03 << 0)
  385. #define DWC3_DEPCMD_SETTRANSFRESOURCE (0x02 << 0)
  386. #define DWC3_DEPCMD_SETEPCONFIG (0x01 << 0)
  387. #define DWC3_DEPCMD_CMD(x) ((x) & 0xf)
  388. /* The EP number goes 0..31 so ep0 is always out and ep1 is always in */
  389. #define DWC3_DALEPENA_EP(n) BIT(n)
  390. #define DWC3_DEPCMD_TYPE_CONTROL 0
  391. #define DWC3_DEPCMD_TYPE_ISOC 1
  392. #define DWC3_DEPCMD_TYPE_BULK 2
  393. #define DWC3_DEPCMD_TYPE_INTR 3
  394. #define DWC3_DEV_IMOD_COUNT_SHIFT 16
  395. #define DWC3_DEV_IMOD_COUNT_MASK (0xffff << 16)
  396. #define DWC3_DEV_IMOD_INTERVAL_SHIFT 0
  397. #define DWC3_DEV_IMOD_INTERVAL_MASK (0xffff << 0)
  398. /* Structures */
  399. struct dwc3_trb;
  400. /**
  401. * struct dwc3_event_buffer - Software event buffer representation
  402. * @buf: _THE_ buffer
  403. * @cache: The buffer cache used in the threaded interrupt
  404. * @length: size of this buffer
  405. * @lpos: event offset
  406. * @count: cache of last read event count register
  407. * @flags: flags related to this event buffer
  408. * @dma: dma_addr_t
  409. * @dwc: pointer to DWC controller
  410. */
  411. struct dwc3_event_buffer {
  412. void *buf;
  413. void *cache;
  414. unsigned length;
  415. unsigned int lpos;
  416. unsigned int count;
  417. unsigned int flags;
  418. #define DWC3_EVENT_PENDING BIT(0)
  419. dma_addr_t dma;
  420. struct dwc3 *dwc;
  421. };
  422. #define DWC3_EP_FLAG_STALLED BIT(0)
  423. #define DWC3_EP_FLAG_WEDGED BIT(1)
  424. #define DWC3_EP_DIRECTION_TX true
  425. #define DWC3_EP_DIRECTION_RX false
  426. #define DWC3_TRB_NUM 256
  427. /**
  428. * struct dwc3_ep - device side endpoint representation
  429. * @endpoint: usb endpoint
  430. * @pending_list: list of pending requests for this endpoint
  431. * @started_list: list of started requests on this endpoint
  432. * @wait_end_transfer: wait_queue_head_t for waiting on End Transfer complete
  433. * @lock: spinlock for endpoint request queue traversal
  434. * @regs: pointer to first endpoint register
  435. * @trb_pool: array of transaction buffers
  436. * @trb_pool_dma: dma address of @trb_pool
  437. * @trb_enqueue: enqueue 'pointer' into TRB array
  438. * @trb_dequeue: dequeue 'pointer' into TRB array
  439. * @dwc: pointer to DWC controller
  440. * @saved_state: ep state saved during hibernation
  441. * @flags: endpoint flags (wedged, stalled, ...)
  442. * @number: endpoint number (1 - 15)
  443. * @type: set to bmAttributes & USB_ENDPOINT_XFERTYPE_MASK
  444. * @resource_index: Resource transfer index
  445. * @interval: the interval on which the ISOC transfer is started
  446. * @allocated_requests: number of requests allocated
  447. * @queued_requests: number of requests queued for transfer
  448. * @name: a human readable name e.g. ep1out-bulk
  449. * @direction: true for TX, false for RX
  450. * @stream_capable: true when streams are enabled
  451. */
  452. struct dwc3_ep {
  453. struct usb_ep endpoint;
  454. struct list_head pending_list;
  455. struct list_head started_list;
  456. wait_queue_head_t wait_end_transfer;
  457. spinlock_t lock;
  458. void __iomem *regs;
  459. struct dwc3_trb *trb_pool;
  460. dma_addr_t trb_pool_dma;
  461. struct dwc3 *dwc;
  462. u32 saved_state;
  463. unsigned flags;
  464. #define DWC3_EP_ENABLED BIT(0)
  465. #define DWC3_EP_STALL BIT(1)
  466. #define DWC3_EP_WEDGE BIT(2)
  467. #define DWC3_EP_BUSY BIT(4)
  468. #define DWC3_EP_PENDING_REQUEST BIT(5)
  469. #define DWC3_EP_MISSED_ISOC BIT(6)
  470. #define DWC3_EP_END_TRANSFER_PENDING BIT(7)
  471. #define DWC3_EP_TRANSFER_STARTED BIT(8)
  472. /* This last one is specific to EP0 */
  473. #define DWC3_EP0_DIR_IN BIT(31)
  474. /*
  475. * IMPORTANT: we *know* we have 256 TRBs in our @trb_pool, so we will
  476. * use a u8 type here. If anybody decides to increase number of TRBs to
  477. * anything larger than 256 - I can't see why people would want to do
  478. * this though - then this type needs to be changed.
  479. *
  480. * By using u8 types we ensure that our % operator when incrementing
  481. * enqueue and dequeue get optimized away by the compiler.
  482. */
  483. u8 trb_enqueue;
  484. u8 trb_dequeue;
  485. u8 number;
  486. u8 type;
  487. u8 resource_index;
  488. u32 allocated_requests;
  489. u32 queued_requests;
  490. u32 interval;
  491. char name[20];
  492. unsigned direction:1;
  493. unsigned stream_capable:1;
  494. };
  495. enum dwc3_phy {
  496. DWC3_PHY_UNKNOWN = 0,
  497. DWC3_PHY_USB3,
  498. DWC3_PHY_USB2,
  499. };
  500. enum dwc3_ep0_next {
  501. DWC3_EP0_UNKNOWN = 0,
  502. DWC3_EP0_COMPLETE,
  503. DWC3_EP0_NRDY_DATA,
  504. DWC3_EP0_NRDY_STATUS,
  505. };
  506. enum dwc3_ep0_state {
  507. EP0_UNCONNECTED = 0,
  508. EP0_SETUP_PHASE,
  509. EP0_DATA_PHASE,
  510. EP0_STATUS_PHASE,
  511. };
  512. enum dwc3_link_state {
  513. /* In SuperSpeed */
  514. DWC3_LINK_STATE_U0 = 0x00, /* in HS, means ON */
  515. DWC3_LINK_STATE_U1 = 0x01,
  516. DWC3_LINK_STATE_U2 = 0x02, /* in HS, means SLEEP */
  517. DWC3_LINK_STATE_U3 = 0x03, /* in HS, means SUSPEND */
  518. DWC3_LINK_STATE_SS_DIS = 0x04,
  519. DWC3_LINK_STATE_RX_DET = 0x05, /* in HS, means Early Suspend */
  520. DWC3_LINK_STATE_SS_INACT = 0x06,
  521. DWC3_LINK_STATE_POLL = 0x07,
  522. DWC3_LINK_STATE_RECOV = 0x08,
  523. DWC3_LINK_STATE_HRESET = 0x09,
  524. DWC3_LINK_STATE_CMPLY = 0x0a,
  525. DWC3_LINK_STATE_LPBK = 0x0b,
  526. DWC3_LINK_STATE_RESET = 0x0e,
  527. DWC3_LINK_STATE_RESUME = 0x0f,
  528. DWC3_LINK_STATE_MASK = 0x0f,
  529. };
  530. /* TRB Length, PCM and Status */
  531. #define DWC3_TRB_SIZE_MASK (0x00ffffff)
  532. #define DWC3_TRB_SIZE_LENGTH(n) ((n) & DWC3_TRB_SIZE_MASK)
  533. #define DWC3_TRB_SIZE_PCM1(n) (((n) & 0x03) << 24)
  534. #define DWC3_TRB_SIZE_TRBSTS(n) (((n) & (0x0f << 28)) >> 28)
  535. #define DWC3_TRBSTS_OK 0
  536. #define DWC3_TRBSTS_MISSED_ISOC 1
  537. #define DWC3_TRBSTS_SETUP_PENDING 2
  538. #define DWC3_TRB_STS_XFER_IN_PROG 4
  539. /* TRB Control */
  540. #define DWC3_TRB_CTRL_HWO BIT(0)
  541. #define DWC3_TRB_CTRL_LST BIT(1)
  542. #define DWC3_TRB_CTRL_CHN BIT(2)
  543. #define DWC3_TRB_CTRL_CSP BIT(3)
  544. #define DWC3_TRB_CTRL_TRBCTL(n) (((n) & 0x3f) << 4)
  545. #define DWC3_TRB_CTRL_ISP_IMI BIT(10)
  546. #define DWC3_TRB_CTRL_IOC BIT(11)
  547. #define DWC3_TRB_CTRL_SID_SOFN(n) (((n) & 0xffff) << 14)
  548. #define DWC3_TRBCTL_TYPE(n) ((n) & (0x3f << 4))
  549. #define DWC3_TRBCTL_NORMAL DWC3_TRB_CTRL_TRBCTL(1)
  550. #define DWC3_TRBCTL_CONTROL_SETUP DWC3_TRB_CTRL_TRBCTL(2)
  551. #define DWC3_TRBCTL_CONTROL_STATUS2 DWC3_TRB_CTRL_TRBCTL(3)
  552. #define DWC3_TRBCTL_CONTROL_STATUS3 DWC3_TRB_CTRL_TRBCTL(4)
  553. #define DWC3_TRBCTL_CONTROL_DATA DWC3_TRB_CTRL_TRBCTL(5)
  554. #define DWC3_TRBCTL_ISOCHRONOUS_FIRST DWC3_TRB_CTRL_TRBCTL(6)
  555. #define DWC3_TRBCTL_ISOCHRONOUS DWC3_TRB_CTRL_TRBCTL(7)
  556. #define DWC3_TRBCTL_LINK_TRB DWC3_TRB_CTRL_TRBCTL(8)
  557. /**
  558. * struct dwc3_trb - transfer request block (hw format)
  559. * @bpl: DW0-3
  560. * @bph: DW4-7
  561. * @size: DW8-B
  562. * @ctrl: DWC-F
  563. */
  564. struct dwc3_trb {
  565. u32 bpl;
  566. u32 bph;
  567. u32 size;
  568. u32 ctrl;
  569. } __packed;
  570. /**
  571. * struct dwc3_hwparams - copy of HWPARAMS registers
  572. * @hwparams0: GHWPARAMS0
  573. * @hwparams1: GHWPARAMS1
  574. * @hwparams2: GHWPARAMS2
  575. * @hwparams3: GHWPARAMS3
  576. * @hwparams4: GHWPARAMS4
  577. * @hwparams5: GHWPARAMS5
  578. * @hwparams6: GHWPARAMS6
  579. * @hwparams7: GHWPARAMS7
  580. * @hwparams8: GHWPARAMS8
  581. */
  582. struct dwc3_hwparams {
  583. u32 hwparams0;
  584. u32 hwparams1;
  585. u32 hwparams2;
  586. u32 hwparams3;
  587. u32 hwparams4;
  588. u32 hwparams5;
  589. u32 hwparams6;
  590. u32 hwparams7;
  591. u32 hwparams8;
  592. };
  593. /* HWPARAMS0 */
  594. #define DWC3_MODE(n) ((n) & 0x7)
  595. #define DWC3_MDWIDTH(n) (((n) & 0xff00) >> 8)
  596. /* HWPARAMS1 */
  597. #define DWC3_NUM_INT(n) (((n) & (0x3f << 15)) >> 15)
  598. /* HWPARAMS3 */
  599. #define DWC3_NUM_IN_EPS_MASK (0x1f << 18)
  600. #define DWC3_NUM_EPS_MASK (0x3f << 12)
  601. #define DWC3_NUM_EPS(p) (((p)->hwparams3 & \
  602. (DWC3_NUM_EPS_MASK)) >> 12)
  603. #define DWC3_NUM_IN_EPS(p) (((p)->hwparams3 & \
  604. (DWC3_NUM_IN_EPS_MASK)) >> 18)
  605. /* HWPARAMS7 */
  606. #define DWC3_RAM1_DEPTH(n) ((n) & 0xffff)
  607. /**
  608. * struct dwc3_request - representation of a transfer request
  609. * @request: struct usb_request to be transferred
  610. * @list: a list_head used for request queueing
  611. * @dep: struct dwc3_ep owning this request
  612. * @sg: pointer to first incomplete sg
  613. * @num_pending_sgs: counter to pending sgs
  614. * @remaining: amount of data remaining
  615. * @epnum: endpoint number to which this request refers
  616. * @trb: pointer to struct dwc3_trb
  617. * @trb_dma: DMA address of @trb
  618. * @unaligned: true for OUT endpoints with length not divisible by maxp
  619. * @direction: IN or OUT direction flag
  620. * @mapped: true when request has been dma-mapped
  621. * @started: request is started
  622. * @zero: wants a ZLP
  623. */
  624. struct dwc3_request {
  625. struct usb_request request;
  626. struct list_head list;
  627. struct dwc3_ep *dep;
  628. struct scatterlist *sg;
  629. unsigned num_pending_sgs;
  630. unsigned remaining;
  631. u8 epnum;
  632. struct dwc3_trb *trb;
  633. dma_addr_t trb_dma;
  634. unsigned unaligned:1;
  635. unsigned direction:1;
  636. unsigned mapped:1;
  637. unsigned started:1;
  638. unsigned zero:1;
  639. };
  640. /*
  641. * struct dwc3_scratchpad_array - hibernation scratchpad array
  642. * (format defined by hw)
  643. */
  644. struct dwc3_scratchpad_array {
  645. __le64 dma_adr[DWC3_MAX_HIBER_SCRATCHBUFS];
  646. };
  647. /**
  648. * struct dwc3 - representation of our controller
  649. * @drd_work: workqueue used for role swapping
  650. * @ep0_trb: trb which is used for the ctrl_req
  651. * @bounce: address of bounce buffer
  652. * @scratchbuf: address of scratch buffer
  653. * @setup_buf: used while precessing STD USB requests
  654. * @ep0_trb_addr: dma address of @ep0_trb
  655. * @bounce_addr: dma address of @bounce
  656. * @ep0_usb_req: dummy req used while handling STD USB requests
  657. * @scratch_addr: dma address of scratchbuf
  658. * @ep0_in_setup: one control transfer is completed and enter setup phase
  659. * @lock: for synchronizing
  660. * @dev: pointer to our struct device
  661. * @sysdev: pointer to the DMA-capable device
  662. * @xhci: pointer to our xHCI child
  663. * @xhci_resources: struct resources for our @xhci child
  664. * @ev_buf: struct dwc3_event_buffer pointer
  665. * @eps: endpoint array
  666. * @gadget: device side representation of the peripheral controller
  667. * @gadget_driver: pointer to the gadget driver
  668. * @regs: base address for our registers
  669. * @regs_size: address space size
  670. * @fladj: frame length adjustment
  671. * @irq_gadget: peripheral controller's IRQ number
  672. * @nr_scratch: number of scratch buffers
  673. * @u1u2: only used on revisions <1.83a for workaround
  674. * @maximum_speed: maximum speed requested (mainly for testing purposes)
  675. * @revision: revision register contents
  676. * @dr_mode: requested mode of operation
  677. * @current_dr_role: current role of operation when in dual-role mode
  678. * @desired_dr_role: desired role of operation when in dual-role mode
  679. * @edev: extcon handle
  680. * @edev_nb: extcon notifier
  681. * @hsphy_mode: UTMI phy mode, one of following:
  682. * - USBPHY_INTERFACE_MODE_UTMI
  683. * - USBPHY_INTERFACE_MODE_UTMIW
  684. * @usb2_phy: pointer to USB2 PHY
  685. * @usb3_phy: pointer to USB3 PHY
  686. * @usb2_generic_phy: pointer to USB2 PHY
  687. * @usb3_generic_phy: pointer to USB3 PHY
  688. * @ulpi: pointer to ulpi interface
  689. * @isoch_delay: wValue from Set Isochronous Delay request;
  690. * @u2sel: parameter from Set SEL request.
  691. * @u2pel: parameter from Set SEL request.
  692. * @u1sel: parameter from Set SEL request.
  693. * @u1pel: parameter from Set SEL request.
  694. * @num_eps: number of endpoints
  695. * @ep0_next_event: hold the next expected event
  696. * @ep0state: state of endpoint zero
  697. * @link_state: link state
  698. * @speed: device speed (super, high, full, low)
  699. * @hwparams: copy of hwparams registers
  700. * @root: debugfs root folder pointer
  701. * @regset: debugfs pointer to regdump file
  702. * @test_mode: true when we're entering a USB test mode
  703. * @test_mode_nr: test feature selector
  704. * @lpm_nyet_threshold: LPM NYET response threshold
  705. * @hird_threshold: HIRD threshold
  706. * @hsphy_interface: "utmi" or "ulpi"
  707. * @connected: true when we're connected to a host, false otherwise
  708. * @delayed_status: true when gadget driver asks for delayed status
  709. * @ep0_bounced: true when we used bounce buffer
  710. * @ep0_expect_in: true when we expect a DATA IN transfer
  711. * @has_hibernation: true when dwc3 was configured with Hibernation
  712. * @sysdev_is_parent: true when dwc3 device has a parent driver
  713. * @has_lpm_erratum: true when core was configured with LPM Erratum. Note that
  714. * there's now way for software to detect this in runtime.
  715. * @is_utmi_l1_suspend: the core asserts output signal
  716. * 0 - utmi_sleep_n
  717. * 1 - utmi_l1_suspend_n
  718. * @is_fpga: true when we are using the FPGA board
  719. * @pending_events: true when we have pending IRQs to be handled
  720. * @pullups_connected: true when Run/Stop bit is set
  721. * @setup_packet_pending: true when there's a Setup Packet in FIFO. Workaround
  722. * @three_stage_setup: set if we perform a three phase setup
  723. * @usb3_lpm_capable: set if hadrware supports Link Power Management
  724. * @disable_scramble_quirk: set if we enable the disable scramble quirk
  725. * @u2exit_lfps_quirk: set if we enable u2exit lfps quirk
  726. * @u2ss_inp3_quirk: set if we enable P3 OK for U2/SS Inactive quirk
  727. * @req_p1p2p3_quirk: set if we enable request p1p2p3 quirk
  728. * @del_p1p2p3_quirk: set if we enable delay p1p2p3 quirk
  729. * @del_phy_power_chg_quirk: set if we enable delay phy power change quirk
  730. * @lfps_filter_quirk: set if we enable LFPS filter quirk
  731. * @rx_detect_poll_quirk: set if we enable rx_detect to polling lfps quirk
  732. * @dis_u3_susphy_quirk: set if we disable usb3 suspend phy
  733. * @dis_u2_susphy_quirk: set if we disable usb2 suspend phy
  734. * @dis_enblslpm_quirk: set if we clear enblslpm in GUSB2PHYCFG,
  735. * disabling the suspend signal to the PHY.
  736. * @dis_rxdet_inp3_quirk: set if we disable Rx.Detect in P3
  737. * @dis_u2_freeclk_exists_quirk : set if we clear u2_freeclk_exists
  738. * in GUSB2PHYCFG, specify that USB2 PHY doesn't
  739. * provide a free-running PHY clock.
  740. * @dis_del_phy_power_chg_quirk: set if we disable delay phy power
  741. * change quirk.
  742. * @dis_tx_ipgap_linecheck_quirk: set if we disable u2mac linestate
  743. * check during HS transmit.
  744. * @tx_de_emphasis_quirk: set if we enable Tx de-emphasis quirk
  745. * @tx_de_emphasis: Tx de-emphasis value
  746. * 0 - -6dB de-emphasis
  747. * 1 - -3.5dB de-emphasis
  748. * 2 - No de-emphasis
  749. * 3 - Reserved
  750. * @imod_interval: set the interrupt moderation interval in 250ns
  751. * increments or 0 to disable.
  752. */
  753. struct dwc3 {
  754. struct work_struct drd_work;
  755. struct dwc3_trb *ep0_trb;
  756. void *bounce;
  757. void *scratchbuf;
  758. u8 *setup_buf;
  759. dma_addr_t ep0_trb_addr;
  760. dma_addr_t bounce_addr;
  761. dma_addr_t scratch_addr;
  762. struct dwc3_request ep0_usb_req;
  763. struct completion ep0_in_setup;
  764. /* device lock */
  765. spinlock_t lock;
  766. struct device *dev;
  767. struct device *sysdev;
  768. struct platform_device *xhci;
  769. struct resource xhci_resources[DWC3_XHCI_RESOURCES_NUM];
  770. struct dwc3_event_buffer *ev_buf;
  771. struct dwc3_ep *eps[DWC3_ENDPOINTS_NUM];
  772. struct usb_gadget gadget;
  773. struct usb_gadget_driver *gadget_driver;
  774. struct usb_phy *usb2_phy;
  775. struct usb_phy *usb3_phy;
  776. struct phy *usb2_generic_phy;
  777. struct phy *usb3_generic_phy;
  778. struct ulpi *ulpi;
  779. void __iomem *regs;
  780. size_t regs_size;
  781. enum usb_dr_mode dr_mode;
  782. u32 current_dr_role;
  783. u32 desired_dr_role;
  784. struct extcon_dev *edev;
  785. struct notifier_block edev_nb;
  786. enum usb_phy_interface hsphy_mode;
  787. u32 fladj;
  788. u32 irq_gadget;
  789. u32 nr_scratch;
  790. u32 u1u2;
  791. u32 maximum_speed;
  792. /*
  793. * All 3.1 IP version constants are greater than the 3.0 IP
  794. * version constants. This works for most version checks in
  795. * dwc3. However, in the future, this may not apply as
  796. * features may be developed on newer versions of the 3.0 IP
  797. * that are not in the 3.1 IP.
  798. */
  799. u32 revision;
  800. #define DWC3_REVISION_173A 0x5533173a
  801. #define DWC3_REVISION_175A 0x5533175a
  802. #define DWC3_REVISION_180A 0x5533180a
  803. #define DWC3_REVISION_183A 0x5533183a
  804. #define DWC3_REVISION_185A 0x5533185a
  805. #define DWC3_REVISION_187A 0x5533187a
  806. #define DWC3_REVISION_188A 0x5533188a
  807. #define DWC3_REVISION_190A 0x5533190a
  808. #define DWC3_REVISION_194A 0x5533194a
  809. #define DWC3_REVISION_200A 0x5533200a
  810. #define DWC3_REVISION_202A 0x5533202a
  811. #define DWC3_REVISION_210A 0x5533210a
  812. #define DWC3_REVISION_220A 0x5533220a
  813. #define DWC3_REVISION_230A 0x5533230a
  814. #define DWC3_REVISION_240A 0x5533240a
  815. #define DWC3_REVISION_250A 0x5533250a
  816. #define DWC3_REVISION_260A 0x5533260a
  817. #define DWC3_REVISION_270A 0x5533270a
  818. #define DWC3_REVISION_280A 0x5533280a
  819. #define DWC3_REVISION_290A 0x5533290a
  820. #define DWC3_REVISION_300A 0x5533300a
  821. #define DWC3_REVISION_310A 0x5533310a
  822. /*
  823. * NOTICE: we're using bit 31 as a "is usb 3.1" flag. This is really
  824. * just so dwc31 revisions are always larger than dwc3.
  825. */
  826. #define DWC3_REVISION_IS_DWC31 0x80000000
  827. #define DWC3_USB31_REVISION_110A (0x3131302a | DWC3_REVISION_IS_DWC31)
  828. #define DWC3_USB31_REVISION_120A (0x3132302a | DWC3_REVISION_IS_DWC31)
  829. enum dwc3_ep0_next ep0_next_event;
  830. enum dwc3_ep0_state ep0state;
  831. enum dwc3_link_state link_state;
  832. u16 isoch_delay;
  833. u16 u2sel;
  834. u16 u2pel;
  835. u8 u1sel;
  836. u8 u1pel;
  837. u8 speed;
  838. u8 num_eps;
  839. struct dwc3_hwparams hwparams;
  840. struct dentry *root;
  841. struct debugfs_regset32 *regset;
  842. u8 test_mode;
  843. u8 test_mode_nr;
  844. u8 lpm_nyet_threshold;
  845. u8 hird_threshold;
  846. const char *hsphy_interface;
  847. unsigned connected:1;
  848. unsigned delayed_status:1;
  849. unsigned ep0_bounced:1;
  850. unsigned ep0_expect_in:1;
  851. unsigned has_hibernation:1;
  852. unsigned sysdev_is_parent:1;
  853. unsigned has_lpm_erratum:1;
  854. unsigned is_utmi_l1_suspend:1;
  855. unsigned is_fpga:1;
  856. unsigned pending_events:1;
  857. unsigned pullups_connected:1;
  858. unsigned setup_packet_pending:1;
  859. unsigned three_stage_setup:1;
  860. unsigned usb3_lpm_capable:1;
  861. unsigned disable_scramble_quirk:1;
  862. unsigned u2exit_lfps_quirk:1;
  863. unsigned u2ss_inp3_quirk:1;
  864. unsigned req_p1p2p3_quirk:1;
  865. unsigned del_p1p2p3_quirk:1;
  866. unsigned del_phy_power_chg_quirk:1;
  867. unsigned lfps_filter_quirk:1;
  868. unsigned rx_detect_poll_quirk:1;
  869. unsigned dis_u3_susphy_quirk:1;
  870. unsigned dis_u2_susphy_quirk:1;
  871. unsigned dis_enblslpm_quirk:1;
  872. unsigned dis_rxdet_inp3_quirk:1;
  873. unsigned dis_u2_freeclk_exists_quirk:1;
  874. unsigned dis_del_phy_power_chg_quirk:1;
  875. unsigned dis_tx_ipgap_linecheck_quirk:1;
  876. unsigned tx_de_emphasis_quirk:1;
  877. unsigned tx_de_emphasis:2;
  878. u16 imod_interval;
  879. };
  880. #define work_to_dwc(w) (container_of((w), struct dwc3, drd_work))
  881. /* -------------------------------------------------------------------------- */
  882. struct dwc3_event_type {
  883. u32 is_devspec:1;
  884. u32 type:7;
  885. u32 reserved8_31:24;
  886. } __packed;
  887. #define DWC3_DEPEVT_XFERCOMPLETE 0x01
  888. #define DWC3_DEPEVT_XFERINPROGRESS 0x02
  889. #define DWC3_DEPEVT_XFERNOTREADY 0x03
  890. #define DWC3_DEPEVT_RXTXFIFOEVT 0x04
  891. #define DWC3_DEPEVT_STREAMEVT 0x06
  892. #define DWC3_DEPEVT_EPCMDCMPLT 0x07
  893. /**
  894. * struct dwc3_event_depvt - Device Endpoint Events
  895. * @one_bit: indicates this is an endpoint event (not used)
  896. * @endpoint_number: number of the endpoint
  897. * @endpoint_event: The event we have:
  898. * 0x00 - Reserved
  899. * 0x01 - XferComplete
  900. * 0x02 - XferInProgress
  901. * 0x03 - XferNotReady
  902. * 0x04 - RxTxFifoEvt (IN->Underrun, OUT->Overrun)
  903. * 0x05 - Reserved
  904. * 0x06 - StreamEvt
  905. * 0x07 - EPCmdCmplt
  906. * @reserved11_10: Reserved, don't use.
  907. * @status: Indicates the status of the event. Refer to databook for
  908. * more information.
  909. * @parameters: Parameters of the current event. Refer to databook for
  910. * more information.
  911. */
  912. struct dwc3_event_depevt {
  913. u32 one_bit:1;
  914. u32 endpoint_number:5;
  915. u32 endpoint_event:4;
  916. u32 reserved11_10:2;
  917. u32 status:4;
  918. /* Within XferNotReady */
  919. #define DEPEVT_STATUS_TRANSFER_ACTIVE BIT(3)
  920. /* Within XferComplete */
  921. #define DEPEVT_STATUS_BUSERR BIT(0)
  922. #define DEPEVT_STATUS_SHORT BIT(1)
  923. #define DEPEVT_STATUS_IOC BIT(2)
  924. #define DEPEVT_STATUS_LST BIT(3)
  925. /* Stream event only */
  926. #define DEPEVT_STREAMEVT_FOUND 1
  927. #define DEPEVT_STREAMEVT_NOTFOUND 2
  928. /* Control-only Status */
  929. #define DEPEVT_STATUS_CONTROL_DATA 1
  930. #define DEPEVT_STATUS_CONTROL_STATUS 2
  931. #define DEPEVT_STATUS_CONTROL_PHASE(n) ((n) & 3)
  932. /* In response to Start Transfer */
  933. #define DEPEVT_TRANSFER_NO_RESOURCE 1
  934. #define DEPEVT_TRANSFER_BUS_EXPIRY 2
  935. u32 parameters:16;
  936. /* For Command Complete Events */
  937. #define DEPEVT_PARAMETER_CMD(n) (((n) & (0xf << 8)) >> 8)
  938. } __packed;
  939. /**
  940. * struct dwc3_event_devt - Device Events
  941. * @one_bit: indicates this is a non-endpoint event (not used)
  942. * @device_event: indicates it's a device event. Should read as 0x00
  943. * @type: indicates the type of device event.
  944. * 0 - DisconnEvt
  945. * 1 - USBRst
  946. * 2 - ConnectDone
  947. * 3 - ULStChng
  948. * 4 - WkUpEvt
  949. * 5 - Reserved
  950. * 6 - EOPF
  951. * 7 - SOF
  952. * 8 - Reserved
  953. * 9 - ErrticErr
  954. * 10 - CmdCmplt
  955. * 11 - EvntOverflow
  956. * 12 - VndrDevTstRcved
  957. * @reserved15_12: Reserved, not used
  958. * @event_info: Information about this event
  959. * @reserved31_25: Reserved, not used
  960. */
  961. struct dwc3_event_devt {
  962. u32 one_bit:1;
  963. u32 device_event:7;
  964. u32 type:4;
  965. u32 reserved15_12:4;
  966. u32 event_info:9;
  967. u32 reserved31_25:7;
  968. } __packed;
  969. /**
  970. * struct dwc3_event_gevt - Other Core Events
  971. * @one_bit: indicates this is a non-endpoint event (not used)
  972. * @device_event: indicates it's (0x03) Carkit or (0x04) I2C event.
  973. * @phy_port_number: self-explanatory
  974. * @reserved31_12: Reserved, not used.
  975. */
  976. struct dwc3_event_gevt {
  977. u32 one_bit:1;
  978. u32 device_event:7;
  979. u32 phy_port_number:4;
  980. u32 reserved31_12:20;
  981. } __packed;
  982. /**
  983. * union dwc3_event - representation of Event Buffer contents
  984. * @raw: raw 32-bit event
  985. * @type: the type of the event
  986. * @depevt: Device Endpoint Event
  987. * @devt: Device Event
  988. * @gevt: Global Event
  989. */
  990. union dwc3_event {
  991. u32 raw;
  992. struct dwc3_event_type type;
  993. struct dwc3_event_depevt depevt;
  994. struct dwc3_event_devt devt;
  995. struct dwc3_event_gevt gevt;
  996. };
  997. /**
  998. * struct dwc3_gadget_ep_cmd_params - representation of endpoint command
  999. * parameters
  1000. * @param2: third parameter
  1001. * @param1: second parameter
  1002. * @param0: first parameter
  1003. */
  1004. struct dwc3_gadget_ep_cmd_params {
  1005. u32 param2;
  1006. u32 param1;
  1007. u32 param0;
  1008. };
  1009. /*
  1010. * DWC3 Features to be used as Driver Data
  1011. */
  1012. #define DWC3_HAS_PERIPHERAL BIT(0)
  1013. #define DWC3_HAS_XHCI BIT(1)
  1014. #define DWC3_HAS_OTG BIT(3)
  1015. /* prototypes */
  1016. void dwc3_set_mode(struct dwc3 *dwc, u32 mode);
  1017. u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type);
  1018. /* check whether we are on the DWC_usb3 core */
  1019. static inline bool dwc3_is_usb3(struct dwc3 *dwc)
  1020. {
  1021. return !(dwc->revision & DWC3_REVISION_IS_DWC31);
  1022. }
  1023. /* check whether we are on the DWC_usb31 core */
  1024. static inline bool dwc3_is_usb31(struct dwc3 *dwc)
  1025. {
  1026. return !!(dwc->revision & DWC3_REVISION_IS_DWC31);
  1027. }
  1028. bool dwc3_has_imod(struct dwc3 *dwc);
  1029. #if IS_ENABLED(CONFIG_USB_DWC3_HOST) || IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE)
  1030. int dwc3_host_init(struct dwc3 *dwc);
  1031. void dwc3_host_exit(struct dwc3 *dwc);
  1032. #else
  1033. static inline int dwc3_host_init(struct dwc3 *dwc)
  1034. { return 0; }
  1035. static inline void dwc3_host_exit(struct dwc3 *dwc)
  1036. { }
  1037. #endif
  1038. #if IS_ENABLED(CONFIG_USB_DWC3_GADGET) || IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE)
  1039. int dwc3_gadget_init(struct dwc3 *dwc);
  1040. void dwc3_gadget_exit(struct dwc3 *dwc);
  1041. int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode);
  1042. int dwc3_gadget_get_link_state(struct dwc3 *dwc);
  1043. int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state);
  1044. int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
  1045. struct dwc3_gadget_ep_cmd_params *params);
  1046. int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param);
  1047. #else
  1048. static inline int dwc3_gadget_init(struct dwc3 *dwc)
  1049. { return 0; }
  1050. static inline void dwc3_gadget_exit(struct dwc3 *dwc)
  1051. { }
  1052. static inline int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
  1053. { return 0; }
  1054. static inline int dwc3_gadget_get_link_state(struct dwc3 *dwc)
  1055. { return 0; }
  1056. static inline int dwc3_gadget_set_link_state(struct dwc3 *dwc,
  1057. enum dwc3_link_state state)
  1058. { return 0; }
  1059. static inline int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
  1060. struct dwc3_gadget_ep_cmd_params *params)
  1061. { return 0; }
  1062. static inline int dwc3_send_gadget_generic_command(struct dwc3 *dwc,
  1063. int cmd, u32 param)
  1064. { return 0; }
  1065. #endif
  1066. #if IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE)
  1067. int dwc3_drd_init(struct dwc3 *dwc);
  1068. void dwc3_drd_exit(struct dwc3 *dwc);
  1069. #else
  1070. static inline int dwc3_drd_init(struct dwc3 *dwc)
  1071. { return 0; }
  1072. static inline void dwc3_drd_exit(struct dwc3 *dwc)
  1073. { }
  1074. #endif
  1075. /* power management interface */
  1076. #if !IS_ENABLED(CONFIG_USB_DWC3_HOST)
  1077. int dwc3_gadget_suspend(struct dwc3 *dwc);
  1078. int dwc3_gadget_resume(struct dwc3 *dwc);
  1079. void dwc3_gadget_process_pending_events(struct dwc3 *dwc);
  1080. #else
  1081. static inline int dwc3_gadget_suspend(struct dwc3 *dwc)
  1082. {
  1083. return 0;
  1084. }
  1085. static inline int dwc3_gadget_resume(struct dwc3 *dwc)
  1086. {
  1087. return 0;
  1088. }
  1089. static inline void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
  1090. {
  1091. }
  1092. #endif /* !IS_ENABLED(CONFIG_USB_DWC3_HOST) */
  1093. #if IS_ENABLED(CONFIG_USB_DWC3_ULPI)
  1094. int dwc3_ulpi_init(struct dwc3 *dwc);
  1095. void dwc3_ulpi_exit(struct dwc3 *dwc);
  1096. #else
  1097. static inline int dwc3_ulpi_init(struct dwc3 *dwc)
  1098. { return 0; }
  1099. static inline void dwc3_ulpi_exit(struct dwc3 *dwc)
  1100. { }
  1101. #endif
  1102. #endif /* __DRIVERS_USB_DWC3_CORE_H */