core.h 24 KB

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