xref: /freebsd/sys/contrib/dev/athk/ath10k/pci.h (revision da8fa4e37a0c048a67d7baa3b5a9bed637d02564)
1*da8fa4e3SBjoern A. Zeeb /* SPDX-License-Identifier: ISC */
2*da8fa4e3SBjoern A. Zeeb /*
3*da8fa4e3SBjoern A. Zeeb  * Copyright (c) 2005-2011 Atheros Communications Inc.
4*da8fa4e3SBjoern A. Zeeb  * Copyright (c) 2011-2017 Qualcomm Atheros, Inc.
5*da8fa4e3SBjoern A. Zeeb  */
6*da8fa4e3SBjoern A. Zeeb 
7*da8fa4e3SBjoern A. Zeeb #ifndef _PCI_H_
8*da8fa4e3SBjoern A. Zeeb #define _PCI_H_
9*da8fa4e3SBjoern A. Zeeb 
10*da8fa4e3SBjoern A. Zeeb #include <linux/interrupt.h>
11*da8fa4e3SBjoern A. Zeeb #include <linux/mutex.h>
12*da8fa4e3SBjoern A. Zeeb 
13*da8fa4e3SBjoern A. Zeeb #include "hw.h"
14*da8fa4e3SBjoern A. Zeeb #include "ce.h"
15*da8fa4e3SBjoern A. Zeeb #include "ahb.h"
16*da8fa4e3SBjoern A. Zeeb 
17*da8fa4e3SBjoern A. Zeeb /*
18*da8fa4e3SBjoern A. Zeeb  * maximum number of bytes that can be
19*da8fa4e3SBjoern A. Zeeb  * handled atomically by DiagRead/DiagWrite
20*da8fa4e3SBjoern A. Zeeb  */
21*da8fa4e3SBjoern A. Zeeb #define DIAG_TRANSFER_LIMIT 2048
22*da8fa4e3SBjoern A. Zeeb 
23*da8fa4e3SBjoern A. Zeeb struct bmi_xfer {
24*da8fa4e3SBjoern A. Zeeb 	bool tx_done;
25*da8fa4e3SBjoern A. Zeeb 	bool rx_done;
26*da8fa4e3SBjoern A. Zeeb 	bool wait_for_resp;
27*da8fa4e3SBjoern A. Zeeb 	u32 resp_len;
28*da8fa4e3SBjoern A. Zeeb };
29*da8fa4e3SBjoern A. Zeeb 
30*da8fa4e3SBjoern A. Zeeb /*
31*da8fa4e3SBjoern A. Zeeb  * PCI-specific Target state
32*da8fa4e3SBjoern A. Zeeb  *
33*da8fa4e3SBjoern A. Zeeb  * NOTE: Structure is shared between Host software and Target firmware!
34*da8fa4e3SBjoern A. Zeeb  *
35*da8fa4e3SBjoern A. Zeeb  * Much of this may be of interest to the Host so
36*da8fa4e3SBjoern A. Zeeb  * HOST_INTEREST->hi_interconnect_state points here
37*da8fa4e3SBjoern A. Zeeb  * (and all members are 32-bit quantities in order to
38*da8fa4e3SBjoern A. Zeeb  * facilitate Host access). In particular, Host software is
39*da8fa4e3SBjoern A. Zeeb  * required to initialize pipe_cfg_addr and svc_to_pipe_map.
40*da8fa4e3SBjoern A. Zeeb  */
41*da8fa4e3SBjoern A. Zeeb struct pcie_state {
42*da8fa4e3SBjoern A. Zeeb 	/* Pipe configuration Target address */
43*da8fa4e3SBjoern A. Zeeb 	/* NB: ce_pipe_config[CE_COUNT] */
44*da8fa4e3SBjoern A. Zeeb 	u32 pipe_cfg_addr;
45*da8fa4e3SBjoern A. Zeeb 
46*da8fa4e3SBjoern A. Zeeb 	/* Service to pipe map Target address */
47*da8fa4e3SBjoern A. Zeeb 	/* NB: service_to_pipe[PIPE_TO_CE_MAP_CN] */
48*da8fa4e3SBjoern A. Zeeb 	u32 svc_to_pipe_map;
49*da8fa4e3SBjoern A. Zeeb 
50*da8fa4e3SBjoern A. Zeeb 	/* number of MSI interrupts requested */
51*da8fa4e3SBjoern A. Zeeb 	u32 msi_requested;
52*da8fa4e3SBjoern A. Zeeb 
53*da8fa4e3SBjoern A. Zeeb 	/* number of MSI interrupts granted */
54*da8fa4e3SBjoern A. Zeeb 	u32 msi_granted;
55*da8fa4e3SBjoern A. Zeeb 
56*da8fa4e3SBjoern A. Zeeb 	/* Message Signalled Interrupt address */
57*da8fa4e3SBjoern A. Zeeb 	u32 msi_addr;
58*da8fa4e3SBjoern A. Zeeb 
59*da8fa4e3SBjoern A. Zeeb 	/* Base data */
60*da8fa4e3SBjoern A. Zeeb 	u32 msi_data;
61*da8fa4e3SBjoern A. Zeeb 
62*da8fa4e3SBjoern A. Zeeb 	/*
63*da8fa4e3SBjoern A. Zeeb 	 * Data for firmware interrupt;
64*da8fa4e3SBjoern A. Zeeb 	 * MSI data for other interrupts are
65*da8fa4e3SBjoern A. Zeeb 	 * in various SoC registers
66*da8fa4e3SBjoern A. Zeeb 	 */
67*da8fa4e3SBjoern A. Zeeb 	u32 msi_fw_intr_data;
68*da8fa4e3SBjoern A. Zeeb 
69*da8fa4e3SBjoern A. Zeeb 	/* PCIE_PWR_METHOD_* */
70*da8fa4e3SBjoern A. Zeeb 	u32 power_mgmt_method;
71*da8fa4e3SBjoern A. Zeeb 
72*da8fa4e3SBjoern A. Zeeb 	/* PCIE_CONFIG_FLAG_* */
73*da8fa4e3SBjoern A. Zeeb 	u32 config_flags;
74*da8fa4e3SBjoern A. Zeeb };
75*da8fa4e3SBjoern A. Zeeb 
76*da8fa4e3SBjoern A. Zeeb /* PCIE_CONFIG_FLAG definitions */
77*da8fa4e3SBjoern A. Zeeb #define PCIE_CONFIG_FLAG_ENABLE_L1  0x0000001
78*da8fa4e3SBjoern A. Zeeb 
79*da8fa4e3SBjoern A. Zeeb /* Per-pipe state. */
80*da8fa4e3SBjoern A. Zeeb struct ath10k_pci_pipe {
81*da8fa4e3SBjoern A. Zeeb 	/* Handle of underlying Copy Engine */
82*da8fa4e3SBjoern A. Zeeb 	struct ath10k_ce_pipe *ce_hdl;
83*da8fa4e3SBjoern A. Zeeb 
84*da8fa4e3SBjoern A. Zeeb 	/* Our pipe number; facilitiates use of pipe_info ptrs. */
85*da8fa4e3SBjoern A. Zeeb 	u8 pipe_num;
86*da8fa4e3SBjoern A. Zeeb 
87*da8fa4e3SBjoern A. Zeeb 	/* Convenience back pointer to hif_ce_state. */
88*da8fa4e3SBjoern A. Zeeb 	struct ath10k *hif_ce_state;
89*da8fa4e3SBjoern A. Zeeb 
90*da8fa4e3SBjoern A. Zeeb 	size_t buf_sz;
91*da8fa4e3SBjoern A. Zeeb 
92*da8fa4e3SBjoern A. Zeeb 	/* protects compl_free and num_send_allowed */
93*da8fa4e3SBjoern A. Zeeb 	spinlock_t pipe_lock;
94*da8fa4e3SBjoern A. Zeeb };
95*da8fa4e3SBjoern A. Zeeb 
96*da8fa4e3SBjoern A. Zeeb struct ath10k_pci_supp_chip {
97*da8fa4e3SBjoern A. Zeeb 	u32 dev_id;
98*da8fa4e3SBjoern A. Zeeb 	u32 rev_id;
99*da8fa4e3SBjoern A. Zeeb };
100*da8fa4e3SBjoern A. Zeeb 
101*da8fa4e3SBjoern A. Zeeb enum ath10k_pci_irq_mode {
102*da8fa4e3SBjoern A. Zeeb 	ATH10K_PCI_IRQ_AUTO = 0,
103*da8fa4e3SBjoern A. Zeeb 	ATH10K_PCI_IRQ_LEGACY = 1,
104*da8fa4e3SBjoern A. Zeeb 	ATH10K_PCI_IRQ_MSI = 2,
105*da8fa4e3SBjoern A. Zeeb };
106*da8fa4e3SBjoern A. Zeeb 
107*da8fa4e3SBjoern A. Zeeb struct ath10k_pci {
108*da8fa4e3SBjoern A. Zeeb 	struct pci_dev *pdev;
109*da8fa4e3SBjoern A. Zeeb 	struct device *dev;
110*da8fa4e3SBjoern A. Zeeb 	struct ath10k *ar;
111*da8fa4e3SBjoern A. Zeeb 	void __iomem *mem;
112*da8fa4e3SBjoern A. Zeeb 	size_t mem_len;
113*da8fa4e3SBjoern A. Zeeb 
114*da8fa4e3SBjoern A. Zeeb 	/* Operating interrupt mode */
115*da8fa4e3SBjoern A. Zeeb 	enum ath10k_pci_irq_mode oper_irq_mode;
116*da8fa4e3SBjoern A. Zeeb 
117*da8fa4e3SBjoern A. Zeeb 	struct ath10k_pci_pipe pipe_info[CE_COUNT_MAX];
118*da8fa4e3SBjoern A. Zeeb 
119*da8fa4e3SBjoern A. Zeeb 	/* Copy Engine used for Diagnostic Accesses */
120*da8fa4e3SBjoern A. Zeeb 	struct ath10k_ce_pipe *ce_diag;
121*da8fa4e3SBjoern A. Zeeb 	/* For protecting ce_diag */
122*da8fa4e3SBjoern A. Zeeb 	struct mutex ce_diag_mutex;
123*da8fa4e3SBjoern A. Zeeb 
124*da8fa4e3SBjoern A. Zeeb 	struct work_struct dump_work;
125*da8fa4e3SBjoern A. Zeeb 
126*da8fa4e3SBjoern A. Zeeb 	struct ath10k_ce ce;
127*da8fa4e3SBjoern A. Zeeb 	struct timer_list rx_post_retry;
128*da8fa4e3SBjoern A. Zeeb 
129*da8fa4e3SBjoern A. Zeeb 	/* Due to HW quirks it is recommended to disable ASPM during device
130*da8fa4e3SBjoern A. Zeeb 	 * bootup. To do that the original PCI-E Link Control is stored before
131*da8fa4e3SBjoern A. Zeeb 	 * device bootup is executed and re-programmed later.
132*da8fa4e3SBjoern A. Zeeb 	 */
133*da8fa4e3SBjoern A. Zeeb 	u16 link_ctl;
134*da8fa4e3SBjoern A. Zeeb 
135*da8fa4e3SBjoern A. Zeeb 	/* Protects ps_awake and ps_wake_refcount */
136*da8fa4e3SBjoern A. Zeeb 	spinlock_t ps_lock;
137*da8fa4e3SBjoern A. Zeeb 
138*da8fa4e3SBjoern A. Zeeb 	/* The device has a special powersave-oriented register. When device is
139*da8fa4e3SBjoern A. Zeeb 	 * considered asleep it drains less power and driver is forbidden from
140*da8fa4e3SBjoern A. Zeeb 	 * accessing most MMIO registers. If host were to access them without
141*da8fa4e3SBjoern A. Zeeb 	 * waking up the device might scribble over host memory or return
142*da8fa4e3SBjoern A. Zeeb 	 * 0xdeadbeef readouts.
143*da8fa4e3SBjoern A. Zeeb 	 */
144*da8fa4e3SBjoern A. Zeeb 	unsigned long ps_wake_refcount;
145*da8fa4e3SBjoern A. Zeeb 
146*da8fa4e3SBjoern A. Zeeb 	/* Waking up takes some time (up to 2ms in some cases) so it can be bad
147*da8fa4e3SBjoern A. Zeeb 	 * for latency. To mitigate this the device isn't immediately allowed
148*da8fa4e3SBjoern A. Zeeb 	 * to sleep after all references are undone - instead there's a grace
149*da8fa4e3SBjoern A. Zeeb 	 * period after which the powersave register is updated unless some
150*da8fa4e3SBjoern A. Zeeb 	 * activity to/from device happened in the meantime.
151*da8fa4e3SBjoern A. Zeeb 	 *
152*da8fa4e3SBjoern A. Zeeb 	 * Also see comments on ATH10K_PCI_SLEEP_GRACE_PERIOD_MSEC.
153*da8fa4e3SBjoern A. Zeeb 	 */
154*da8fa4e3SBjoern A. Zeeb 	struct timer_list ps_timer;
155*da8fa4e3SBjoern A. Zeeb 
156*da8fa4e3SBjoern A. Zeeb 	/* MMIO registers are used to communicate with the device. With
157*da8fa4e3SBjoern A. Zeeb 	 * intensive traffic accessing powersave register would be a bit
158*da8fa4e3SBjoern A. Zeeb 	 * wasteful overhead and would needlessly stall CPU. It is far more
159*da8fa4e3SBjoern A. Zeeb 	 * efficient to rely on a variable in RAM and update it only upon
160*da8fa4e3SBjoern A. Zeeb 	 * powersave register state changes.
161*da8fa4e3SBjoern A. Zeeb 	 */
162*da8fa4e3SBjoern A. Zeeb 	bool ps_awake;
163*da8fa4e3SBjoern A. Zeeb 
164*da8fa4e3SBjoern A. Zeeb 	/* pci power save, disable for QCA988X and QCA99X0.
165*da8fa4e3SBjoern A. Zeeb 	 * Writing 'false' to this variable avoids frequent locking
166*da8fa4e3SBjoern A. Zeeb 	 * on MMIO read/write.
167*da8fa4e3SBjoern A. Zeeb 	 */
168*da8fa4e3SBjoern A. Zeeb 	bool pci_ps;
169*da8fa4e3SBjoern A. Zeeb 
170*da8fa4e3SBjoern A. Zeeb 	/* Chip specific pci reset routine used to do a safe reset */
171*da8fa4e3SBjoern A. Zeeb 	int (*pci_soft_reset)(struct ath10k *ar);
172*da8fa4e3SBjoern A. Zeeb 
173*da8fa4e3SBjoern A. Zeeb 	/* Chip specific pci full reset function */
174*da8fa4e3SBjoern A. Zeeb 	int (*pci_hard_reset)(struct ath10k *ar);
175*da8fa4e3SBjoern A. Zeeb 
176*da8fa4e3SBjoern A. Zeeb 	/* chip specific methods for converting target CPU virtual address
177*da8fa4e3SBjoern A. Zeeb 	 * space to CE address space
178*da8fa4e3SBjoern A. Zeeb 	 */
179*da8fa4e3SBjoern A. Zeeb 	u32 (*targ_cpu_to_ce_addr)(struct ath10k *ar, u32 addr);
180*da8fa4e3SBjoern A. Zeeb 
181*da8fa4e3SBjoern A. Zeeb 	struct ce_attr *attr;
182*da8fa4e3SBjoern A. Zeeb 	struct ce_pipe_config *pipe_config;
183*da8fa4e3SBjoern A. Zeeb 	struct ce_service_to_pipe *serv_to_pipe;
184*da8fa4e3SBjoern A. Zeeb 
185*da8fa4e3SBjoern A. Zeeb 	/* Keep this entry in the last, memory for struct ath10k_ahb is
186*da8fa4e3SBjoern A. Zeeb 	 * allocated (ahb support enabled case) in the continuation of
187*da8fa4e3SBjoern A. Zeeb 	 * this struct.
188*da8fa4e3SBjoern A. Zeeb 	 */
189*da8fa4e3SBjoern A. Zeeb 	struct ath10k_ahb ahb[];
190*da8fa4e3SBjoern A. Zeeb 
191*da8fa4e3SBjoern A. Zeeb };
192*da8fa4e3SBjoern A. Zeeb 
193*da8fa4e3SBjoern A. Zeeb static inline struct ath10k_pci *ath10k_pci_priv(struct ath10k *ar)
194*da8fa4e3SBjoern A. Zeeb {
195*da8fa4e3SBjoern A. Zeeb 	return (struct ath10k_pci *)ar->drv_priv;
196*da8fa4e3SBjoern A. Zeeb }
197*da8fa4e3SBjoern A. Zeeb 
198*da8fa4e3SBjoern A. Zeeb #define ATH10K_PCI_RX_POST_RETRY_MS 50
199*da8fa4e3SBjoern A. Zeeb #define ATH_PCI_RESET_WAIT_MAX 10 /* ms */
200*da8fa4e3SBjoern A. Zeeb #define PCIE_WAKE_TIMEOUT 30000	/* 30ms */
201*da8fa4e3SBjoern A. Zeeb #define PCIE_WAKE_LATE_US 10000	/* 10ms */
202*da8fa4e3SBjoern A. Zeeb 
203*da8fa4e3SBjoern A. Zeeb #define BAR_NUM 0
204*da8fa4e3SBjoern A. Zeeb 
205*da8fa4e3SBjoern A. Zeeb #define CDC_WAR_MAGIC_STR   0xceef0000
206*da8fa4e3SBjoern A. Zeeb #define CDC_WAR_DATA_CE     4
207*da8fa4e3SBjoern A. Zeeb 
208*da8fa4e3SBjoern A. Zeeb /* Wait up to this many Ms for a Diagnostic Access CE operation to complete */
209*da8fa4e3SBjoern A. Zeeb #define DIAG_ACCESS_CE_TIMEOUT_US 10000 /* 10 ms */
210*da8fa4e3SBjoern A. Zeeb #define DIAG_ACCESS_CE_WAIT_US	50
211*da8fa4e3SBjoern A. Zeeb 
212*da8fa4e3SBjoern A. Zeeb void ath10k_pci_write32(struct ath10k *ar, u32 offset, u32 value);
213*da8fa4e3SBjoern A. Zeeb void ath10k_pci_soc_write32(struct ath10k *ar, u32 addr, u32 val);
214*da8fa4e3SBjoern A. Zeeb void ath10k_pci_reg_write32(struct ath10k *ar, u32 addr, u32 val);
215*da8fa4e3SBjoern A. Zeeb 
216*da8fa4e3SBjoern A. Zeeb u32 ath10k_pci_read32(struct ath10k *ar, u32 offset);
217*da8fa4e3SBjoern A. Zeeb u32 ath10k_pci_soc_read32(struct ath10k *ar, u32 addr);
218*da8fa4e3SBjoern A. Zeeb u32 ath10k_pci_reg_read32(struct ath10k *ar, u32 addr);
219*da8fa4e3SBjoern A. Zeeb 
220*da8fa4e3SBjoern A. Zeeb int ath10k_pci_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
221*da8fa4e3SBjoern A. Zeeb 			 struct ath10k_hif_sg_item *items, int n_items);
222*da8fa4e3SBjoern A. Zeeb int ath10k_pci_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
223*da8fa4e3SBjoern A. Zeeb 			     size_t buf_len);
224*da8fa4e3SBjoern A. Zeeb int ath10k_pci_diag_write_mem(struct ath10k *ar, u32 address,
225*da8fa4e3SBjoern A. Zeeb 			      const void *data, int nbytes);
226*da8fa4e3SBjoern A. Zeeb int ath10k_pci_hif_exchange_bmi_msg(struct ath10k *ar, void *req, u32 req_len,
227*da8fa4e3SBjoern A. Zeeb 				    void *resp, u32 *resp_len);
228*da8fa4e3SBjoern A. Zeeb int ath10k_pci_hif_map_service_to_pipe(struct ath10k *ar, u16 service_id,
229*da8fa4e3SBjoern A. Zeeb 				       u8 *ul_pipe, u8 *dl_pipe);
230*da8fa4e3SBjoern A. Zeeb void ath10k_pci_hif_get_default_pipe(struct ath10k *ar, u8 *ul_pipe,
231*da8fa4e3SBjoern A. Zeeb 				     u8 *dl_pipe);
232*da8fa4e3SBjoern A. Zeeb void ath10k_pci_hif_send_complete_check(struct ath10k *ar, u8 pipe,
233*da8fa4e3SBjoern A. Zeeb 					int force);
234*da8fa4e3SBjoern A. Zeeb u16 ath10k_pci_hif_get_free_queue_number(struct ath10k *ar, u8 pipe);
235*da8fa4e3SBjoern A. Zeeb void ath10k_pci_hif_power_down(struct ath10k *ar);
236*da8fa4e3SBjoern A. Zeeb int ath10k_pci_alloc_pipes(struct ath10k *ar);
237*da8fa4e3SBjoern A. Zeeb void ath10k_pci_free_pipes(struct ath10k *ar);
238*da8fa4e3SBjoern A. Zeeb void ath10k_pci_rx_replenish_retry(struct timer_list *t);
239*da8fa4e3SBjoern A. Zeeb void ath10k_pci_ce_deinit(struct ath10k *ar);
240*da8fa4e3SBjoern A. Zeeb void ath10k_pci_init_napi(struct ath10k *ar);
241*da8fa4e3SBjoern A. Zeeb int ath10k_pci_init_pipes(struct ath10k *ar);
242*da8fa4e3SBjoern A. Zeeb int ath10k_pci_init_config(struct ath10k *ar);
243*da8fa4e3SBjoern A. Zeeb void ath10k_pci_rx_post(struct ath10k *ar);
244*da8fa4e3SBjoern A. Zeeb void ath10k_pci_flush(struct ath10k *ar);
245*da8fa4e3SBjoern A. Zeeb void ath10k_pci_enable_legacy_irq(struct ath10k *ar);
246*da8fa4e3SBjoern A. Zeeb bool ath10k_pci_irq_pending(struct ath10k *ar);
247*da8fa4e3SBjoern A. Zeeb void ath10k_pci_disable_and_clear_legacy_irq(struct ath10k *ar);
248*da8fa4e3SBjoern A. Zeeb void ath10k_pci_irq_msi_fw_mask(struct ath10k *ar);
249*da8fa4e3SBjoern A. Zeeb int ath10k_pci_wait_for_target_init(struct ath10k *ar);
250*da8fa4e3SBjoern A. Zeeb int ath10k_pci_setup_resource(struct ath10k *ar);
251*da8fa4e3SBjoern A. Zeeb void ath10k_pci_release_resource(struct ath10k *ar);
252*da8fa4e3SBjoern A. Zeeb 
253*da8fa4e3SBjoern A. Zeeb /* QCA6174 is known to have Tx/Rx issues when SOC_WAKE register is poked too
254*da8fa4e3SBjoern A. Zeeb  * frequently. To avoid this put SoC to sleep after a very conservative grace
255*da8fa4e3SBjoern A. Zeeb  * period. Adjust with great care.
256*da8fa4e3SBjoern A. Zeeb  */
257*da8fa4e3SBjoern A. Zeeb #define ATH10K_PCI_SLEEP_GRACE_PERIOD_MSEC 60
258*da8fa4e3SBjoern A. Zeeb 
259*da8fa4e3SBjoern A. Zeeb #endif /* _PCI_H_ */
260