bdc.h 12 KB

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