18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
28c2ecf20Sopenharmony_ci/* Copyright(c) 2018-2019  Realtek Corporation
38c2ecf20Sopenharmony_ci */
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#ifndef __RTK_PCI_H_
68c2ecf20Sopenharmony_ci#define __RTK_PCI_H_
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#define RTK_DEFAULT_TX_DESC_NUM 128
98c2ecf20Sopenharmony_ci#define RTK_BEQ_TX_DESC_NUM	256
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#define RTK_MAX_RX_DESC_NUM	512
128c2ecf20Sopenharmony_ci/* 11K + rx desc size */
138c2ecf20Sopenharmony_ci#define RTK_PCI_RX_BUF_SIZE	(11454 + 24)
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#define RTK_PCI_CTRL		0x300
168c2ecf20Sopenharmony_ci#define BIT_RST_TRXDMA_INTF	BIT(20)
178c2ecf20Sopenharmony_ci#define BIT_RX_TAG_EN		BIT(15)
188c2ecf20Sopenharmony_ci#define REG_DBI_WDATA_V1	0x03E8
198c2ecf20Sopenharmony_ci#define REG_DBI_RDATA_V1	0x03EC
208c2ecf20Sopenharmony_ci#define REG_DBI_FLAG_V1		0x03F0
218c2ecf20Sopenharmony_ci#define BIT_DBI_RFLAG		BIT(17)
228c2ecf20Sopenharmony_ci#define BIT_DBI_WFLAG		BIT(16)
238c2ecf20Sopenharmony_ci#define BITS_DBI_WREN		GENMASK(15, 12)
248c2ecf20Sopenharmony_ci#define BITS_DBI_ADDR_MASK	GENMASK(11, 2)
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define REG_MDIO_V1		0x03F4
278c2ecf20Sopenharmony_ci#define REG_PCIE_MIX_CFG	0x03F8
288c2ecf20Sopenharmony_ci#define BITS_MDIO_ADDR_MASK	GENMASK(4, 0)
298c2ecf20Sopenharmony_ci#define BIT_MDIO_WFLAG_V1	BIT(5)
308c2ecf20Sopenharmony_ci#define RTW_PCI_MDIO_PG_SZ	BIT(5)
318c2ecf20Sopenharmony_ci#define RTW_PCI_MDIO_PG_OFFS_G1	0
328c2ecf20Sopenharmony_ci#define RTW_PCI_MDIO_PG_OFFS_G2	2
338c2ecf20Sopenharmony_ci#define RTW_PCI_WR_RETRY_CNT	20
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define RTK_PCIE_LINK_CFG	0x0719
368c2ecf20Sopenharmony_ci#define BIT_CLKREQ_SW_EN	BIT(4)
378c2ecf20Sopenharmony_ci#define BIT_L1_SW_EN		BIT(3)
388c2ecf20Sopenharmony_ci#define RTK_PCIE_CLKDLY_CTRL	0x0725
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#define BIT_PCI_BCNQ_FLAG	BIT(4)
418c2ecf20Sopenharmony_ci#define RTK_PCI_TXBD_DESA_BCNQ	0x308
428c2ecf20Sopenharmony_ci#define RTK_PCI_TXBD_DESA_H2CQ	0x1320
438c2ecf20Sopenharmony_ci#define RTK_PCI_TXBD_DESA_MGMTQ	0x310
448c2ecf20Sopenharmony_ci#define RTK_PCI_TXBD_DESA_BKQ	0x330
458c2ecf20Sopenharmony_ci#define RTK_PCI_TXBD_DESA_BEQ	0x328
468c2ecf20Sopenharmony_ci#define RTK_PCI_TXBD_DESA_VIQ	0x320
478c2ecf20Sopenharmony_ci#define RTK_PCI_TXBD_DESA_VOQ	0x318
488c2ecf20Sopenharmony_ci#define RTK_PCI_TXBD_DESA_HI0Q	0x340
498c2ecf20Sopenharmony_ci#define RTK_PCI_RXBD_DESA_MPDUQ	0x338
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#define TRX_BD_IDX_MASK		GENMASK(11, 0)
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci/* BCNQ is specialized for rsvd page, does not need to specify a number */
548c2ecf20Sopenharmony_ci#define RTK_PCI_TXBD_NUM_H2CQ	0x1328
558c2ecf20Sopenharmony_ci#define RTK_PCI_TXBD_NUM_MGMTQ	0x380
568c2ecf20Sopenharmony_ci#define RTK_PCI_TXBD_NUM_BKQ	0x38A
578c2ecf20Sopenharmony_ci#define RTK_PCI_TXBD_NUM_BEQ	0x388
588c2ecf20Sopenharmony_ci#define RTK_PCI_TXBD_NUM_VIQ	0x386
598c2ecf20Sopenharmony_ci#define RTK_PCI_TXBD_NUM_VOQ	0x384
608c2ecf20Sopenharmony_ci#define RTK_PCI_TXBD_NUM_HI0Q	0x38C
618c2ecf20Sopenharmony_ci#define RTK_PCI_RXBD_NUM_MPDUQ	0x382
628c2ecf20Sopenharmony_ci#define RTK_PCI_TXBD_IDX_H2CQ	0x132C
638c2ecf20Sopenharmony_ci#define RTK_PCI_TXBD_IDX_MGMTQ	0x3B0
648c2ecf20Sopenharmony_ci#define RTK_PCI_TXBD_IDX_BKQ	0x3AC
658c2ecf20Sopenharmony_ci#define RTK_PCI_TXBD_IDX_BEQ	0x3A8
668c2ecf20Sopenharmony_ci#define RTK_PCI_TXBD_IDX_VIQ	0x3A4
678c2ecf20Sopenharmony_ci#define RTK_PCI_TXBD_IDX_VOQ	0x3A0
688c2ecf20Sopenharmony_ci#define RTK_PCI_TXBD_IDX_HI0Q	0x3B8
698c2ecf20Sopenharmony_ci#define RTK_PCI_RXBD_IDX_MPDUQ	0x3B4
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci#define RTK_PCI_TXBD_RWPTR_CLR	0x39C
728c2ecf20Sopenharmony_ci#define RTK_PCI_TXBD_H2CQ_CSR	0x1330
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci#define BIT_CLR_H2CQ_HOST_IDX	BIT(16)
758c2ecf20Sopenharmony_ci#define BIT_CLR_H2CQ_HW_IDX	BIT(8)
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#define RTK_PCI_HIMR0		0x0B0
788c2ecf20Sopenharmony_ci#define RTK_PCI_HISR0		0x0B4
798c2ecf20Sopenharmony_ci#define RTK_PCI_HIMR1		0x0B8
808c2ecf20Sopenharmony_ci#define RTK_PCI_HISR1		0x0BC
818c2ecf20Sopenharmony_ci#define RTK_PCI_HIMR2		0x10B0
828c2ecf20Sopenharmony_ci#define RTK_PCI_HISR2		0x10B4
838c2ecf20Sopenharmony_ci#define RTK_PCI_HIMR3		0x10B8
848c2ecf20Sopenharmony_ci#define RTK_PCI_HISR3		0x10BC
858c2ecf20Sopenharmony_ci/* IMR 0 */
868c2ecf20Sopenharmony_ci#define IMR_TIMER2		BIT(31)
878c2ecf20Sopenharmony_ci#define IMR_TIMER1		BIT(30)
888c2ecf20Sopenharmony_ci#define IMR_PSTIMEOUT		BIT(29)
898c2ecf20Sopenharmony_ci#define IMR_GTINT4		BIT(28)
908c2ecf20Sopenharmony_ci#define IMR_GTINT3		BIT(27)
918c2ecf20Sopenharmony_ci#define IMR_TBDER		BIT(26)
928c2ecf20Sopenharmony_ci#define IMR_TBDOK		BIT(25)
938c2ecf20Sopenharmony_ci#define IMR_TSF_BIT32_TOGGLE	BIT(24)
948c2ecf20Sopenharmony_ci#define IMR_BCNDMAINT0		BIT(20)
958c2ecf20Sopenharmony_ci#define IMR_BCNDOK0		BIT(16)
968c2ecf20Sopenharmony_ci#define IMR_HSISR_IND_ON_INT	BIT(15)
978c2ecf20Sopenharmony_ci#define IMR_BCNDMAINT_E		BIT(14)
988c2ecf20Sopenharmony_ci#define IMR_ATIMEND		BIT(12)
998c2ecf20Sopenharmony_ci#define IMR_HISR1_IND_INT	BIT(11)
1008c2ecf20Sopenharmony_ci#define IMR_C2HCMD		BIT(10)
1018c2ecf20Sopenharmony_ci#define IMR_CPWM2		BIT(9)
1028c2ecf20Sopenharmony_ci#define IMR_CPWM		BIT(8)
1038c2ecf20Sopenharmony_ci#define IMR_HIGHDOK		BIT(7)
1048c2ecf20Sopenharmony_ci#define IMR_MGNTDOK		BIT(6)
1058c2ecf20Sopenharmony_ci#define IMR_BKDOK		BIT(5)
1068c2ecf20Sopenharmony_ci#define IMR_BEDOK		BIT(4)
1078c2ecf20Sopenharmony_ci#define IMR_VIDOK		BIT(3)
1088c2ecf20Sopenharmony_ci#define IMR_VODOK		BIT(2)
1098c2ecf20Sopenharmony_ci#define IMR_RDU			BIT(1)
1108c2ecf20Sopenharmony_ci#define IMR_ROK			BIT(0)
1118c2ecf20Sopenharmony_ci/* IMR 1 */
1128c2ecf20Sopenharmony_ci#define IMR_TXFIFO_TH_INT	BIT(30)
1138c2ecf20Sopenharmony_ci#define IMR_BTON_STS_UPDATE	BIT(29)
1148c2ecf20Sopenharmony_ci#define IMR_MCUERR		BIT(28)
1158c2ecf20Sopenharmony_ci#define IMR_BCNDMAINT7		BIT(27)
1168c2ecf20Sopenharmony_ci#define IMR_BCNDMAINT6		BIT(26)
1178c2ecf20Sopenharmony_ci#define IMR_BCNDMAINT5		BIT(25)
1188c2ecf20Sopenharmony_ci#define IMR_BCNDMAINT4		BIT(24)
1198c2ecf20Sopenharmony_ci#define IMR_BCNDMAINT3		BIT(23)
1208c2ecf20Sopenharmony_ci#define IMR_BCNDMAINT2		BIT(22)
1218c2ecf20Sopenharmony_ci#define IMR_BCNDMAINT1		BIT(21)
1228c2ecf20Sopenharmony_ci#define IMR_BCNDOK7		BIT(20)
1238c2ecf20Sopenharmony_ci#define IMR_BCNDOK6		BIT(19)
1248c2ecf20Sopenharmony_ci#define IMR_BCNDOK5		BIT(18)
1258c2ecf20Sopenharmony_ci#define IMR_BCNDOK4		BIT(17)
1268c2ecf20Sopenharmony_ci#define IMR_BCNDOK3		BIT(16)
1278c2ecf20Sopenharmony_ci#define IMR_BCNDOK2		BIT(15)
1288c2ecf20Sopenharmony_ci#define IMR_BCNDOK1		BIT(14)
1298c2ecf20Sopenharmony_ci#define IMR_ATIMEND_E		BIT(13)
1308c2ecf20Sopenharmony_ci#define IMR_ATIMEND		BIT(12)
1318c2ecf20Sopenharmony_ci#define IMR_TXERR		BIT(11)
1328c2ecf20Sopenharmony_ci#define IMR_RXERR		BIT(10)
1338c2ecf20Sopenharmony_ci#define IMR_TXFOVW		BIT(9)
1348c2ecf20Sopenharmony_ci#define IMR_RXFOVW		BIT(8)
1358c2ecf20Sopenharmony_ci#define IMR_CPU_MGQ_TXDONE	BIT(5)
1368c2ecf20Sopenharmony_ci#define IMR_PS_TIMER_C		BIT(4)
1378c2ecf20Sopenharmony_ci#define IMR_PS_TIMER_B		BIT(3)
1388c2ecf20Sopenharmony_ci#define IMR_PS_TIMER_A		BIT(2)
1398c2ecf20Sopenharmony_ci#define IMR_CPUMGQ_TX_TIMER	BIT(1)
1408c2ecf20Sopenharmony_ci/* IMR 3 */
1418c2ecf20Sopenharmony_ci#define IMR_H2CDOK		BIT(16)
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci/* one element is reserved to know if the ring is closed */
1448c2ecf20Sopenharmony_cistatic inline int avail_desc(u32 wp, u32 rp, u32 len)
1458c2ecf20Sopenharmony_ci{
1468c2ecf20Sopenharmony_ci	if (rp > wp)
1478c2ecf20Sopenharmony_ci		return rp - wp - 1;
1488c2ecf20Sopenharmony_ci	else
1498c2ecf20Sopenharmony_ci		return len - wp + rp - 1;
1508c2ecf20Sopenharmony_ci}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci#define RTK_PCI_TXBD_OWN_OFFSET 15
1538c2ecf20Sopenharmony_ci#define RTK_PCI_TXBD_BCN_WORK	0x383
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cistruct rtw_pci_tx_buffer_desc {
1568c2ecf20Sopenharmony_ci	__le16 buf_size;
1578c2ecf20Sopenharmony_ci	__le16 psb_len;
1588c2ecf20Sopenharmony_ci	__le32 dma;
1598c2ecf20Sopenharmony_ci};
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistruct rtw_pci_tx_data {
1628c2ecf20Sopenharmony_ci	dma_addr_t dma;
1638c2ecf20Sopenharmony_ci	u8 sn;
1648c2ecf20Sopenharmony_ci};
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cistruct rtw_pci_ring {
1678c2ecf20Sopenharmony_ci	u8 *head;
1688c2ecf20Sopenharmony_ci	dma_addr_t dma;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	u8 desc_size;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	u32 len;
1738c2ecf20Sopenharmony_ci	u32 wp;
1748c2ecf20Sopenharmony_ci	u32 rp;
1758c2ecf20Sopenharmony_ci};
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistruct rtw_pci_tx_ring {
1788c2ecf20Sopenharmony_ci	struct rtw_pci_ring r;
1798c2ecf20Sopenharmony_ci	struct sk_buff_head queue;
1808c2ecf20Sopenharmony_ci	bool queue_stopped;
1818c2ecf20Sopenharmony_ci};
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_cistruct rtw_pci_rx_buffer_desc {
1848c2ecf20Sopenharmony_ci	__le16 buf_size;
1858c2ecf20Sopenharmony_ci	__le16 total_pkt_size;
1868c2ecf20Sopenharmony_ci	__le32 dma;
1878c2ecf20Sopenharmony_ci};
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_cistruct rtw_pci_rx_ring {
1908c2ecf20Sopenharmony_ci	struct rtw_pci_ring r;
1918c2ecf20Sopenharmony_ci	struct sk_buff *buf[RTK_MAX_RX_DESC_NUM];
1928c2ecf20Sopenharmony_ci};
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci#define RX_TAG_MAX	8192
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_cistruct rtw_pci {
1978c2ecf20Sopenharmony_ci	struct pci_dev *pdev;
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	/* Used for PCI interrupt. */
2008c2ecf20Sopenharmony_ci	spinlock_t hwirq_lock;
2018c2ecf20Sopenharmony_ci	/* Used for PCI TX queueing. */
2028c2ecf20Sopenharmony_ci	spinlock_t irq_lock;
2038c2ecf20Sopenharmony_ci	u32 irq_mask[4];
2048c2ecf20Sopenharmony_ci	bool irq_enabled;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	u16 rx_tag;
2078c2ecf20Sopenharmony_ci	DECLARE_BITMAP(tx_queued, RTK_MAX_TX_QUEUE_NUM);
2088c2ecf20Sopenharmony_ci	struct rtw_pci_tx_ring tx_rings[RTK_MAX_TX_QUEUE_NUM];
2098c2ecf20Sopenharmony_ci	struct rtw_pci_rx_ring rx_rings[RTK_MAX_RX_QUEUE_NUM];
2108c2ecf20Sopenharmony_ci	u16 link_ctrl;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	void __iomem *mmap;
2138c2ecf20Sopenharmony_ci};
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_cistatic inline u32 max_num_of_tx_queue(u8 queue)
2168c2ecf20Sopenharmony_ci{
2178c2ecf20Sopenharmony_ci	u32 max_num;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	switch (queue) {
2208c2ecf20Sopenharmony_ci	case RTW_TX_QUEUE_BE:
2218c2ecf20Sopenharmony_ci		max_num = RTK_BEQ_TX_DESC_NUM;
2228c2ecf20Sopenharmony_ci		break;
2238c2ecf20Sopenharmony_ci	case RTW_TX_QUEUE_BCN:
2248c2ecf20Sopenharmony_ci		max_num = 1;
2258c2ecf20Sopenharmony_ci		break;
2268c2ecf20Sopenharmony_ci	default:
2278c2ecf20Sopenharmony_ci		max_num = RTK_DEFAULT_TX_DESC_NUM;
2288c2ecf20Sopenharmony_ci		break;
2298c2ecf20Sopenharmony_ci	}
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	return max_num;
2328c2ecf20Sopenharmony_ci}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_cistatic inline struct
2358c2ecf20Sopenharmony_cirtw_pci_tx_data *rtw_pci_get_tx_data(struct sk_buff *skb)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof(struct rtw_pci_tx_data) >
2408c2ecf20Sopenharmony_ci		     sizeof(info->status.status_driver_data));
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	return (struct rtw_pci_tx_data *)info->status.status_driver_data;
2438c2ecf20Sopenharmony_ci}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_cistatic inline
2468c2ecf20Sopenharmony_cistruct rtw_pci_tx_buffer_desc *get_tx_buffer_desc(struct rtw_pci_tx_ring *ring,
2478c2ecf20Sopenharmony_ci						  u32 size)
2488c2ecf20Sopenharmony_ci{
2498c2ecf20Sopenharmony_ci	u8 *buf_desc;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	buf_desc = ring->r.head + ring->r.wp * size;
2528c2ecf20Sopenharmony_ci	return (struct rtw_pci_tx_buffer_desc *)buf_desc;
2538c2ecf20Sopenharmony_ci}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci#endif
256