core.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions, and the following disclaimer,
  14. * without modification.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. The names of the above-listed copyright holders may not be used
  19. * to endorse or promote products derived from this software without
  20. * specific prior written permission.
  21. *
  22. * ALTERNATIVELY, this software may be distributed under the terms of the
  23. * GNU General Public License ("GPL") version 2, as published by the Free
  24. * Software Foundation.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  27. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  30. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  31. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  32. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  33. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  34. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  36. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. #ifndef __DRIVERS_USB_DWC3_CORE_H
  39. #define __DRIVERS_USB_DWC3_CORE_H
  40. #include <linux/device.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/ioport.h>
  43. #include <linux/list.h>
  44. #include <linux/dma-mapping.h>
  45. #include <linux/mm.h>
  46. #include <linux/debugfs.h>
  47. #include <linux/usb/ch9.h>
  48. #include <linux/usb/gadget.h>
  49. /* Global constants */
  50. #define DWC3_EP0_BOUNCE_SIZE 512
  51. #define DWC3_ENDPOINTS_NUM 32
  52. #define DWC3_XHCI_RESOURCES_NUM 2
  53. #define DWC3_EVENT_SIZE 4 /* bytes */
  54. #define DWC3_EVENT_MAX_NUM 64 /* 2 events/endpoint */
  55. #define DWC3_EVENT_BUFFERS_SIZE (DWC3_EVENT_SIZE * DWC3_EVENT_MAX_NUM)
  56. #define DWC3_EVENT_TYPE_MASK 0xfe
  57. #define DWC3_EVENT_TYPE_DEV 0
  58. #define DWC3_EVENT_TYPE_CARKIT 3
  59. #define DWC3_EVENT_TYPE_I2C 4
  60. #define DWC3_DEVICE_EVENT_DISCONNECT 0
  61. #define DWC3_DEVICE_EVENT_RESET 1
  62. #define DWC3_DEVICE_EVENT_CONNECT_DONE 2
  63. #define DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE 3
  64. #define DWC3_DEVICE_EVENT_WAKEUP 4
  65. #define DWC3_DEVICE_EVENT_HIBER_REQ 5
  66. #define DWC3_DEVICE_EVENT_EOPF 6
  67. #define DWC3_DEVICE_EVENT_SOF 7
  68. #define DWC3_DEVICE_EVENT_ERRATIC_ERROR 9
  69. #define DWC3_DEVICE_EVENT_CMD_CMPL 10
  70. #define DWC3_DEVICE_EVENT_OVERFLOW 11
  71. #define DWC3_GEVNTCOUNT_MASK 0xfffc
  72. #define DWC3_GSNPSID_MASK 0xffff0000
  73. #define DWC3_GSNPSREV_MASK 0xffff
  74. /* DWC3 registers memory space boundries */
  75. #define DWC3_XHCI_REGS_START 0x0
  76. #define DWC3_XHCI_REGS_END 0x7fff
  77. #define DWC3_GLOBALS_REGS_START 0xc100
  78. #define DWC3_GLOBALS_REGS_END 0xc6ff
  79. #define DWC3_DEVICE_REGS_START 0xc700
  80. #define DWC3_DEVICE_REGS_END 0xcbff
  81. #define DWC3_OTG_REGS_START 0xcc00
  82. #define DWC3_OTG_REGS_END 0xccff
  83. /* Global Registers */
  84. #define DWC3_GSBUSCFG0 0xc100
  85. #define DWC3_GSBUSCFG1 0xc104
  86. #define DWC3_GTXTHRCFG 0xc108
  87. #define DWC3_GRXTHRCFG 0xc10c
  88. #define DWC3_GCTL 0xc110
  89. #define DWC3_GEVTEN 0xc114
  90. #define DWC3_GSTS 0xc118
  91. #define DWC3_GSNPSID 0xc120
  92. #define DWC3_GGPIO 0xc124
  93. #define DWC3_GUID 0xc128
  94. #define DWC3_GUCTL 0xc12c
  95. #define DWC3_GBUSERRADDR0 0xc130
  96. #define DWC3_GBUSERRADDR1 0xc134
  97. #define DWC3_GPRTBIMAP0 0xc138
  98. #define DWC3_GPRTBIMAP1 0xc13c
  99. #define DWC3_GHWPARAMS0 0xc140
  100. #define DWC3_GHWPARAMS1 0xc144
  101. #define DWC3_GHWPARAMS2 0xc148
  102. #define DWC3_GHWPARAMS3 0xc14c
  103. #define DWC3_GHWPARAMS4 0xc150
  104. #define DWC3_GHWPARAMS5 0xc154
  105. #define DWC3_GHWPARAMS6 0xc158
  106. #define DWC3_GHWPARAMS7 0xc15c
  107. #define DWC3_GDBGFIFOSPACE 0xc160
  108. #define DWC3_GDBGLTSSM 0xc164
  109. #define DWC3_GPRTBIMAP_HS0 0xc180
  110. #define DWC3_GPRTBIMAP_HS1 0xc184
  111. #define DWC3_GPRTBIMAP_FS0 0xc188
  112. #define DWC3_GPRTBIMAP_FS1 0xc18c
  113. #define DWC3_GUSB2PHYCFG(n) (0xc200 + (n * 0x04))
  114. #define DWC3_GUSB2I2CCTL(n) (0xc240 + (n * 0x04))
  115. #define DWC3_GUSB2PHYACC(n) (0xc280 + (n * 0x04))
  116. #define DWC3_GUSB3PIPECTL(n) (0xc2c0 + (n * 0x04))
  117. #define DWC3_GTXFIFOSIZ(n) (0xc300 + (n * 0x04))
  118. #define DWC3_GRXFIFOSIZ(n) (0xc380 + (n * 0x04))
  119. #define DWC3_GEVNTADRLO(n) (0xc400 + (n * 0x10))
  120. #define DWC3_GEVNTADRHI(n) (0xc404 + (n * 0x10))
  121. #define DWC3_GEVNTSIZ(n) (0xc408 + (n * 0x10))
  122. #define DWC3_GEVNTCOUNT(n) (0xc40c + (n * 0x10))
  123. #define DWC3_GHWPARAMS8 0xc600
  124. /* Device Registers */
  125. #define DWC3_DCFG 0xc700
  126. #define DWC3_DCTL 0xc704
  127. #define DWC3_DEVTEN 0xc708
  128. #define DWC3_DSTS 0xc70c
  129. #define DWC3_DGCMDPAR 0xc710
  130. #define DWC3_DGCMD 0xc714
  131. #define DWC3_DALEPENA 0xc720
  132. #define DWC3_DEPCMDPAR2(n) (0xc800 + (n * 0x10))
  133. #define DWC3_DEPCMDPAR1(n) (0xc804 + (n * 0x10))
  134. #define DWC3_DEPCMDPAR0(n) (0xc808 + (n * 0x10))
  135. #define DWC3_DEPCMD(n) (0xc80c + (n * 0x10))
  136. /* OTG Registers */
  137. #define DWC3_OCFG 0xcc00
  138. #define DWC3_OCTL 0xcc04
  139. #define DWC3_OEVTEN 0xcc08
  140. #define DWC3_OSTS 0xcc0C
  141. /* Bit fields */
  142. /* Global Configuration Register */
  143. #define DWC3_GCTL_PWRDNSCALE(n) ((n) << 19)
  144. #define DWC3_GCTL_U2RSTECN (1 << 16)
  145. #define DWC3_GCTL_RAMCLKSEL(x) (((x) & DWC3_GCTL_CLK_MASK) << 6)
  146. #define DWC3_GCTL_CLK_BUS (0)
  147. #define DWC3_GCTL_CLK_PIPE (1)
  148. #define DWC3_GCTL_CLK_PIPEHALF (2)
  149. #define DWC3_GCTL_CLK_MASK (3)
  150. #define DWC3_GCTL_PRTCAP(n) (((n) & (3 << 12)) >> 12)
  151. #define DWC3_GCTL_PRTCAPDIR(n) ((n) << 12)
  152. #define DWC3_GCTL_PRTCAP_HOST 1
  153. #define DWC3_GCTL_PRTCAP_DEVICE 2
  154. #define DWC3_GCTL_PRTCAP_OTG 3
  155. #define DWC3_GCTL_CORESOFTRESET (1 << 11)
  156. #define DWC3_GCTL_SCALEDOWN(n) ((n) << 4)
  157. #define DWC3_GCTL_SCALEDOWN_MASK DWC3_GCTL_SCALEDOWN(3)
  158. #define DWC3_GCTL_DISSCRAMBLE (1 << 3)
  159. #define DWC3_GCTL_GBLHIBERNATIONEN (1 << 1)
  160. #define DWC3_GCTL_DSBLCLKGTNG (1 << 0)
  161. /* Global USB2 PHY Configuration Register */
  162. #define DWC3_GUSB2PHYCFG_PHYSOFTRST (1 << 31)
  163. #define DWC3_GUSB2PHYCFG_SUSPHY (1 << 6)
  164. /* Global USB3 PIPE Control Register */
  165. #define DWC3_GUSB3PIPECTL_PHYSOFTRST (1 << 31)
  166. #define DWC3_GUSB3PIPECTL_SUSPHY (1 << 17)
  167. /* Global TX Fifo Size Register */
  168. #define DWC3_GTXFIFOSIZ_TXFDEF(n) ((n) & 0xffff)
  169. #define DWC3_GTXFIFOSIZ_TXFSTADDR(n) ((n) & 0xffff0000)
  170. /* Global HWPARAMS1 Register */
  171. #define DWC3_GHWPARAMS1_EN_PWROPT(n) (((n) & (3 << 24)) >> 24)
  172. #define DWC3_GHWPARAMS1_EN_PWROPT_NO 0
  173. #define DWC3_GHWPARAMS1_EN_PWROPT_CLK 1
  174. #define DWC3_GHWPARAMS1_EN_PWROPT_HIB 2
  175. #define DWC3_GHWPARAMS1_PWROPT(n) ((n) << 24)
  176. #define DWC3_GHWPARAMS1_PWROPT_MASK DWC3_GHWPARAMS1_PWROPT(3)
  177. /* Global HWPARAMS4 Register */
  178. #define DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(n) (((n) & (0x0f << 13)) >> 13)
  179. #define DWC3_MAX_HIBER_SCRATCHBUFS 15
  180. /* Device Configuration Register */
  181. #define DWC3_DCFG_LPM_CAP (1 << 22)
  182. #define DWC3_DCFG_DEVADDR(addr) ((addr) << 3)
  183. #define DWC3_DCFG_DEVADDR_MASK DWC3_DCFG_DEVADDR(0x7f)
  184. #define DWC3_DCFG_SPEED_MASK (7 << 0)
  185. #define DWC3_DCFG_SUPERSPEED (4 << 0)
  186. #define DWC3_DCFG_HIGHSPEED (0 << 0)
  187. #define DWC3_DCFG_FULLSPEED2 (1 << 0)
  188. #define DWC3_DCFG_LOWSPEED (2 << 0)
  189. #define DWC3_DCFG_FULLSPEED1 (3 << 0)
  190. #define DWC3_DCFG_LPM_CAP (1 << 22)
  191. /* Device Control Register */
  192. #define DWC3_DCTL_RUN_STOP (1 << 31)
  193. #define DWC3_DCTL_CSFTRST (1 << 30)
  194. #define DWC3_DCTL_LSFTRST (1 << 29)
  195. #define DWC3_DCTL_HIRD_THRES_MASK (0x1f << 24)
  196. #define DWC3_DCTL_HIRD_THRES(n) ((n) << 24)
  197. #define DWC3_DCTL_APPL1RES (1 << 23)
  198. /* These apply for core versions 1.87a and earlier */
  199. #define DWC3_DCTL_TRGTULST_MASK (0x0f << 17)
  200. #define DWC3_DCTL_TRGTULST(n) ((n) << 17)
  201. #define DWC3_DCTL_TRGTULST_U2 (DWC3_DCTL_TRGTULST(2))
  202. #define DWC3_DCTL_TRGTULST_U3 (DWC3_DCTL_TRGTULST(3))
  203. #define DWC3_DCTL_TRGTULST_SS_DIS (DWC3_DCTL_TRGTULST(4))
  204. #define DWC3_DCTL_TRGTULST_RX_DET (DWC3_DCTL_TRGTULST(5))
  205. #define DWC3_DCTL_TRGTULST_SS_INACT (DWC3_DCTL_TRGTULST(6))
  206. /* These apply for core versions 1.94a and later */
  207. #define DWC3_DCTL_KEEP_CONNECT (1 << 19)
  208. #define DWC3_DCTL_L1_HIBER_EN (1 << 18)
  209. #define DWC3_DCTL_CRS (1 << 17)
  210. #define DWC3_DCTL_CSS (1 << 16)
  211. #define DWC3_DCTL_INITU2ENA (1 << 12)
  212. #define DWC3_DCTL_ACCEPTU2ENA (1 << 11)
  213. #define DWC3_DCTL_INITU1ENA (1 << 10)
  214. #define DWC3_DCTL_ACCEPTU1ENA (1 << 9)
  215. #define DWC3_DCTL_TSTCTRL_MASK (0xf << 1)
  216. #define DWC3_DCTL_ULSTCHNGREQ_MASK (0x0f << 5)
  217. #define DWC3_DCTL_ULSTCHNGREQ(n) (((n) << 5) & DWC3_DCTL_ULSTCHNGREQ_MASK)
  218. #define DWC3_DCTL_ULSTCHNG_NO_ACTION (DWC3_DCTL_ULSTCHNGREQ(0))
  219. #define DWC3_DCTL_ULSTCHNG_SS_DISABLED (DWC3_DCTL_ULSTCHNGREQ(4))
  220. #define DWC3_DCTL_ULSTCHNG_RX_DETECT (DWC3_DCTL_ULSTCHNGREQ(5))
  221. #define DWC3_DCTL_ULSTCHNG_SS_INACTIVE (DWC3_DCTL_ULSTCHNGREQ(6))
  222. #define DWC3_DCTL_ULSTCHNG_RECOVERY (DWC3_DCTL_ULSTCHNGREQ(8))
  223. #define DWC3_DCTL_ULSTCHNG_COMPLIANCE (DWC3_DCTL_ULSTCHNGREQ(10))
  224. #define DWC3_DCTL_ULSTCHNG_LOOPBACK (DWC3_DCTL_ULSTCHNGREQ(11))
  225. /* Device Event Enable Register */
  226. #define DWC3_DEVTEN_VNDRDEVTSTRCVEDEN (1 << 12)
  227. #define DWC3_DEVTEN_EVNTOVERFLOWEN (1 << 11)
  228. #define DWC3_DEVTEN_CMDCMPLTEN (1 << 10)
  229. #define DWC3_DEVTEN_ERRTICERREN (1 << 9)
  230. #define DWC3_DEVTEN_SOFEN (1 << 7)
  231. #define DWC3_DEVTEN_EOPFEN (1 << 6)
  232. #define DWC3_DEVTEN_HIBERNATIONREQEVTEN (1 << 5)
  233. #define DWC3_DEVTEN_WKUPEVTEN (1 << 4)
  234. #define DWC3_DEVTEN_ULSTCNGEN (1 << 3)
  235. #define DWC3_DEVTEN_CONNECTDONEEN (1 << 2)
  236. #define DWC3_DEVTEN_USBRSTEN (1 << 1)
  237. #define DWC3_DEVTEN_DISCONNEVTEN (1 << 0)
  238. /* Device Status Register */
  239. #define DWC3_DSTS_DCNRD (1 << 29)
  240. /* This applies for core versions 1.87a and earlier */
  241. #define DWC3_DSTS_PWRUPREQ (1 << 24)
  242. /* These apply for core versions 1.94a and later */
  243. #define DWC3_DSTS_RSS (1 << 25)
  244. #define DWC3_DSTS_SSS (1 << 24)
  245. #define DWC3_DSTS_COREIDLE (1 << 23)
  246. #define DWC3_DSTS_DEVCTRLHLT (1 << 22)
  247. #define DWC3_DSTS_USBLNKST_MASK (0x0f << 18)
  248. #define DWC3_DSTS_USBLNKST(n) (((n) & DWC3_DSTS_USBLNKST_MASK) >> 18)
  249. #define DWC3_DSTS_RXFIFOEMPTY (1 << 17)
  250. #define DWC3_DSTS_SOFFN_MASK (0x3fff << 3)
  251. #define DWC3_DSTS_SOFFN(n) (((n) & DWC3_DSTS_SOFFN_MASK) >> 3)
  252. #define DWC3_DSTS_CONNECTSPD (7 << 0)
  253. #define DWC3_DSTS_SUPERSPEED (4 << 0)
  254. #define DWC3_DSTS_HIGHSPEED (0 << 0)
  255. #define DWC3_DSTS_FULLSPEED2 (1 << 0)
  256. #define DWC3_DSTS_LOWSPEED (2 << 0)
  257. #define DWC3_DSTS_FULLSPEED1 (3 << 0)
  258. /* Device Generic Command Register */
  259. #define DWC3_DGCMD_SET_LMP 0x01
  260. #define DWC3_DGCMD_SET_PERIODIC_PAR 0x02
  261. #define DWC3_DGCMD_XMIT_FUNCTION 0x03
  262. /* These apply for core versions 1.94a and later */
  263. #define DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO 0x04
  264. #define DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI 0x05
  265. #define DWC3_DGCMD_SELECTED_FIFO_FLUSH 0x09
  266. #define DWC3_DGCMD_ALL_FIFO_FLUSH 0x0a
  267. #define DWC3_DGCMD_SET_ENDPOINT_NRDY 0x0c
  268. #define DWC3_DGCMD_RUN_SOC_BUS_LOOPBACK 0x10
  269. #define DWC3_DGCMD_STATUS(n) (((n) >> 15) & 1)
  270. #define DWC3_DGCMD_CMDACT (1 << 10)
  271. #define DWC3_DGCMD_CMDIOC (1 << 8)
  272. /* Device Generic Command Parameter Register */
  273. #define DWC3_DGCMDPAR_FORCE_LINKPM_ACCEPT (1 << 0)
  274. #define DWC3_DGCMDPAR_FIFO_NUM(n) ((n) << 0)
  275. #define DWC3_DGCMDPAR_RX_FIFO (0 << 5)
  276. #define DWC3_DGCMDPAR_TX_FIFO (1 << 5)
  277. #define DWC3_DGCMDPAR_LOOPBACK_DIS (0 << 0)
  278. #define DWC3_DGCMDPAR_LOOPBACK_ENA (1 << 0)
  279. /* Device Endpoint Command Register */
  280. #define DWC3_DEPCMD_PARAM_SHIFT 16
  281. #define DWC3_DEPCMD_PARAM(x) ((x) << DWC3_DEPCMD_PARAM_SHIFT)
  282. #define DWC3_DEPCMD_GET_RSC_IDX(x) (((x) >> DWC3_DEPCMD_PARAM_SHIFT) & 0x7f)
  283. #define DWC3_DEPCMD_STATUS(x) (((x) >> 15) & 1)
  284. #define DWC3_DEPCMD_HIPRI_FORCERM (1 << 11)
  285. #define DWC3_DEPCMD_CMDACT (1 << 10)
  286. #define DWC3_DEPCMD_CMDIOC (1 << 8)
  287. #define DWC3_DEPCMD_DEPSTARTCFG (0x09 << 0)
  288. #define DWC3_DEPCMD_ENDTRANSFER (0x08 << 0)
  289. #define DWC3_DEPCMD_UPDATETRANSFER (0x07 << 0)
  290. #define DWC3_DEPCMD_STARTTRANSFER (0x06 << 0)
  291. #define DWC3_DEPCMD_CLEARSTALL (0x05 << 0)
  292. #define DWC3_DEPCMD_SETSTALL (0x04 << 0)
  293. /* This applies for core versions 1.90a and earlier */
  294. #define DWC3_DEPCMD_GETSEQNUMBER (0x03 << 0)
  295. /* This applies for core versions 1.94a and later */
  296. #define DWC3_DEPCMD_GETEPSTATE (0x03 << 0)
  297. #define DWC3_DEPCMD_SETTRANSFRESOURCE (0x02 << 0)
  298. #define DWC3_DEPCMD_SETEPCONFIG (0x01 << 0)
  299. /* The EP number goes 0..31 so ep0 is always out and ep1 is always in */
  300. #define DWC3_DALEPENA_EP(n) (1 << n)
  301. #define DWC3_DEPCMD_TYPE_CONTROL 0
  302. #define DWC3_DEPCMD_TYPE_ISOC 1
  303. #define DWC3_DEPCMD_TYPE_BULK 2
  304. #define DWC3_DEPCMD_TYPE_INTR 3
  305. /* Structures */
  306. struct dwc3_trb;
  307. /**
  308. * struct dwc3_event_buffer - Software event buffer representation
  309. * @list: a list of event buffers
  310. * @buf: _THE_ buffer
  311. * @length: size of this buffer
  312. * @dma: dma_addr_t
  313. * @dwc: pointer to DWC controller
  314. */
  315. struct dwc3_event_buffer {
  316. void *buf;
  317. unsigned length;
  318. unsigned int lpos;
  319. dma_addr_t dma;
  320. struct dwc3 *dwc;
  321. };
  322. #define DWC3_EP_FLAG_STALLED (1 << 0)
  323. #define DWC3_EP_FLAG_WEDGED (1 << 1)
  324. #define DWC3_EP_DIRECTION_TX true
  325. #define DWC3_EP_DIRECTION_RX false
  326. #define DWC3_TRB_NUM 32
  327. #define DWC3_TRB_MASK (DWC3_TRB_NUM - 1)
  328. /**
  329. * struct dwc3_ep - device side endpoint representation
  330. * @endpoint: usb endpoint
  331. * @request_list: list of requests for this endpoint
  332. * @req_queued: list of requests on this ep which have TRBs setup
  333. * @trb_pool: array of transaction buffers
  334. * @trb_pool_dma: dma address of @trb_pool
  335. * @free_slot: next slot which is going to be used
  336. * @busy_slot: first slot which is owned by HW
  337. * @desc: usb_endpoint_descriptor pointer
  338. * @dwc: pointer to DWC controller
  339. * @flags: endpoint flags (wedged, stalled, ...)
  340. * @current_trb: index of current used trb
  341. * @number: endpoint number (1 - 15)
  342. * @type: set to bmAttributes & USB_ENDPOINT_XFERTYPE_MASK
  343. * @resource_index: Resource transfer index
  344. * @interval: the intervall on which the ISOC transfer is started
  345. * @name: a human readable name e.g. ep1out-bulk
  346. * @direction: true for TX, false for RX
  347. * @stream_capable: true when streams are enabled
  348. */
  349. struct dwc3_ep {
  350. struct usb_ep endpoint;
  351. struct list_head request_list;
  352. struct list_head req_queued;
  353. struct dwc3_trb *trb_pool;
  354. dma_addr_t trb_pool_dma;
  355. u32 free_slot;
  356. u32 busy_slot;
  357. const struct usb_ss_ep_comp_descriptor *comp_desc;
  358. struct dwc3 *dwc;
  359. unsigned flags;
  360. #define DWC3_EP_ENABLED (1 << 0)
  361. #define DWC3_EP_STALL (1 << 1)
  362. #define DWC3_EP_WEDGE (1 << 2)
  363. #define DWC3_EP_BUSY (1 << 4)
  364. #define DWC3_EP_PENDING_REQUEST (1 << 5)
  365. #define DWC3_EP_MISSED_ISOC (1 << 6)
  366. /* This last one is specific to EP0 */
  367. #define DWC3_EP0_DIR_IN (1 << 31)
  368. unsigned current_trb;
  369. u8 number;
  370. u8 type;
  371. u8 resource_index;
  372. u32 interval;
  373. char name[20];
  374. unsigned direction:1;
  375. unsigned stream_capable:1;
  376. };
  377. enum dwc3_phy {
  378. DWC3_PHY_UNKNOWN = 0,
  379. DWC3_PHY_USB3,
  380. DWC3_PHY_USB2,
  381. };
  382. enum dwc3_ep0_next {
  383. DWC3_EP0_UNKNOWN = 0,
  384. DWC3_EP0_COMPLETE,
  385. DWC3_EP0_NRDY_DATA,
  386. DWC3_EP0_NRDY_STATUS,
  387. };
  388. enum dwc3_ep0_state {
  389. EP0_UNCONNECTED = 0,
  390. EP0_SETUP_PHASE,
  391. EP0_DATA_PHASE,
  392. EP0_STATUS_PHASE,
  393. };
  394. enum dwc3_link_state {
  395. /* In SuperSpeed */
  396. DWC3_LINK_STATE_U0 = 0x00, /* in HS, means ON */
  397. DWC3_LINK_STATE_U1 = 0x01,
  398. DWC3_LINK_STATE_U2 = 0x02, /* in HS, means SLEEP */
  399. DWC3_LINK_STATE_U3 = 0x03, /* in HS, means SUSPEND */
  400. DWC3_LINK_STATE_SS_DIS = 0x04,
  401. DWC3_LINK_STATE_RX_DET = 0x05, /* in HS, means Early Suspend */
  402. DWC3_LINK_STATE_SS_INACT = 0x06,
  403. DWC3_LINK_STATE_POLL = 0x07,
  404. DWC3_LINK_STATE_RECOV = 0x08,
  405. DWC3_LINK_STATE_HRESET = 0x09,
  406. DWC3_LINK_STATE_CMPLY = 0x0a,
  407. DWC3_LINK_STATE_LPBK = 0x0b,
  408. DWC3_LINK_STATE_RESET = 0x0e,
  409. DWC3_LINK_STATE_RESUME = 0x0f,
  410. DWC3_LINK_STATE_MASK = 0x0f,
  411. };
  412. enum dwc3_device_state {
  413. DWC3_DEFAULT_STATE,
  414. DWC3_ADDRESS_STATE,
  415. DWC3_CONFIGURED_STATE,
  416. };
  417. /* TRB Length, PCM and Status */
  418. #define DWC3_TRB_SIZE_MASK (0x00ffffff)
  419. #define DWC3_TRB_SIZE_LENGTH(n) ((n) & DWC3_TRB_SIZE_MASK)
  420. #define DWC3_TRB_SIZE_PCM1(n) (((n) & 0x03) << 24)
  421. #define DWC3_TRB_SIZE_TRBSTS(n) (((n) & (0x0f << 28)) >> 28)
  422. #define DWC3_TRBSTS_OK 0
  423. #define DWC3_TRBSTS_MISSED_ISOC 1
  424. #define DWC3_TRBSTS_SETUP_PENDING 2
  425. #define DWC3_TRB_STS_XFER_IN_PROG 4
  426. /* TRB Control */
  427. #define DWC3_TRB_CTRL_HWO (1 << 0)
  428. #define DWC3_TRB_CTRL_LST (1 << 1)
  429. #define DWC3_TRB_CTRL_CHN (1 << 2)
  430. #define DWC3_TRB_CTRL_CSP (1 << 3)
  431. #define DWC3_TRB_CTRL_TRBCTL(n) (((n) & 0x3f) << 4)
  432. #define DWC3_TRB_CTRL_ISP_IMI (1 << 10)
  433. #define DWC3_TRB_CTRL_IOC (1 << 11)
  434. #define DWC3_TRB_CTRL_SID_SOFN(n) (((n) & 0xffff) << 14)
  435. #define DWC3_TRBCTL_NORMAL DWC3_TRB_CTRL_TRBCTL(1)
  436. #define DWC3_TRBCTL_CONTROL_SETUP DWC3_TRB_CTRL_TRBCTL(2)
  437. #define DWC3_TRBCTL_CONTROL_STATUS2 DWC3_TRB_CTRL_TRBCTL(3)
  438. #define DWC3_TRBCTL_CONTROL_STATUS3 DWC3_TRB_CTRL_TRBCTL(4)
  439. #define DWC3_TRBCTL_CONTROL_DATA DWC3_TRB_CTRL_TRBCTL(5)
  440. #define DWC3_TRBCTL_ISOCHRONOUS_FIRST DWC3_TRB_CTRL_TRBCTL(6)
  441. #define DWC3_TRBCTL_ISOCHRONOUS DWC3_TRB_CTRL_TRBCTL(7)
  442. #define DWC3_TRBCTL_LINK_TRB DWC3_TRB_CTRL_TRBCTL(8)
  443. /**
  444. * struct dwc3_trb - transfer request block (hw format)
  445. * @bpl: DW0-3
  446. * @bph: DW4-7
  447. * @size: DW8-B
  448. * @trl: DWC-F
  449. */
  450. struct dwc3_trb {
  451. u32 bpl;
  452. u32 bph;
  453. u32 size;
  454. u32 ctrl;
  455. } __packed;
  456. /**
  457. * dwc3_hwparams - copy of HWPARAMS registers
  458. * @hwparams0 - GHWPARAMS0
  459. * @hwparams1 - GHWPARAMS1
  460. * @hwparams2 - GHWPARAMS2
  461. * @hwparams3 - GHWPARAMS3
  462. * @hwparams4 - GHWPARAMS4
  463. * @hwparams5 - GHWPARAMS5
  464. * @hwparams6 - GHWPARAMS6
  465. * @hwparams7 - GHWPARAMS7
  466. * @hwparams8 - GHWPARAMS8
  467. */
  468. struct dwc3_hwparams {
  469. u32 hwparams0;
  470. u32 hwparams1;
  471. u32 hwparams2;
  472. u32 hwparams3;
  473. u32 hwparams4;
  474. u32 hwparams5;
  475. u32 hwparams6;
  476. u32 hwparams7;
  477. u32 hwparams8;
  478. };
  479. /* HWPARAMS0 */
  480. #define DWC3_MODE(n) ((n) & 0x7)
  481. #define DWC3_MODE_DEVICE 0
  482. #define DWC3_MODE_HOST 1
  483. #define DWC3_MODE_DRD 2
  484. #define DWC3_MODE_HUB 3
  485. #define DWC3_MDWIDTH(n) (((n) & 0xff00) >> 8)
  486. /* HWPARAMS1 */
  487. #define DWC3_NUM_INT(n) (((n) & (0x3f << 15)) >> 15)
  488. /* HWPARAMS7 */
  489. #define DWC3_RAM1_DEPTH(n) ((n) & 0xffff)
  490. struct dwc3_request {
  491. struct usb_request request;
  492. struct list_head list;
  493. struct dwc3_ep *dep;
  494. u32 start_slot;
  495. u8 epnum;
  496. struct dwc3_trb *trb;
  497. dma_addr_t trb_dma;
  498. unsigned direction:1;
  499. unsigned mapped:1;
  500. unsigned queued:1;
  501. };
  502. /*
  503. * struct dwc3_scratchpad_array - hibernation scratchpad array
  504. * (format defined by hw)
  505. */
  506. struct dwc3_scratchpad_array {
  507. __le64 dma_adr[DWC3_MAX_HIBER_SCRATCHBUFS];
  508. };
  509. /**
  510. * struct dwc3 - representation of our controller
  511. * @ctrl_req: usb control request which is used for ep0
  512. * @ep0_trb: trb which is used for the ctrl_req
  513. * @ep0_bounce: bounce buffer for ep0
  514. * @setup_buf: used while precessing STD USB requests
  515. * @ctrl_req_addr: dma address of ctrl_req
  516. * @ep0_trb: dma address of ep0_trb
  517. * @ep0_usb_req: dummy req used while handling STD USB requests
  518. * @ep0_bounce_addr: dma address of ep0_bounce
  519. * @lock: for synchronizing
  520. * @dev: pointer to our struct device
  521. * @xhci: pointer to our xHCI child
  522. * @event_buffer_list: a list of event buffers
  523. * @gadget: device side representation of the peripheral controller
  524. * @gadget_driver: pointer to the gadget driver
  525. * @regs: base address for our registers
  526. * @regs_size: address space size
  527. * @irq: IRQ number
  528. * @num_event_buffers: calculated number of event buffers
  529. * @u1u2: only used on revisions <1.83a for workaround
  530. * @maximum_speed: maximum speed requested (mainly for testing purposes)
  531. * @revision: revision register contents
  532. * @mode: mode of operation
  533. * @usb2_phy: pointer to USB2 PHY
  534. * @usb3_phy: pointer to USB3 PHY
  535. * @is_selfpowered: true when we are selfpowered
  536. * @three_stage_setup: set if we perform a three phase setup
  537. * @ep0_bounced: true when we used bounce buffer
  538. * @ep0_expect_in: true when we expect a DATA IN transfer
  539. * @start_config_issued: true when StartConfig command has been issued
  540. * @setup_packet_pending: true when there's a Setup Packet in FIFO. Workaround
  541. * @needs_fifo_resize: not all users might want fifo resizing, flag it
  542. * @resize_fifos: tells us it's ok to reconfigure our TxFIFO sizes.
  543. * @isoch_delay: wValue from Set Isochronous Delay request;
  544. * @u2sel: parameter from Set SEL request.
  545. * @u2pel: parameter from Set SEL request.
  546. * @u1sel: parameter from Set SEL request.
  547. * @u1pel: parameter from Set SEL request.
  548. * @ep0_next_event: hold the next expected event
  549. * @ep0state: state of endpoint zero
  550. * @link_state: link state
  551. * @speed: device speed (super, high, full, low)
  552. * @mem: points to start of memory which is used for this struct.
  553. * @hwparams: copy of hwparams registers
  554. * @root: debugfs root folder pointer
  555. */
  556. struct dwc3 {
  557. struct usb_ctrlrequest *ctrl_req;
  558. struct dwc3_trb *ep0_trb;
  559. void *ep0_bounce;
  560. u8 *setup_buf;
  561. dma_addr_t ctrl_req_addr;
  562. dma_addr_t ep0_trb_addr;
  563. dma_addr_t ep0_bounce_addr;
  564. struct dwc3_request ep0_usb_req;
  565. /* device lock */
  566. spinlock_t lock;
  567. struct device *dev;
  568. struct platform_device *xhci;
  569. struct resource xhci_resources[DWC3_XHCI_RESOURCES_NUM];
  570. struct dwc3_event_buffer **ev_buffs;
  571. struct dwc3_ep *eps[DWC3_ENDPOINTS_NUM];
  572. struct usb_gadget gadget;
  573. struct usb_gadget_driver *gadget_driver;
  574. struct usb_phy *usb2_phy;
  575. struct usb_phy *usb3_phy;
  576. void __iomem *regs;
  577. size_t regs_size;
  578. u32 num_event_buffers;
  579. u32 u1u2;
  580. u32 maximum_speed;
  581. u32 revision;
  582. u32 mode;
  583. #define DWC3_REVISION_173A 0x5533173a
  584. #define DWC3_REVISION_175A 0x5533175a
  585. #define DWC3_REVISION_180A 0x5533180a
  586. #define DWC3_REVISION_183A 0x5533183a
  587. #define DWC3_REVISION_185A 0x5533185a
  588. #define DWC3_REVISION_187A 0x5533187a
  589. #define DWC3_REVISION_188A 0x5533188a
  590. #define DWC3_REVISION_190A 0x5533190a
  591. #define DWC3_REVISION_194A 0x5533194a
  592. #define DWC3_REVISION_200A 0x5533200a
  593. #define DWC3_REVISION_202A 0x5533202a
  594. #define DWC3_REVISION_210A 0x5533210a
  595. #define DWC3_REVISION_220A 0x5533220a
  596. unsigned is_selfpowered:1;
  597. unsigned three_stage_setup:1;
  598. unsigned ep0_bounced:1;
  599. unsigned ep0_expect_in:1;
  600. unsigned start_config_issued:1;
  601. unsigned setup_packet_pending:1;
  602. unsigned delayed_status:1;
  603. unsigned needs_fifo_resize:1;
  604. unsigned resize_fifos:1;
  605. enum dwc3_ep0_next ep0_next_event;
  606. enum dwc3_ep0_state ep0state;
  607. enum dwc3_link_state link_state;
  608. enum dwc3_device_state dev_state;
  609. u16 isoch_delay;
  610. u16 u2sel;
  611. u16 u2pel;
  612. u8 u1sel;
  613. u8 u1pel;
  614. u8 speed;
  615. void *mem;
  616. struct dwc3_hwparams hwparams;
  617. struct dentry *root;
  618. struct debugfs_regset32 *regset;
  619. u8 test_mode;
  620. u8 test_mode_nr;
  621. };
  622. /* -------------------------------------------------------------------------- */
  623. /* -------------------------------------------------------------------------- */
  624. struct dwc3_event_type {
  625. u32 is_devspec:1;
  626. u32 type:6;
  627. u32 reserved8_31:25;
  628. } __packed;
  629. #define DWC3_DEPEVT_XFERCOMPLETE 0x01
  630. #define DWC3_DEPEVT_XFERINPROGRESS 0x02
  631. #define DWC3_DEPEVT_XFERNOTREADY 0x03
  632. #define DWC3_DEPEVT_RXTXFIFOEVT 0x04
  633. #define DWC3_DEPEVT_STREAMEVT 0x06
  634. #define DWC3_DEPEVT_EPCMDCMPLT 0x07
  635. /**
  636. * struct dwc3_event_depvt - Device Endpoint Events
  637. * @one_bit: indicates this is an endpoint event (not used)
  638. * @endpoint_number: number of the endpoint
  639. * @endpoint_event: The event we have:
  640. * 0x00 - Reserved
  641. * 0x01 - XferComplete
  642. * 0x02 - XferInProgress
  643. * 0x03 - XferNotReady
  644. * 0x04 - RxTxFifoEvt (IN->Underrun, OUT->Overrun)
  645. * 0x05 - Reserved
  646. * 0x06 - StreamEvt
  647. * 0x07 - EPCmdCmplt
  648. * @reserved11_10: Reserved, don't use.
  649. * @status: Indicates the status of the event. Refer to databook for
  650. * more information.
  651. * @parameters: Parameters of the current event. Refer to databook for
  652. * more information.
  653. */
  654. struct dwc3_event_depevt {
  655. u32 one_bit:1;
  656. u32 endpoint_number:5;
  657. u32 endpoint_event:4;
  658. u32 reserved11_10:2;
  659. u32 status:4;
  660. /* Within XferNotReady */
  661. #define DEPEVT_STATUS_TRANSFER_ACTIVE (1 << 3)
  662. /* Within XferComplete */
  663. #define DEPEVT_STATUS_BUSERR (1 << 0)
  664. #define DEPEVT_STATUS_SHORT (1 << 1)
  665. #define DEPEVT_STATUS_IOC (1 << 2)
  666. #define DEPEVT_STATUS_LST (1 << 3)
  667. /* Stream event only */
  668. #define DEPEVT_STREAMEVT_FOUND 1
  669. #define DEPEVT_STREAMEVT_NOTFOUND 2
  670. /* Control-only Status */
  671. #define DEPEVT_STATUS_CONTROL_DATA 1
  672. #define DEPEVT_STATUS_CONTROL_STATUS 2
  673. u32 parameters:16;
  674. } __packed;
  675. /**
  676. * struct dwc3_event_devt - Device Events
  677. * @one_bit: indicates this is a non-endpoint event (not used)
  678. * @device_event: indicates it's a device event. Should read as 0x00
  679. * @type: indicates the type of device event.
  680. * 0 - DisconnEvt
  681. * 1 - USBRst
  682. * 2 - ConnectDone
  683. * 3 - ULStChng
  684. * 4 - WkUpEvt
  685. * 5 - Reserved
  686. * 6 - EOPF
  687. * 7 - SOF
  688. * 8 - Reserved
  689. * 9 - ErrticErr
  690. * 10 - CmdCmplt
  691. * 11 - EvntOverflow
  692. * 12 - VndrDevTstRcved
  693. * @reserved15_12: Reserved, not used
  694. * @event_info: Information about this event
  695. * @reserved31_24: Reserved, not used
  696. */
  697. struct dwc3_event_devt {
  698. u32 one_bit:1;
  699. u32 device_event:7;
  700. u32 type:4;
  701. u32 reserved15_12:4;
  702. u32 event_info:8;
  703. u32 reserved31_24:8;
  704. } __packed;
  705. /**
  706. * struct dwc3_event_gevt - Other Core Events
  707. * @one_bit: indicates this is a non-endpoint event (not used)
  708. * @device_event: indicates it's (0x03) Carkit or (0x04) I2C event.
  709. * @phy_port_number: self-explanatory
  710. * @reserved31_12: Reserved, not used.
  711. */
  712. struct dwc3_event_gevt {
  713. u32 one_bit:1;
  714. u32 device_event:7;
  715. u32 phy_port_number:4;
  716. u32 reserved31_12:20;
  717. } __packed;
  718. /**
  719. * union dwc3_event - representation of Event Buffer contents
  720. * @raw: raw 32-bit event
  721. * @type: the type of the event
  722. * @depevt: Device Endpoint Event
  723. * @devt: Device Event
  724. * @gevt: Global Event
  725. */
  726. union dwc3_event {
  727. u32 raw;
  728. struct dwc3_event_type type;
  729. struct dwc3_event_depevt depevt;
  730. struct dwc3_event_devt devt;
  731. struct dwc3_event_gevt gevt;
  732. };
  733. /*
  734. * DWC3 Features to be used as Driver Data
  735. */
  736. #define DWC3_HAS_PERIPHERAL BIT(0)
  737. #define DWC3_HAS_XHCI BIT(1)
  738. #define DWC3_HAS_OTG BIT(3)
  739. /* prototypes */
  740. void dwc3_set_mode(struct dwc3 *dwc, u32 mode);
  741. int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc);
  742. #if IS_ENABLED(CONFIG_USB_DWC3_HOST) || IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE)
  743. int dwc3_host_init(struct dwc3 *dwc);
  744. void dwc3_host_exit(struct dwc3 *dwc);
  745. #else
  746. static inline int dwc3_host_init(struct dwc3 *dwc)
  747. { return 0; }
  748. static inline void dwc3_host_exit(struct dwc3 *dwc)
  749. { }
  750. #endif
  751. #if IS_ENABLED(CONFIG_USB_DWC3_GADGET) || IS_ENABLED(CONFIG_USB_DWC3_DUAL_ROLE)
  752. int dwc3_gadget_init(struct dwc3 *dwc);
  753. void dwc3_gadget_exit(struct dwc3 *dwc);
  754. #else
  755. static inline int dwc3_gadget_init(struct dwc3 *dwc)
  756. { return 0; }
  757. static inline void dwc3_gadget_exit(struct dwc3 *dwc)
  758. { }
  759. #endif
  760. #endif /* __DRIVERS_USB_DWC3_CORE_H */