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