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