bdc.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * bdc.h - header for the BRCM BDC USB3.0 device controller
  4. *
  5. * Copyright (C) 2014 Broadcom Corporation
  6. *
  7. * Author: Ashwini Pahuja
  8. */
  9. #ifndef __LINUX_BDC_H__
  10. #define __LINUX_BDC_H__
  11. #include <linux/kernel.h>
  12. #include <linux/usb.h>
  13. #include <linux/device.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/list.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/mm.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/usb/ch9.h>
  20. #include <linux/usb/gadget.h>
  21. #include <asm/unaligned.h>
  22. #define BRCM_BDC_NAME "bdc"
  23. #define BRCM_BDC_DESC "Broadcom USB Device Controller driver"
  24. #define DMA_ADDR_INVALID (~(dma_addr_t)0)
  25. /* BDC command operation timeout in usec*/
  26. #define BDC_CMD_TIMEOUT 1000
  27. /* BDC controller operation timeout in usec*/
  28. #define BDC_COP_TIMEOUT 500
  29. /*
  30. * Maximum size of ep0 response buffer for ch9 requests,
  31. * the set_sel request uses 6 so far, the max.
  32. */
  33. #define EP0_RESPONSE_BUFF 6
  34. /* Start with SS as default */
  35. #define EP0_MAX_PKT_SIZE 512
  36. /* 64 entries in a SRR */
  37. #define NUM_SR_ENTRIES 64
  38. /* Num of bds per table */
  39. #define NUM_BDS_PER_TABLE 64
  40. /* Num of tables in bd list for control,bulk and Int ep */
  41. #define NUM_TABLES 2
  42. /* Num of tables in bd list for Isoch ep */
  43. #define NUM_TABLES_ISOCH 6
  44. /* U1 Timeout default: 248usec */
  45. #define U1_TIMEOUT 0xf8
  46. /* Interrupt coalescence in usec */
  47. #define INT_CLS 500
  48. /* Register offsets */
  49. /* Configuration and Capability registers */
  50. #define BDC_BDCCFG0 0x00
  51. #define BDC_BDCCFG1 0x04
  52. #define BDC_BDCCAP0 0x08
  53. #define BDC_BDCCAP1 0x0c
  54. #define BDC_CMDPAR0 0x10
  55. #define BDC_CMDPAR1 0x14
  56. #define BDC_CMDPAR2 0x18
  57. #define BDC_CMDSC 0x1c
  58. #define BDC_USPC 0x20
  59. #define BDC_USPPMS 0x28
  60. #define BDC_USPPM2 0x2c
  61. #define BDC_SPBBAL 0x38
  62. #define BDC_SPBBAH 0x3c
  63. #define BDC_BDCSC 0x40
  64. #define BDC_XSFNTF 0x4c
  65. #define BDC_DVCSA 0x50
  66. #define BDC_DVCSB 0x54
  67. #define BDC_EPSTS0 0x60
  68. #define BDC_EPSTS1 0x64
  69. #define BDC_EPSTS2 0x68
  70. #define BDC_EPSTS3 0x6c
  71. #define BDC_EPSTS4 0x70
  72. #define BDC_EPSTS5 0x74
  73. #define BDC_EPSTS6 0x78
  74. #define BDC_EPSTS7 0x7c
  75. #define BDC_SRRBAL(n) (0x200 + ((n) * 0x10))
  76. #define BDC_SRRBAH(n) (0x204 + ((n) * 0x10))
  77. #define BDC_SRRINT(n) (0x208 + ((n) * 0x10))
  78. #define BDC_INTCTLS(n) (0x20c + ((n) * 0x10))
  79. /* Extended capability regs */
  80. #define BDC_FSCNOC 0xcd4
  81. #define BDC_FSCNIC 0xce4
  82. #define NUM_NCS(p) ((p) >> 28)
  83. /* Register bit fields and Masks */
  84. /* BDC Configuration 0 */
  85. #define BDC_PGS(p) (((p) & (0x7 << 8)) >> 8)
  86. #define BDC_SPB(p) ((p) & 0x7)
  87. /* BDC Capability1 */
  88. #define BDC_P64 BIT(0)
  89. /* BDC Command register */
  90. #define BDC_CMD_FH 0xe
  91. #define BDC_CMD_DNC 0x6
  92. #define BDC_CMD_EPO 0x4
  93. #define BDC_CMD_BLA 0x3
  94. #define BDC_CMD_EPC 0x2
  95. #define BDC_CMD_DVC 0x1
  96. #define BDC_CMD_CWS BIT(5)
  97. #define BDC_CMD_CST(p) (((p) & (0xf << 6))>>6)
  98. #define BDC_CMD_EPN(p) (((p) & 0x1f) << 10)
  99. #define BDC_SUB_CMD_ADD (0x1 << 17)
  100. #define BDC_SUB_CMD_FWK (0x4 << 17)
  101. /* Reset sequence number */
  102. #define BDC_CMD_EPO_RST_SN (0x1 << 16)
  103. #define BDC_CMD_EP0_XSD (0x1 << 16)
  104. #define BDC_SUB_CMD_ADD_EP (0x1 << 17)
  105. #define BDC_SUB_CMD_DRP_EP (0x2 << 17)
  106. #define BDC_SUB_CMD_EP_STP (0x2 << 17)
  107. #define BDC_SUB_CMD_EP_STL (0x4 << 17)
  108. #define BDC_SUB_CMD_EP_RST (0x1 << 17)
  109. #define BDC_CMD_SRD BIT(27)
  110. /* CMD completion status */
  111. #define BDC_CMDS_SUCC 0x1
  112. #define BDC_CMDS_PARA 0x3
  113. #define BDC_CMDS_STAT 0x4
  114. #define BDC_CMDS_FAIL 0x5
  115. #define BDC_CMDS_INTL 0x6
  116. #define BDC_CMDS_BUSY 0xf
  117. /* CMDSC Param 2 shifts */
  118. #define EPT_SHIFT 22
  119. #define MP_SHIFT 10
  120. #define MB_SHIFT 6
  121. #define EPM_SHIFT 4
  122. /* BDC USPSC */
  123. #define BDC_VBC BIT(31)
  124. #define BDC_PRC BIT(30)
  125. #define BDC_PCE BIT(29)
  126. #define BDC_CFC BIT(28)
  127. #define BDC_PCC BIT(27)
  128. #define BDC_PSC BIT(26)
  129. #define BDC_VBS BIT(25)
  130. #define BDC_PRS BIT(24)
  131. #define BDC_PCS BIT(23)
  132. #define BDC_PSP(p) (((p) & (0x7 << 20))>>20)
  133. #define BDC_SCN BIT(8)
  134. #define BDC_SDC BIT(7)
  135. #define BDC_SWS BIT(4)
  136. #define BDC_USPSC_RW (BDC_SCN|BDC_SDC|BDC_SWS|0xf)
  137. #define BDC_PSP(p) (((p) & (0x7 << 20))>>20)
  138. #define BDC_SPEED_FS 0x1
  139. #define BDC_SPEED_LS 0x2
  140. #define BDC_SPEED_HS 0x3
  141. #define BDC_SPEED_SS 0x4
  142. #define BDC_PST(p) ((p) & 0xf)
  143. #define BDC_PST_MASK 0xf
  144. /* USPPMS */
  145. #define BDC_U2E BIT(31)
  146. #define BDC_U1E BIT(30)
  147. #define BDC_U2A BIT(29)
  148. #define BDC_PORT_W1S BIT(17)
  149. #define BDC_U1T(p) ((p) & 0xff)
  150. #define BDC_U2T(p) (((p) & 0xff) << 8)
  151. #define BDC_U1T_MASK 0xff
  152. /* USBPM2 */
  153. /* Hardware LPM Enable */
  154. #define BDC_HLE BIT(16)
  155. /* BDC Status and Control */
  156. #define BDC_COP_RST (1 << 29)
  157. #define BDC_COP_RUN (2 << 29)
  158. #define BDC_COP_STP (4 << 29)
  159. #define BDC_COP_MASK (BDC_COP_RST|BDC_COP_RUN|BDC_COP_STP)
  160. #define BDC_COS BIT(28)
  161. #define BDC_CSTS(p) (((p) & (0x7 << 20)) >> 20)
  162. #define BDC_MASK_MCW BIT(7)
  163. #define BDC_GIE BIT(1)
  164. #define BDC_GIP BIT(0)
  165. #define BDC_HLT 1
  166. #define BDC_NOR 2
  167. #define BDC_OIP 7
  168. /* Buffer descriptor and Status report bit fields and masks */
  169. #define BD_TYPE_BITMASK (0xf)
  170. #define BD_CHAIN 0xf
  171. #define BD_TFS_SHIFT 4
  172. #define BD_SOT BIT(26)
  173. #define BD_EOT BIT(27)
  174. #define BD_ISP BIT(29)
  175. #define BD_IOC BIT(30)
  176. #define BD_SBF BIT(31)
  177. #define BD_INTR_TARGET(p) (((p) & 0x1f) << 27)
  178. #define BDC_SRR_RWS BIT(4)
  179. #define BDC_SRR_RST BIT(3)
  180. #define BDC_SRR_ISR BIT(2)
  181. #define BDC_SRR_IE BIT(1)
  182. #define BDC_SRR_IP BIT(0)
  183. #define BDC_SRR_EPI(p) (((p) & (0xff << 24)) >> 24)
  184. #define BDC_SRR_DPI(p) (((p) & (0xff << 16)) >> 16)
  185. #define BDC_SRR_DPI_MASK 0x00ff0000
  186. #define MARK_CHAIN_BD (BD_CHAIN|BD_EOT|BD_SOT)
  187. /* Control transfer BD specific fields */
  188. #define BD_DIR_IN BIT(25)
  189. #define BDC_PTC_MASK 0xf0000000
  190. /* status report defines */
  191. #define SR_XSF 0
  192. #define SR_USPC 4
  193. #define SR_BD_LEN(p) ((p) & 0xffffff)
  194. #define XSF_SUCC 0x1
  195. #define XSF_SHORT 0x3
  196. #define XSF_BABB 0x4
  197. #define XSF_SETUP_RECV 0x6
  198. #define XSF_DATA_START 0x7
  199. #define XSF_STATUS_START 0x8
  200. #define XSF_STS(p) (((p) >> 28) & 0xf)
  201. /* Transfer BD fields */
  202. #define BD_LEN(p) ((p) & 0x1ffff)
  203. #define BD_LTF BIT(25)
  204. #define BD_TYPE_DS 0x1
  205. #define BD_TYPE_SS 0x2
  206. #define BDC_EP_ENABLED BIT(0)
  207. #define BDC_EP_STALL BIT(1)
  208. #define BDC_EP_STOP BIT(2)
  209. /* One BD can transfer max 65536 bytes */
  210. #define BD_MAX_BUFF_SIZE (1 << 16)
  211. /* Maximum bytes in one XFR, Refer to BDC spec */
  212. #define MAX_XFR_LEN 16777215
  213. /* defines for Force Header command */
  214. #define DEV_NOTF_TYPE 6
  215. #define FWK_SUBTYPE 1
  216. #define TRA_PACKET 4
  217. #define to_bdc_ep(e) container_of(e, struct bdc_ep, usb_ep)
  218. #define to_bdc_req(r) container_of(r, struct bdc_req, usb_req)
  219. #define gadget_to_bdc(g) container_of(g, struct bdc, gadget)
  220. /* FUNCTION WAKE DEV NOTIFICATION interval, USB3 spec table 8.13 */
  221. #define BDC_TNOTIFY 2500 /*in ms*/
  222. /* Devstatus bitfields */
  223. #define REMOTE_WAKEUP_ISSUED BIT(16)
  224. #define DEVICE_SUSPENDED BIT(17)
  225. #define FUNC_WAKE_ISSUED BIT(18)
  226. #define REMOTE_WAKE_ENABLE (1 << USB_DEVICE_REMOTE_WAKEUP)
  227. /* On disconnect, preserve these bits and clear rest */
  228. #define DEVSTATUS_CLEAR (1 << USB_DEVICE_SELF_POWERED)
  229. /* Hardware and software Data structures */
  230. /* Endpoint bd: buffer descriptor */
  231. struct bdc_bd {
  232. __le32 offset[4];
  233. };
  234. /* Status report in Status report ring(srr) */
  235. struct bdc_sr {
  236. __le32 offset[4];
  237. };
  238. /* bd_table: contiguous bd's in a table */
  239. struct bd_table {
  240. struct bdc_bd *start_bd;
  241. /* dma address of start bd of table*/
  242. dma_addr_t dma;
  243. };
  244. /*
  245. * Each endpoint has a bdl(buffer descriptor list), bdl consists of 1 or more bd
  246. * table's chained to each other through a chain bd, every table has equal
  247. * number of bds. the software uses bdi(bd index) to refer to particular bd in
  248. * the list.
  249. */
  250. struct bd_list {
  251. /* Array of bd table pointers*/
  252. struct bd_table **bd_table_array;
  253. /* How many tables chained to each other */
  254. int num_tabs;
  255. /* Max_bdi = num_tabs * num_bds_table - 1 */
  256. int max_bdi;
  257. /* current enq bdi from sw point of view */
  258. int eqp_bdi;
  259. /* current deq bdi from sw point of view */
  260. int hwd_bdi;
  261. /* numbers of bds per table */
  262. int num_bds_table;
  263. };
  264. struct bdc_req;
  265. /* Representation of a transfer, one transfer can have multiple bd's */
  266. struct bd_transfer {
  267. struct bdc_req *req;
  268. /* start bd index */
  269. int start_bdi;
  270. /* this will be the next hw dqp when this transfer completes */
  271. int next_hwd_bdi;
  272. /* number of bds in this transfer */
  273. int num_bds;
  274. };
  275. /*
  276. * Representation of a gadget request, every gadget request is contained
  277. * by 1 bd_transfer.
  278. */
  279. struct bdc_req {
  280. struct usb_request usb_req;
  281. struct list_head queue;
  282. struct bdc_ep *ep;
  283. /* only one Transfer per request */
  284. struct bd_transfer bd_xfr;
  285. int epnum;
  286. };
  287. /* scratchpad buffer needed by bdc hardware */
  288. struct bdc_scratchpad {
  289. dma_addr_t sp_dma;
  290. void *buff;
  291. u32 size;
  292. };
  293. /* endpoint representation */
  294. struct bdc_ep {
  295. struct usb_ep usb_ep;
  296. struct list_head queue;
  297. struct bdc *bdc;
  298. u8 ep_type;
  299. u8 dir;
  300. u8 ep_num;
  301. const struct usb_ss_ep_comp_descriptor *comp_desc;
  302. const struct usb_endpoint_descriptor *desc;
  303. unsigned int flags;
  304. char name[20];
  305. /* endpoint bd list*/
  306. struct bd_list bd_list;
  307. /*
  308. * HW generates extra event for multi bd tranfers, this flag helps in
  309. * ignoring the extra event
  310. */
  311. bool ignore_next_sr;
  312. };
  313. /* bdc cmmand parameter structure */
  314. struct bdc_cmd_params {
  315. u32 param2;
  316. u32 param1;
  317. u32 param0;
  318. };
  319. /* status report ring(srr), currently one srr is supported for entire system */
  320. struct srr {
  321. struct bdc_sr *sr_bds;
  322. u16 eqp_index;
  323. u16 dqp_index;
  324. dma_addr_t dma_addr;
  325. };
  326. /* EP0 states */
  327. enum bdc_ep0_state {
  328. WAIT_FOR_SETUP = 0,
  329. WAIT_FOR_DATA_START,
  330. WAIT_FOR_DATA_XMIT,
  331. WAIT_FOR_STATUS_START,
  332. WAIT_FOR_STATUS_XMIT,
  333. STATUS_PENDING
  334. };
  335. /* Link states */
  336. enum bdc_link_state {
  337. BDC_LINK_STATE_U0 = 0x00,
  338. BDC_LINK_STATE_U3 = 0x03,
  339. BDC_LINK_STATE_RX_DET = 0x05,
  340. BDC_LINK_STATE_RESUME = 0x0f
  341. };
  342. /* representation of bdc */
  343. struct bdc {
  344. struct usb_gadget gadget;
  345. struct usb_gadget_driver *gadget_driver;
  346. struct device *dev;
  347. /* device lock */
  348. spinlock_t lock;
  349. /* generic phy */
  350. struct phy **phys;
  351. int num_phys;
  352. /* num of endpoints for a particular instantiation of IP */
  353. unsigned int num_eps;
  354. /*
  355. * Array of ep's, it uses the same index covention as bdc hw i.e.
  356. * 1 for ep0, 2 for 1out,3 for 1in ....
  357. */
  358. struct bdc_ep **bdc_ep_array;
  359. void __iomem *regs;
  360. struct bdc_scratchpad scratchpad;
  361. u32 sp_buff_size;
  362. /* current driver supports 1 status ring */
  363. struct srr srr;
  364. /* Last received setup packet */
  365. struct usb_ctrlrequest setup_pkt;
  366. struct bdc_req ep0_req;
  367. struct bdc_req status_req;
  368. enum bdc_ep0_state ep0_state;
  369. bool delayed_status;
  370. bool zlp_needed;
  371. bool reinit;
  372. bool pullup;
  373. /* Bits 0-15 are standard and 16-31 for proprietary information */
  374. u32 devstatus;
  375. int irq;
  376. void *mem;
  377. u32 dev_addr;
  378. /* DMA pools */
  379. struct dma_pool *bd_table_pool;
  380. u8 test_mode;
  381. /* array of callbacks for various status report handlers */
  382. void (*sr_handler[2])(struct bdc *, struct bdc_sr *);
  383. /* ep0 callback handlers */
  384. void (*sr_xsf_ep0[3])(struct bdc *, struct bdc_sr *);
  385. /* ep0 response buffer for ch9 requests like GET_STATUS and SET_SEL */
  386. unsigned char ep0_response_buff[EP0_RESPONSE_BUFF];
  387. /*
  388. * Timer to check if host resumed transfer after bdc sent Func wake
  389. * notification packet after a remote wakeup. if not, then resend the
  390. * Func Wake packet every 2.5 secs. Refer to USB3 spec section 8.5.6.4
  391. */
  392. struct delayed_work func_wake_notify;
  393. struct clk *clk;
  394. };
  395. static inline u32 bdc_readl(void __iomem *base, u32 offset)
  396. {
  397. return readl(base + offset);
  398. }
  399. static inline void bdc_writel(void __iomem *base, u32 offset, u32 value)
  400. {
  401. writel(value, base + offset);
  402. }
  403. /* Buffer descriptor list operations */
  404. void bdc_notify_xfr(struct bdc *bdc, u32 epnum);
  405. void bdc_softconn(struct bdc *bdc);
  406. void bdc_softdisconn(struct bdc *bdc);
  407. int bdc_run(struct bdc *bdc);
  408. int bdc_stop(struct bdc *bdc);
  409. int bdc_reset(struct bdc *bdc);
  410. int bdc_udc_init(struct bdc *bdc);
  411. void bdc_udc_exit(struct bdc *bdc);
  412. int bdc_reinit(struct bdc *bdc);
  413. /* Status report handlers */
  414. /* Upstream port status change sr */
  415. void bdc_sr_uspc(struct bdc *bdc, struct bdc_sr *sreport);
  416. /* transfer sr */
  417. void bdc_sr_xsf(struct bdc *bdc, struct bdc_sr *sreport);
  418. /* EP0 XSF handlers */
  419. void bdc_xsf_ep0_setup_recv(struct bdc *bdc, struct bdc_sr *sreport);
  420. void bdc_xsf_ep0_data_start(struct bdc *bdc, struct bdc_sr *sreport);
  421. void bdc_xsf_ep0_status_start(struct bdc *bdc, struct bdc_sr *sreport);
  422. #endif /* __LINUX_BDC_H__ */