core.h 24 KB

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