btintel_pcie.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. *
  4. * Bluetooth support for Intel PCIe devices
  5. *
  6. * Copyright (C) 2024 Intel Corporation
  7. */
  8. /* Control and Status Register(BTINTEL_PCIE_CSR) */
  9. #define BTINTEL_PCIE_CSR_BASE (0x000)
  10. #define BTINTEL_PCIE_CSR_FUNC_CTRL_REG (BTINTEL_PCIE_CSR_BASE + 0x024)
  11. #define BTINTEL_PCIE_CSR_HW_REV_REG (BTINTEL_PCIE_CSR_BASE + 0x028)
  12. #define BTINTEL_PCIE_CSR_RF_ID_REG (BTINTEL_PCIE_CSR_BASE + 0x09C)
  13. #define BTINTEL_PCIE_CSR_BOOT_STAGE_REG (BTINTEL_PCIE_CSR_BASE + 0x108)
  14. #define BTINTEL_PCIE_CSR_IPC_SLEEP_CTL_REG (BTINTEL_PCIE_CSR_BASE + 0x114)
  15. #define BTINTEL_PCIE_CSR_CI_ADDR_LSB_REG (BTINTEL_PCIE_CSR_BASE + 0x118)
  16. #define BTINTEL_PCIE_CSR_CI_ADDR_MSB_REG (BTINTEL_PCIE_CSR_BASE + 0x11C)
  17. #define BTINTEL_PCIE_CSR_IMG_RESPONSE_REG (BTINTEL_PCIE_CSR_BASE + 0x12C)
  18. #define BTINTEL_PCIE_CSR_HBUS_TARG_WRPTR (BTINTEL_PCIE_CSR_BASE + 0x460)
  19. /* BTINTEL_PCIE_CSR Function Control Register */
  20. #define BTINTEL_PCIE_CSR_FUNC_CTRL_FUNC_ENA (BIT(0))
  21. #define BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_INIT (BIT(6))
  22. #define BTINTEL_PCIE_CSR_FUNC_CTRL_FUNC_INIT (BIT(7))
  23. #define BTINTEL_PCIE_CSR_FUNC_CTRL_MAC_ACCESS_STS (BIT(20))
  24. #define BTINTEL_PCIE_CSR_FUNC_CTRL_SW_RESET (BIT(31))
  25. /* Value for BTINTEL_PCIE_CSR_BOOT_STAGE register */
  26. #define BTINTEL_PCIE_CSR_BOOT_STAGE_ROM (BIT(0))
  27. #define BTINTEL_PCIE_CSR_BOOT_STAGE_IML (BIT(1))
  28. #define BTINTEL_PCIE_CSR_BOOT_STAGE_OPFW (BIT(2))
  29. #define BTINTEL_PCIE_CSR_BOOT_STAGE_ROM_LOCKDOWN (BIT(10))
  30. #define BTINTEL_PCIE_CSR_BOOT_STAGE_IML_LOCKDOWN (BIT(11))
  31. #define BTINTEL_PCIE_CSR_BOOT_STAGE_MAC_ACCESS_ON (BIT(16))
  32. #define BTINTEL_PCIE_CSR_BOOT_STAGE_ALIVE (BIT(23))
  33. #define BTINTEL_PCIE_CSR_BOOT_STAGE_D3_STATE_READY (BIT(24))
  34. /* Registers for MSI-X */
  35. #define BTINTEL_PCIE_CSR_MSIX_BASE (0x2000)
  36. #define BTINTEL_PCIE_CSR_MSIX_FH_INT_CAUSES (BTINTEL_PCIE_CSR_MSIX_BASE + 0x0800)
  37. #define BTINTEL_PCIE_CSR_MSIX_FH_INT_MASK (BTINTEL_PCIE_CSR_MSIX_BASE + 0x0804)
  38. #define BTINTEL_PCIE_CSR_MSIX_HW_INT_CAUSES (BTINTEL_PCIE_CSR_MSIX_BASE + 0x0808)
  39. #define BTINTEL_PCIE_CSR_MSIX_HW_INT_MASK (BTINTEL_PCIE_CSR_MSIX_BASE + 0x080C)
  40. #define BTINTEL_PCIE_CSR_MSIX_AUTOMASK_ST (BTINTEL_PCIE_CSR_MSIX_BASE + 0x0810)
  41. #define BTINTEL_PCIE_CSR_MSIX_AUTOMASK_EN (BTINTEL_PCIE_CSR_MSIX_BASE + 0x0814)
  42. #define BTINTEL_PCIE_CSR_MSIX_IVAR_BASE (BTINTEL_PCIE_CSR_MSIX_BASE + 0x0880)
  43. #define BTINTEL_PCIE_CSR_MSIX_IVAR(cause) (BTINTEL_PCIE_CSR_MSIX_IVAR_BASE + (cause))
  44. /* Causes for the FH register interrupts */
  45. enum msix_fh_int_causes {
  46. BTINTEL_PCIE_MSIX_FH_INT_CAUSES_0 = BIT(0), /* cause 0 */
  47. BTINTEL_PCIE_MSIX_FH_INT_CAUSES_1 = BIT(1), /* cause 1 */
  48. };
  49. /* Causes for the HW register interrupts */
  50. enum msix_hw_int_causes {
  51. BTINTEL_PCIE_MSIX_HW_INT_CAUSES_GP0 = BIT(0), /* cause 32 */
  52. };
  53. /* PCIe device states
  54. * Host-Device interface is active
  55. * Host-Device interface is inactive(as reflected by IPC_SLEEP_CONTROL_CSR_AD)
  56. * Host-Device interface is inactive(as reflected by IPC_SLEEP_CONTROL_CSR_AD)
  57. */
  58. enum {
  59. BTINTEL_PCIE_STATE_D0 = 0,
  60. BTINTEL_PCIE_STATE_D3_HOT = 2,
  61. BTINTEL_PCIE_STATE_D3_COLD = 3,
  62. };
  63. #define BTINTEL_PCIE_MSIX_NON_AUTO_CLEAR_CAUSE BIT(7)
  64. /* Minimum and Maximum number of MSI-X Vector
  65. * Intel Bluetooth PCIe support only 1 vector
  66. */
  67. #define BTINTEL_PCIE_MSIX_VEC_MAX 1
  68. #define BTINTEL_PCIE_MSIX_VEC_MIN 1
  69. /* Default poll time for MAC access during init */
  70. #define BTINTEL_DEFAULT_MAC_ACCESS_TIMEOUT_US 200000
  71. /* Default interrupt timeout in msec */
  72. #define BTINTEL_DEFAULT_INTR_TIMEOUT_MS 3000
  73. /* The number of descriptors in TX queues */
  74. #define BTINTEL_PCIE_TX_DESCS_COUNT 32
  75. /* The number of descriptors in RX queues */
  76. #define BTINTEL_PCIE_RX_DESCS_COUNT 64
  77. /* Number of Queue for TX and RX
  78. * It indicates the index of the IA(Index Array)
  79. */
  80. enum {
  81. BTINTEL_PCIE_TXQ_NUM = 0,
  82. BTINTEL_PCIE_RXQ_NUM = 1,
  83. BTINTEL_PCIE_NUM_QUEUES = 2,
  84. };
  85. /* The size of DMA buffer for TX and RX in bytes */
  86. #define BTINTEL_PCIE_BUFFER_SIZE 4096
  87. /* DMA allocation alignment */
  88. #define BTINTEL_PCIE_DMA_POOL_ALIGNMENT 256
  89. #define BTINTEL_PCIE_TX_WAIT_TIMEOUT_MS 500
  90. /* Doorbell vector for TFD */
  91. #define BTINTEL_PCIE_TX_DB_VEC 0
  92. /* Doorbell vector for FRBD */
  93. #define BTINTEL_PCIE_RX_DB_VEC 513
  94. /* RBD buffer size mapping */
  95. #define BTINTEL_PCIE_RBD_SIZE_4K 0x04
  96. /*
  97. * Struct for Context Information (v2)
  98. *
  99. * All members are write-only for host and read-only for device.
  100. *
  101. * @version: Version of context information
  102. * @size: Size of context information
  103. * @config: Config with which host wants peripheral to execute
  104. * Subset of capability register published by device
  105. * @addr_tr_hia: Address of TR Head Index Array
  106. * @addr_tr_tia: Address of TR Tail Index Array
  107. * @addr_cr_hia: Address of CR Head Index Array
  108. * @addr_cr_tia: Address of CR Tail Index Array
  109. * @num_tr_ia: Number of entries in TR Index Arrays
  110. * @num_cr_ia: Number of entries in CR Index Arrays
  111. * @rbd_siz: RBD Size { 0x4=4K }
  112. * @addr_tfdq: Address of TFD Queue(tx)
  113. * @addr_urbdq0: Address of URBD Queue(tx)
  114. * @num_tfdq: Number of TFD in TFD Queue(tx)
  115. * @num_urbdq0: Number of URBD in URBD Queue(tx)
  116. * @tfdq_db_vec: Queue number of TFD
  117. * @urbdq0_db_vec: Queue number of URBD
  118. * @addr_frbdq: Address of FRBD Queue(rx)
  119. * @addr_urbdq1: Address of URBD Queue(rx)
  120. * @num_frbdq: Number of FRBD in FRBD Queue(rx)
  121. * @frbdq_db_vec: Queue number of FRBD
  122. * @num_urbdq1: Number of URBD in URBD Queue(rx)
  123. * @urbdq_db_vec: Queue number of URBDQ1
  124. * @tr_msi_vec: Transfer Ring MSI-X Vector
  125. * @cr_msi_vec: Completion Ring MSI-X Vector
  126. * @dbgc_addr: DBGC first fragment address
  127. * @dbgc_size: DBGC buffer size
  128. * @early_enable: Enarly debug enable
  129. * @dbg_output_mode: Debug output mode
  130. * Bit[4] DBGC O/P { 0=SRAM, 1=DRAM(not relevant for NPK) }
  131. * Bit[5] DBGC I/P { 0=BDBG, 1=DBGI }
  132. * Bits[6:7] DBGI O/P(relevant if bit[5] = 1)
  133. * 0=BT DBGC, 1=WiFi DBGC, 2=NPK }
  134. * @dbg_preset: Debug preset
  135. * @ext_addr: Address of context information extension
  136. * @ext_size: Size of context information part
  137. *
  138. * Total 38 DWords
  139. */
  140. struct ctx_info {
  141. u16 version;
  142. u16 size;
  143. u32 config;
  144. u32 reserved_dw02;
  145. u32 reserved_dw03;
  146. u64 addr_tr_hia;
  147. u64 addr_tr_tia;
  148. u64 addr_cr_hia;
  149. u64 addr_cr_tia;
  150. u16 num_tr_ia;
  151. u16 num_cr_ia;
  152. u32 rbd_size:4,
  153. reserved_dw13:28;
  154. u64 addr_tfdq;
  155. u64 addr_urbdq0;
  156. u16 num_tfdq;
  157. u16 num_urbdq0;
  158. u16 tfdq_db_vec;
  159. u16 urbdq0_db_vec;
  160. u64 addr_frbdq;
  161. u64 addr_urbdq1;
  162. u16 num_frbdq;
  163. u16 frbdq_db_vec;
  164. u16 num_urbdq1;
  165. u16 urbdq_db_vec;
  166. u16 tr_msi_vec;
  167. u16 cr_msi_vec;
  168. u32 reserved_dw27;
  169. u64 dbgc_addr;
  170. u32 dbgc_size;
  171. u32 early_enable:1,
  172. reserved_dw31:3,
  173. dbg_output_mode:4,
  174. dbg_preset:8,
  175. reserved2_dw31:16;
  176. u64 ext_addr;
  177. u32 ext_size;
  178. u32 test_param;
  179. u32 reserved_dw36;
  180. u32 reserved_dw37;
  181. } __packed;
  182. /* Transfer Descriptor for TX
  183. * @type: Not in use. Set to 0x0
  184. * @size: Size of data in the buffer
  185. * @addr: DMA Address of buffer
  186. */
  187. struct tfd {
  188. u8 type;
  189. u16 size;
  190. u8 reserved;
  191. u64 addr;
  192. u32 reserved1;
  193. } __packed;
  194. /* URB Descriptor for TX
  195. * @tfd_index: Index of TFD in TFDQ + 1
  196. * @num_txq: Queue index of TFD Queue
  197. * @cmpl_count: Completion count. Always 0x01
  198. * @immediate_cmpl: Immediate completion flag: Always 0x01
  199. */
  200. struct urbd0 {
  201. u32 tfd_index:16,
  202. num_txq:8,
  203. cmpl_count:4,
  204. reserved:3,
  205. immediate_cmpl:1;
  206. } __packed;
  207. /* FRB Descriptor for RX
  208. * @tag: RX buffer tag (index of RX buffer queue)
  209. * @addr: Address of buffer
  210. */
  211. struct frbd {
  212. u32 tag:16,
  213. reserved:16;
  214. u32 reserved2;
  215. u64 addr;
  216. } __packed;
  217. /* URB Descriptor for RX
  218. * @frbd_tag: Tag from FRBD
  219. * @status: Status
  220. */
  221. struct urbd1 {
  222. u32 frbd_tag:16,
  223. status:1,
  224. reserved:14,
  225. fixed:1;
  226. } __packed;
  227. /* RFH header in RX packet
  228. * @packet_len: Length of the data in the buffer
  229. * @rxq: RX Queue number
  230. * @cmd_id: Command ID. Not in Use
  231. */
  232. struct rfh_hdr {
  233. u64 packet_len:16,
  234. rxq:6,
  235. reserved:10,
  236. cmd_id:16,
  237. reserved1:16;
  238. } __packed;
  239. /* Internal data buffer
  240. * @data: pointer to the data buffer
  241. * @p_addr: physical address of data buffer
  242. */
  243. struct data_buf {
  244. u8 *data;
  245. dma_addr_t data_p_addr;
  246. };
  247. /* Index Array */
  248. struct ia {
  249. dma_addr_t tr_hia_p_addr;
  250. u16 *tr_hia;
  251. dma_addr_t tr_tia_p_addr;
  252. u16 *tr_tia;
  253. dma_addr_t cr_hia_p_addr;
  254. u16 *cr_hia;
  255. dma_addr_t cr_tia_p_addr;
  256. u16 *cr_tia;
  257. };
  258. /* Structure for TX Queue
  259. * @count: Number of descriptors
  260. * @tfds: Array of TFD
  261. * @urbd0s: Array of URBD0
  262. * @buf: Array of data_buf structure
  263. */
  264. struct txq {
  265. u16 count;
  266. dma_addr_t tfds_p_addr;
  267. struct tfd *tfds;
  268. dma_addr_t urbd0s_p_addr;
  269. struct urbd0 *urbd0s;
  270. dma_addr_t buf_p_addr;
  271. void *buf_v_addr;
  272. struct data_buf *bufs;
  273. };
  274. /* Structure for RX Queue
  275. * @count: Number of descriptors
  276. * @frbds: Array of FRBD
  277. * @urbd1s: Array of URBD1
  278. * @buf: Array of data_buf structure
  279. */
  280. struct rxq {
  281. u16 count;
  282. dma_addr_t frbds_p_addr;
  283. struct frbd *frbds;
  284. dma_addr_t urbd1s_p_addr;
  285. struct urbd1 *urbd1s;
  286. dma_addr_t buf_p_addr;
  287. void *buf_v_addr;
  288. struct data_buf *bufs;
  289. };
  290. /* struct btintel_pcie_data
  291. * @pdev: pci device
  292. * @hdev: hdev device
  293. * @flags: driver state
  294. * @irq_lock: spinlock for MSI-X
  295. * @hci_rx_lock: spinlock for HCI RX flow
  296. * @base_addr: pci base address (from BAR)
  297. * @msix_entries: array of MSI-X entries
  298. * @msix_enabled: true if MSI-X is enabled;
  299. * @alloc_vecs: number of interrupt vectors allocated
  300. * @def_irq: default irq for all causes
  301. * @fh_init_mask: initial unmasked rxq causes
  302. * @hw_init_mask: initial unmaksed hw causes
  303. * @boot_stage_cache: cached value of boot stage register
  304. * @img_resp_cache: cached value of image response register
  305. * @cnvi: CNVi register value
  306. * @cnvr: CNVr register value
  307. * @gp0_received: condition for gp0 interrupt
  308. * @gp0_wait_q: wait_q for gp0 interrupt
  309. * @tx_wait_done: condition for tx interrupt
  310. * @tx_wait_q: wait_q for tx interrupt
  311. * @workqueue: workqueue for RX work
  312. * @rx_skb_q: SKB queue for RX packet
  313. * @rx_work: RX work struct to process the RX packet in @rx_skb_q
  314. * @dma_pool: DMA pool for descriptors, index array and ci
  315. * @dma_p_addr: DMA address for pool
  316. * @dma_v_addr: address of pool
  317. * @ci_p_addr: DMA address for CI struct
  318. * @ci: CI struct
  319. * @ia: Index Array struct
  320. * @txq: TX Queue struct
  321. * @rxq: RX Queue struct
  322. * @alive_intr_ctxt: Alive interrupt context
  323. */
  324. struct btintel_pcie_data {
  325. struct pci_dev *pdev;
  326. struct hci_dev *hdev;
  327. unsigned long flags;
  328. /* lock used in MSI-X interrupt */
  329. spinlock_t irq_lock;
  330. /* lock to serialize rx events */
  331. spinlock_t hci_rx_lock;
  332. void __iomem *base_addr;
  333. struct msix_entry msix_entries[BTINTEL_PCIE_MSIX_VEC_MAX];
  334. bool msix_enabled;
  335. u32 alloc_vecs;
  336. u32 def_irq;
  337. u32 fh_init_mask;
  338. u32 hw_init_mask;
  339. u32 boot_stage_cache;
  340. u32 img_resp_cache;
  341. u32 cnvi;
  342. u32 cnvr;
  343. bool gp0_received;
  344. wait_queue_head_t gp0_wait_q;
  345. bool tx_wait_done;
  346. wait_queue_head_t tx_wait_q;
  347. struct workqueue_struct *workqueue;
  348. struct sk_buff_head rx_skb_q;
  349. struct work_struct rx_work;
  350. struct dma_pool *dma_pool;
  351. dma_addr_t dma_p_addr;
  352. void *dma_v_addr;
  353. dma_addr_t ci_p_addr;
  354. struct ctx_info *ci;
  355. struct ia ia;
  356. struct txq txq;
  357. struct rxq rxq;
  358. u32 alive_intr_ctxt;
  359. };
  360. static inline u32 btintel_pcie_rd_reg32(struct btintel_pcie_data *data,
  361. u32 offset)
  362. {
  363. return ioread32(data->base_addr + offset);
  364. }
  365. static inline void btintel_pcie_wr_reg8(struct btintel_pcie_data *data,
  366. u32 offset, u8 val)
  367. {
  368. iowrite8(val, data->base_addr + offset);
  369. }
  370. static inline void btintel_pcie_wr_reg32(struct btintel_pcie_data *data,
  371. u32 offset, u32 val)
  372. {
  373. iowrite32(val, data->base_addr + offset);
  374. }
  375. static inline void btintel_pcie_set_reg_bits(struct btintel_pcie_data *data,
  376. u32 offset, u32 bits)
  377. {
  378. u32 r;
  379. r = ioread32(data->base_addr + offset);
  380. r |= bits;
  381. iowrite32(r, data->base_addr + offset);
  382. }
  383. static inline void btintel_pcie_clr_reg_bits(struct btintel_pcie_data *data,
  384. u32 offset, u32 bits)
  385. {
  386. u32 r;
  387. r = ioread32(data->base_addr + offset);
  388. r &= ~bits;
  389. iowrite32(r, data->base_addr + offset);
  390. }