core.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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 HWPARAMS1 Register */
  152. #define DWC3_GHWPARAMS1_EN_PWROPT(n) ((n & (3 << 24)) >> 24)
  153. #define DWC3_GHWPARAMS1_EN_PWROPT_NO 0
  154. #define DWC3_GHWPARAMS1_EN_PWROPT_CLK 1
  155. /* Device Configuration Register */
  156. #define DWC3_DCFG_DEVADDR(addr) ((addr) << 3)
  157. #define DWC3_DCFG_DEVADDR_MASK DWC3_DCFG_DEVADDR(0x7f)
  158. #define DWC3_DCFG_SPEED_MASK (7 << 0)
  159. #define DWC3_DCFG_SUPERSPEED (4 << 0)
  160. #define DWC3_DCFG_HIGHSPEED (0 << 0)
  161. #define DWC3_DCFG_FULLSPEED2 (1 << 0)
  162. #define DWC3_DCFG_LOWSPEED (2 << 0)
  163. #define DWC3_DCFG_FULLSPEED1 (3 << 0)
  164. /* Device Control Register */
  165. #define DWC3_DCTL_RUN_STOP (1 << 31)
  166. #define DWC3_DCTL_CSFTRST (1 << 30)
  167. #define DWC3_DCTL_LSFTRST (1 << 29)
  168. #define DWC3_DCTL_HIRD_THRES_MASK (0x1f << 24)
  169. #define DWC3_DCTL_HIRD_THRES(n) (((n) & DWC3_DCTL_HIRD_THRES_MASK) >> 24)
  170. #define DWC3_DCTL_APPL1RES (1 << 23)
  171. #define DWC3_DCTL_INITU2ENA (1 << 12)
  172. #define DWC3_DCTL_ACCEPTU2ENA (1 << 11)
  173. #define DWC3_DCTL_INITU1ENA (1 << 10)
  174. #define DWC3_DCTL_ACCEPTU1ENA (1 << 9)
  175. #define DWC3_DCTL_TSTCTRL_MASK (0xf << 1)
  176. #define DWC3_DCTL_ULSTCHNGREQ_MASK (0x0f << 5)
  177. #define DWC3_DCTL_ULSTCHNGREQ(n) (((n) << 5) & DWC3_DCTL_ULSTCHNGREQ_MASK)
  178. #define DWC3_DCTL_ULSTCHNG_NO_ACTION (DWC3_DCTL_ULSTCHNGREQ(0))
  179. #define DWC3_DCTL_ULSTCHNG_SS_DISABLED (DWC3_DCTL_ULSTCHNGREQ(4))
  180. #define DWC3_DCTL_ULSTCHNG_RX_DETECT (DWC3_DCTL_ULSTCHNGREQ(5))
  181. #define DWC3_DCTL_ULSTCHNG_SS_INACTIVE (DWC3_DCTL_ULSTCHNGREQ(6))
  182. #define DWC3_DCTL_ULSTCHNG_RECOVERY (DWC3_DCTL_ULSTCHNGREQ(8))
  183. #define DWC3_DCTL_ULSTCHNG_COMPLIANCE (DWC3_DCTL_ULSTCHNGREQ(10))
  184. #define DWC3_DCTL_ULSTCHNG_LOOPBACK (DWC3_DCTL_ULSTCHNGREQ(11))
  185. /* Device Event Enable Register */
  186. #define DWC3_DEVTEN_VNDRDEVTSTRCVEDEN (1 << 12)
  187. #define DWC3_DEVTEN_EVNTOVERFLOWEN (1 << 11)
  188. #define DWC3_DEVTEN_CMDCMPLTEN (1 << 10)
  189. #define DWC3_DEVTEN_ERRTICERREN (1 << 9)
  190. #define DWC3_DEVTEN_SOFEN (1 << 7)
  191. #define DWC3_DEVTEN_EOPFEN (1 << 6)
  192. #define DWC3_DEVTEN_WKUPEVTEN (1 << 4)
  193. #define DWC3_DEVTEN_ULSTCNGEN (1 << 3)
  194. #define DWC3_DEVTEN_CONNECTDONEEN (1 << 2)
  195. #define DWC3_DEVTEN_USBRSTEN (1 << 1)
  196. #define DWC3_DEVTEN_DISCONNEVTEN (1 << 0)
  197. /* Device Status Register */
  198. #define DWC3_DSTS_PWRUPREQ (1 << 24)
  199. #define DWC3_DSTS_COREIDLE (1 << 23)
  200. #define DWC3_DSTS_DEVCTRLHLT (1 << 22)
  201. #define DWC3_DSTS_USBLNKST_MASK (0x0f << 18)
  202. #define DWC3_DSTS_USBLNKST(n) (((n) & DWC3_DSTS_USBLNKST_MASK) >> 18)
  203. #define DWC3_DSTS_RXFIFOEMPTY (1 << 17)
  204. #define DWC3_DSTS_SOFFN_MASK (0x3ff << 3)
  205. #define DWC3_DSTS_SOFFN(n) (((n) & DWC3_DSTS_SOFFN_MASK) >> 3)
  206. #define DWC3_DSTS_CONNECTSPD (7 << 0)
  207. #define DWC3_DSTS_SUPERSPEED (4 << 0)
  208. #define DWC3_DSTS_HIGHSPEED (0 << 0)
  209. #define DWC3_DSTS_FULLSPEED2 (1 << 0)
  210. #define DWC3_DSTS_LOWSPEED (2 << 0)
  211. #define DWC3_DSTS_FULLSPEED1 (3 << 0)
  212. /* Device Generic Command Register */
  213. #define DWC3_DGCMD_SET_LMP 0x01
  214. #define DWC3_DGCMD_SET_PERIODIC_PAR 0x02
  215. #define DWC3_DGCMD_XMIT_FUNCTION 0x03
  216. #define DWC3_DGCMD_SELECTED_FIFO_FLUSH 0x09
  217. #define DWC3_DGCMD_ALL_FIFO_FLUSH 0x0a
  218. #define DWC3_DGCMD_SET_ENDPOINT_NRDY 0x0c
  219. #define DWC3_DGCMD_RUN_SOC_BUS_LOOPBACK 0x10
  220. /* Device Endpoint Command Register */
  221. #define DWC3_DEPCMD_PARAM_SHIFT 16
  222. #define DWC3_DEPCMD_PARAM(x) (x << DWC3_DEPCMD_PARAM_SHIFT)
  223. #define DWC3_DEPCMD_GET_RSC_IDX(x) ((x >> DWC3_DEPCMD_PARAM_SHIFT) & 0x7f)
  224. #define DWC3_DEPCMD_STATUS_MASK (0x0f << 12)
  225. #define DWC3_DEPCMD_STATUS(x) ((x & DWC3_DEPCMD_STATUS_MASK) >> 12)
  226. #define DWC3_DEPCMD_HIPRI_FORCERM (1 << 11)
  227. #define DWC3_DEPCMD_CMDACT (1 << 10)
  228. #define DWC3_DEPCMD_CMDIOC (1 << 8)
  229. #define DWC3_DEPCMD_DEPSTARTCFG (0x09 << 0)
  230. #define DWC3_DEPCMD_ENDTRANSFER (0x08 << 0)
  231. #define DWC3_DEPCMD_UPDATETRANSFER (0x07 << 0)
  232. #define DWC3_DEPCMD_STARTTRANSFER (0x06 << 0)
  233. #define DWC3_DEPCMD_CLEARSTALL (0x05 << 0)
  234. #define DWC3_DEPCMD_SETSTALL (0x04 << 0)
  235. #define DWC3_DEPCMD_GETSEQNUMBER (0x03 << 0)
  236. #define DWC3_DEPCMD_SETTRANSFRESOURCE (0x02 << 0)
  237. #define DWC3_DEPCMD_SETEPCONFIG (0x01 << 0)
  238. /* The EP number goes 0..31 so ep0 is always out and ep1 is always in */
  239. #define DWC3_DALEPENA_EP(n) (1 << n)
  240. #define DWC3_DEPCMD_TYPE_CONTROL 0
  241. #define DWC3_DEPCMD_TYPE_ISOC 1
  242. #define DWC3_DEPCMD_TYPE_BULK 2
  243. #define DWC3_DEPCMD_TYPE_INTR 3
  244. /* Structures */
  245. struct dwc3_trb_hw;
  246. /**
  247. * struct dwc3_event_buffer - Software event buffer representation
  248. * @list: a list of event buffers
  249. * @buf: _THE_ buffer
  250. * @length: size of this buffer
  251. * @dma: dma_addr_t
  252. * @dwc: pointer to DWC controller
  253. */
  254. struct dwc3_event_buffer {
  255. void *buf;
  256. unsigned length;
  257. unsigned int lpos;
  258. dma_addr_t dma;
  259. struct dwc3 *dwc;
  260. };
  261. #define DWC3_EP_FLAG_STALLED (1 << 0)
  262. #define DWC3_EP_FLAG_WEDGED (1 << 1)
  263. #define DWC3_EP_DIRECTION_TX true
  264. #define DWC3_EP_DIRECTION_RX false
  265. #define DWC3_TRB_NUM 32
  266. #define DWC3_TRB_MASK (DWC3_TRB_NUM - 1)
  267. /**
  268. * struct dwc3_ep - device side endpoint representation
  269. * @endpoint: usb endpoint
  270. * @request_list: list of requests for this endpoint
  271. * @req_queued: list of requests on this ep which have TRBs setup
  272. * @trb_pool: array of transaction buffers
  273. * @trb_pool_dma: dma address of @trb_pool
  274. * @free_slot: next slot which is going to be used
  275. * @busy_slot: first slot which is owned by HW
  276. * @desc: usb_endpoint_descriptor pointer
  277. * @dwc: pointer to DWC controller
  278. * @flags: endpoint flags (wedged, stalled, ...)
  279. * @current_trb: index of current used trb
  280. * @number: endpoint number (1 - 15)
  281. * @type: set to bmAttributes & USB_ENDPOINT_XFERTYPE_MASK
  282. * @res_trans_idx: Resource transfer index
  283. * @interval: the intervall on which the ISOC transfer is started
  284. * @name: a human readable name e.g. ep1out-bulk
  285. * @direction: true for TX, false for RX
  286. * @stream_capable: true when streams are enabled
  287. */
  288. struct dwc3_ep {
  289. struct usb_ep endpoint;
  290. struct list_head request_list;
  291. struct list_head req_queued;
  292. struct dwc3_trb_hw *trb_pool;
  293. dma_addr_t trb_pool_dma;
  294. u32 free_slot;
  295. u32 busy_slot;
  296. const struct usb_endpoint_descriptor *desc;
  297. const struct usb_ss_ep_comp_descriptor *comp_desc;
  298. struct dwc3 *dwc;
  299. unsigned flags;
  300. #define DWC3_EP_ENABLED (1 << 0)
  301. #define DWC3_EP_STALL (1 << 1)
  302. #define DWC3_EP_WEDGE (1 << 2)
  303. #define DWC3_EP_BUSY (1 << 4)
  304. #define DWC3_EP_PENDING_REQUEST (1 << 5)
  305. /* This last one is specific to EP0 */
  306. #define DWC3_EP0_DIR_IN (1 << 31)
  307. unsigned current_trb;
  308. u8 number;
  309. u8 type;
  310. u8 res_trans_idx;
  311. u32 interval;
  312. char name[20];
  313. unsigned direction:1;
  314. unsigned stream_capable:1;
  315. };
  316. enum dwc3_phy {
  317. DWC3_PHY_UNKNOWN = 0,
  318. DWC3_PHY_USB3,
  319. DWC3_PHY_USB2,
  320. };
  321. enum dwc3_ep0_next {
  322. DWC3_EP0_UNKNOWN = 0,
  323. DWC3_EP0_COMPLETE,
  324. DWC3_EP0_NRDY_SETUP,
  325. DWC3_EP0_NRDY_DATA,
  326. DWC3_EP0_NRDY_STATUS,
  327. };
  328. enum dwc3_ep0_state {
  329. EP0_UNCONNECTED = 0,
  330. EP0_SETUP_PHASE,
  331. EP0_DATA_PHASE,
  332. EP0_STATUS_PHASE,
  333. };
  334. enum dwc3_link_state {
  335. /* In SuperSpeed */
  336. DWC3_LINK_STATE_U0 = 0x00, /* in HS, means ON */
  337. DWC3_LINK_STATE_U1 = 0x01,
  338. DWC3_LINK_STATE_U2 = 0x02, /* in HS, means SLEEP */
  339. DWC3_LINK_STATE_U3 = 0x03, /* in HS, means SUSPEND */
  340. DWC3_LINK_STATE_SS_DIS = 0x04,
  341. DWC3_LINK_STATE_RX_DET = 0x05, /* in HS, means Early Suspend */
  342. DWC3_LINK_STATE_SS_INACT = 0x06,
  343. DWC3_LINK_STATE_POLL = 0x07,
  344. DWC3_LINK_STATE_RECOV = 0x08,
  345. DWC3_LINK_STATE_HRESET = 0x09,
  346. DWC3_LINK_STATE_CMPLY = 0x0a,
  347. DWC3_LINK_STATE_LPBK = 0x0b,
  348. DWC3_LINK_STATE_MASK = 0x0f,
  349. };
  350. enum dwc3_device_state {
  351. DWC3_DEFAULT_STATE,
  352. DWC3_ADDRESS_STATE,
  353. DWC3_CONFIGURED_STATE,
  354. };
  355. /**
  356. * struct dwc3_trb - transfer request block
  357. * @bpl: lower 32bit of the buffer
  358. * @bph: higher 32bit of the buffer
  359. * @length: buffer size (up to 16mb - 1)
  360. * @pcm1: packet count m1
  361. * @trbsts: trb status
  362. * 0 = ok
  363. * 1 = missed isoc
  364. * 2 = setup pending
  365. * @hwo: hardware owner of descriptor
  366. * @lst: last trb
  367. * @chn: chain buffers
  368. * @csp: continue on short packets (only supported on isoc eps)
  369. * @trbctl: trb control
  370. * 1 = normal
  371. * 2 = control-setup
  372. * 3 = control-status-2
  373. * 4 = control-status-3
  374. * 5 = control-data (first trb of data stage)
  375. * 6 = isochronous-first (first trb of service interval)
  376. * 7 = isochronous
  377. * 8 = link trb
  378. * others = reserved
  379. * @isp_imi: interrupt on short packet / interrupt on missed isoc
  380. * @ioc: interrupt on complete
  381. * @sid_sofn: Stream ID / SOF Number
  382. */
  383. struct dwc3_trb {
  384. u64 bplh;
  385. union {
  386. struct {
  387. u32 length:24;
  388. u32 pcm1:2;
  389. u32 reserved27_26:2;
  390. u32 trbsts:4;
  391. #define DWC3_TRB_STS_OKAY 0
  392. #define DWC3_TRB_STS_MISSED_ISOC 1
  393. #define DWC3_TRB_STS_SETUP_PENDING 2
  394. };
  395. u32 len_pcm;
  396. };
  397. union {
  398. struct {
  399. u32 hwo:1;
  400. u32 lst:1;
  401. u32 chn:1;
  402. u32 csp:1;
  403. u32 trbctl:6;
  404. u32 isp_imi:1;
  405. u32 ioc:1;
  406. u32 reserved13_12:2;
  407. u32 sid_sofn:16;
  408. u32 reserved31_30:2;
  409. };
  410. u32 control;
  411. };
  412. } __packed;
  413. /**
  414. * struct dwc3_trb_hw - transfer request block (hw format)
  415. * @bpl: DW0-3
  416. * @bph: DW4-7
  417. * @size: DW8-B
  418. * @trl: DWC-F
  419. */
  420. struct dwc3_trb_hw {
  421. __le32 bpl;
  422. __le32 bph;
  423. __le32 size;
  424. __le32 ctrl;
  425. } __packed;
  426. static inline void dwc3_trb_to_hw(struct dwc3_trb *nat, struct dwc3_trb_hw *hw)
  427. {
  428. hw->bpl = cpu_to_le32(lower_32_bits(nat->bplh));
  429. hw->bph = cpu_to_le32(upper_32_bits(nat->bplh));
  430. hw->size = cpu_to_le32p(&nat->len_pcm);
  431. /* HWO is written last */
  432. hw->ctrl = cpu_to_le32p(&nat->control);
  433. }
  434. static inline void dwc3_trb_to_nat(struct dwc3_trb_hw *hw, struct dwc3_trb *nat)
  435. {
  436. u64 bplh;
  437. bplh = le32_to_cpup(&hw->bpl);
  438. bplh |= (u64) le32_to_cpup(&hw->bph) << 32;
  439. nat->bplh = bplh;
  440. nat->len_pcm = le32_to_cpup(&hw->size);
  441. nat->control = le32_to_cpup(&hw->ctrl);
  442. }
  443. /**
  444. * dwc3_hwparams - copy of HWPARAMS registers
  445. * @hwparams0 - GHWPARAMS0
  446. * @hwparams1 - GHWPARAMS1
  447. * @hwparams2 - GHWPARAMS2
  448. * @hwparams3 - GHWPARAMS3
  449. * @hwparams4 - GHWPARAMS4
  450. * @hwparams5 - GHWPARAMS5
  451. * @hwparams6 - GHWPARAMS6
  452. * @hwparams7 - GHWPARAMS7
  453. * @hwparams8 - GHWPARAMS8
  454. */
  455. struct dwc3_hwparams {
  456. u32 hwparams0;
  457. u32 hwparams1;
  458. u32 hwparams2;
  459. u32 hwparams3;
  460. u32 hwparams4;
  461. u32 hwparams5;
  462. u32 hwparams6;
  463. u32 hwparams7;
  464. u32 hwparams8;
  465. };
  466. /* HWPARAMS0 */
  467. #define DWC3_MODE(n) ((n) & 0x7)
  468. #define DWC3_MODE_DEVICE 0
  469. #define DWC3_MODE_HOST 1
  470. #define DWC3_MODE_DRD 2
  471. #define DWC3_MODE_HUB 3
  472. /* HWPARAMS1 */
  473. #define DWC3_NUM_INT(n) (((n) & (0x3f << 15)) >> 15)
  474. struct dwc3_request {
  475. struct usb_request request;
  476. struct list_head list;
  477. struct dwc3_ep *dep;
  478. u8 epnum;
  479. struct dwc3_trb_hw *trb;
  480. dma_addr_t trb_dma;
  481. unsigned direction:1;
  482. unsigned mapped:1;
  483. unsigned queued:1;
  484. };
  485. /**
  486. * struct dwc3 - representation of our controller
  487. * @ctrl_req: usb control request which is used for ep0
  488. * @ep0_trb: trb which is used for the ctrl_req
  489. * @ep0_bounce: bounce buffer for ep0
  490. * @setup_buf: used while precessing STD USB requests
  491. * @ctrl_req_addr: dma address of ctrl_req
  492. * @ep0_trb: dma address of ep0_trb
  493. * @ep0_usb_req: dummy req used while handling STD USB requests
  494. * @setup_buf_addr: dma address of setup_buf
  495. * @ep0_bounce_addr: dma address of ep0_bounce
  496. * @lock: for synchronizing
  497. * @dev: pointer to our struct device
  498. * @xhci: pointer to our xHCI child
  499. * @event_buffer_list: a list of event buffers
  500. * @gadget: device side representation of the peripheral controller
  501. * @gadget_driver: pointer to the gadget driver
  502. * @regs: base address for our registers
  503. * @regs_size: address space size
  504. * @irq: IRQ number
  505. * @num_event_buffers: calculated number of event buffers
  506. * @u1u2: only used on revisions <1.83a for workaround
  507. * @maximum_speed: maximum speed requested (mainly for testing purposes)
  508. * @revision: revision register contents
  509. * @mode: mode of operation
  510. * @is_selfpowered: true when we are selfpowered
  511. * @three_stage_setup: set if we perform a three phase setup
  512. * @ep0_bounced: true when we used bounce buffer
  513. * @ep0_expect_in: true when we expect a DATA IN transfer
  514. * @start_config_issued: true when StartConfig command has been issued
  515. * @setup_packet_pending: true when there's a Setup Packet in FIFO. Workaround
  516. * @ep0_next_event: hold the next expected event
  517. * @ep0state: state of endpoint zero
  518. * @link_state: link state
  519. * @speed: device speed (super, high, full, low)
  520. * @mem: points to start of memory which is used for this struct.
  521. * @hwparams: copy of hwparams registers
  522. * @root: debugfs root folder pointer
  523. */
  524. struct dwc3 {
  525. struct usb_ctrlrequest *ctrl_req;
  526. struct dwc3_trb_hw *ep0_trb;
  527. void *ep0_bounce;
  528. u8 *setup_buf;
  529. dma_addr_t ctrl_req_addr;
  530. dma_addr_t ep0_trb_addr;
  531. dma_addr_t setup_buf_addr;
  532. dma_addr_t ep0_bounce_addr;
  533. struct dwc3_request ep0_usb_req;
  534. /* device lock */
  535. spinlock_t lock;
  536. struct device *dev;
  537. struct platform_device *xhci;
  538. struct resource *res;
  539. struct dwc3_event_buffer **ev_buffs;
  540. struct dwc3_ep *eps[DWC3_ENDPOINTS_NUM];
  541. struct usb_gadget gadget;
  542. struct usb_gadget_driver *gadget_driver;
  543. void __iomem *regs;
  544. size_t regs_size;
  545. int irq;
  546. u32 num_event_buffers;
  547. u32 u1u2;
  548. u32 maximum_speed;
  549. u32 revision;
  550. u32 mode;
  551. #define DWC3_REVISION_173A 0x5533173a
  552. #define DWC3_REVISION_175A 0x5533175a
  553. #define DWC3_REVISION_180A 0x5533180a
  554. #define DWC3_REVISION_183A 0x5533183a
  555. #define DWC3_REVISION_185A 0x5533185a
  556. #define DWC3_REVISION_188A 0x5533188a
  557. #define DWC3_REVISION_190A 0x5533190a
  558. unsigned is_selfpowered:1;
  559. unsigned three_stage_setup:1;
  560. unsigned ep0_bounced:1;
  561. unsigned ep0_expect_in:1;
  562. unsigned start_config_issued:1;
  563. unsigned setup_packet_pending:1;
  564. unsigned delayed_status:1;
  565. enum dwc3_ep0_next ep0_next_event;
  566. enum dwc3_ep0_state ep0state;
  567. enum dwc3_link_state link_state;
  568. enum dwc3_device_state dev_state;
  569. u8 speed;
  570. void *mem;
  571. struct dwc3_hwparams hwparams;
  572. struct dentry *root;
  573. };
  574. /* -------------------------------------------------------------------------- */
  575. #define DWC3_TRBSTS_OK 0
  576. #define DWC3_TRBSTS_MISSED_ISOC 1
  577. #define DWC3_TRBSTS_SETUP_PENDING 2
  578. #define DWC3_TRBCTL_NORMAL 1
  579. #define DWC3_TRBCTL_CONTROL_SETUP 2
  580. #define DWC3_TRBCTL_CONTROL_STATUS2 3
  581. #define DWC3_TRBCTL_CONTROL_STATUS3 4
  582. #define DWC3_TRBCTL_CONTROL_DATA 5
  583. #define DWC3_TRBCTL_ISOCHRONOUS_FIRST 6
  584. #define DWC3_TRBCTL_ISOCHRONOUS 7
  585. #define DWC3_TRBCTL_LINK_TRB 8
  586. /* -------------------------------------------------------------------------- */
  587. struct dwc3_event_type {
  588. u32 is_devspec:1;
  589. u32 type:6;
  590. u32 reserved8_31:25;
  591. } __packed;
  592. #define DWC3_DEPEVT_XFERCOMPLETE 0x01
  593. #define DWC3_DEPEVT_XFERINPROGRESS 0x02
  594. #define DWC3_DEPEVT_XFERNOTREADY 0x03
  595. #define DWC3_DEPEVT_RXTXFIFOEVT 0x04
  596. #define DWC3_DEPEVT_STREAMEVT 0x06
  597. #define DWC3_DEPEVT_EPCMDCMPLT 0x07
  598. /**
  599. * struct dwc3_event_depvt - Device Endpoint Events
  600. * @one_bit: indicates this is an endpoint event (not used)
  601. * @endpoint_number: number of the endpoint
  602. * @endpoint_event: The event we have:
  603. * 0x00 - Reserved
  604. * 0x01 - XferComplete
  605. * 0x02 - XferInProgress
  606. * 0x03 - XferNotReady
  607. * 0x04 - RxTxFifoEvt (IN->Underrun, OUT->Overrun)
  608. * 0x05 - Reserved
  609. * 0x06 - StreamEvt
  610. * 0x07 - EPCmdCmplt
  611. * @reserved11_10: Reserved, don't use.
  612. * @status: Indicates the status of the event. Refer to databook for
  613. * more information.
  614. * @parameters: Parameters of the current event. Refer to databook for
  615. * more information.
  616. */
  617. struct dwc3_event_depevt {
  618. u32 one_bit:1;
  619. u32 endpoint_number:5;
  620. u32 endpoint_event:4;
  621. u32 reserved11_10:2;
  622. u32 status:4;
  623. #define DEPEVT_STATUS_BUSERR (1 << 0)
  624. #define DEPEVT_STATUS_SHORT (1 << 1)
  625. #define DEPEVT_STATUS_IOC (1 << 2)
  626. #define DEPEVT_STATUS_LST (1 << 3)
  627. /* Stream event only */
  628. #define DEPEVT_STREAMEVT_FOUND 1
  629. #define DEPEVT_STREAMEVT_NOTFOUND 2
  630. /* Control-only Status */
  631. #define DEPEVT_STATUS_CONTROL_SETUP 0
  632. #define DEPEVT_STATUS_CONTROL_DATA 1
  633. #define DEPEVT_STATUS_CONTROL_STATUS 2
  634. u32 parameters:16;
  635. } __packed;
  636. /**
  637. * struct dwc3_event_devt - Device Events
  638. * @one_bit: indicates this is a non-endpoint event (not used)
  639. * @device_event: indicates it's a device event. Should read as 0x00
  640. * @type: indicates the type of device event.
  641. * 0 - DisconnEvt
  642. * 1 - USBRst
  643. * 2 - ConnectDone
  644. * 3 - ULStChng
  645. * 4 - WkUpEvt
  646. * 5 - Reserved
  647. * 6 - EOPF
  648. * 7 - SOF
  649. * 8 - Reserved
  650. * 9 - ErrticErr
  651. * 10 - CmdCmplt
  652. * 11 - EvntOverflow
  653. * 12 - VndrDevTstRcved
  654. * @reserved15_12: Reserved, not used
  655. * @event_info: Information about this event
  656. * @reserved31_24: Reserved, not used
  657. */
  658. struct dwc3_event_devt {
  659. u32 one_bit:1;
  660. u32 device_event:7;
  661. u32 type:4;
  662. u32 reserved15_12:4;
  663. u32 event_info:8;
  664. u32 reserved31_24:8;
  665. } __packed;
  666. /**
  667. * struct dwc3_event_gevt - Other Core Events
  668. * @one_bit: indicates this is a non-endpoint event (not used)
  669. * @device_event: indicates it's (0x03) Carkit or (0x04) I2C event.
  670. * @phy_port_number: self-explanatory
  671. * @reserved31_12: Reserved, not used.
  672. */
  673. struct dwc3_event_gevt {
  674. u32 one_bit:1;
  675. u32 device_event:7;
  676. u32 phy_port_number:4;
  677. u32 reserved31_12:20;
  678. } __packed;
  679. /**
  680. * union dwc3_event - representation of Event Buffer contents
  681. * @raw: raw 32-bit event
  682. * @type: the type of the event
  683. * @depevt: Device Endpoint Event
  684. * @devt: Device Event
  685. * @gevt: Global Event
  686. */
  687. union dwc3_event {
  688. u32 raw;
  689. struct dwc3_event_type type;
  690. struct dwc3_event_depevt depevt;
  691. struct dwc3_event_devt devt;
  692. struct dwc3_event_gevt gevt;
  693. };
  694. /*
  695. * DWC3 Features to be used as Driver Data
  696. */
  697. #define DWC3_HAS_PERIPHERAL BIT(0)
  698. #define DWC3_HAS_XHCI BIT(1)
  699. #define DWC3_HAS_OTG BIT(3)
  700. /* prototypes */
  701. void dwc3_set_mode(struct dwc3 *dwc, u32 mode);
  702. int dwc3_host_init(struct dwc3 *dwc);
  703. void dwc3_host_exit(struct dwc3 *dwc);
  704. int dwc3_gadget_init(struct dwc3 *dwc);
  705. void dwc3_gadget_exit(struct dwc3 *dwc);
  706. extern int dwc3_get_device_id(void);
  707. extern void dwc3_put_device_id(int id);
  708. #endif /* __DRIVERS_USB_DWC3_CORE_H */