xref: /wlan-driver/qca-wifi-host-cmn/hif/src/hif_io32.h (revision 5113495b16420b49004c444715d2daae2066e7dc)
1*5113495bSYour Name /*
2*5113495bSYour Name  * Copyright (c) 2015-2021 The Linux Foundation. All rights reserved.
3*5113495bSYour Name  * Copyright (c) 2021-2023 Qualcomm Innovation Center, Inc. All rights reserved.
4*5113495bSYour Name  *
5*5113495bSYour Name  * Permission to use, copy, modify, and/or distribute this software for
6*5113495bSYour Name  * any purpose with or without fee is hereby granted, provided that the
7*5113495bSYour Name  * above copyright notice and this permission notice appear in all
8*5113495bSYour Name  * copies.
9*5113495bSYour Name  *
10*5113495bSYour Name  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11*5113495bSYour Name  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12*5113495bSYour Name  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13*5113495bSYour Name  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14*5113495bSYour Name  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15*5113495bSYour Name  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16*5113495bSYour Name  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17*5113495bSYour Name  * PERFORMANCE OF THIS SOFTWARE.
18*5113495bSYour Name  */
19*5113495bSYour Name 
20*5113495bSYour Name #ifndef __HIF_IO32_H__
21*5113495bSYour Name #define __HIF_IO32_H__
22*5113495bSYour Name 
23*5113495bSYour Name #include <linux/io.h>
24*5113495bSYour Name #include "hif.h"
25*5113495bSYour Name #include "hif_main.h"
26*5113495bSYour Name #include "pld_common.h"
27*5113495bSYour Name /* Device memory is 32MB but bar size is only 1MB.
28*5113495bSYour Name  * Register remapping logic is used to access 32MB device memory.
29*5113495bSYour Name  * 0-512KB : Fixed address, 512KB-1MB : remapped address.
30*5113495bSYour Name  * Use PCIE_REMAP_1M_BAR_CTRL register to set window
31*5113495bSYour Name  * for pcie based wifi chipsets.
32*5113495bSYour Name  */
33*5113495bSYour Name #define MAX_UNWINDOWED_ADDRESS 0x80000
34*5113495bSYour Name #if defined(QCA_WIFI_QCA6390) || defined(QCA_WIFI_QCA6490) || \
35*5113495bSYour Name 	defined(QCA_WIFI_QCN9000) || defined(QCA_WIFI_QCA6750) || \
36*5113495bSYour Name 	defined(QCA_WIFI_QCN6432) || \
37*5113495bSYour Name 	defined(QCA_WIFI_QCN9224) || defined(QCA_WIFI_KIWI)
38*5113495bSYour Name #define WINDOW_ENABLE_BIT 0x40000000
39*5113495bSYour Name #else
40*5113495bSYour Name #define WINDOW_ENABLE_BIT 0x80000000
41*5113495bSYour Name #endif
42*5113495bSYour Name #ifdef QCA_WIFI_PEACH
43*5113495bSYour Name #define WINDOW_REG_ADDRESS 0x3278
44*5113495bSYour Name #else
45*5113495bSYour Name #define WINDOW_REG_ADDRESS 0x310C
46*5113495bSYour Name #endif
47*5113495bSYour Name #define WINDOW_SHIFT 19
48*5113495bSYour Name #define WINDOW_VALUE_MASK 0x3F
49*5113495bSYour Name #define WINDOW_START MAX_UNWINDOWED_ADDRESS
50*5113495bSYour Name #define WINDOW_RANGE_MASK 0x7FFFF
51*5113495bSYour Name 
52*5113495bSYour Name #if defined(HIF_REG_WINDOW_SUPPORT) && defined(HIF_PCI)
53*5113495bSYour Name 
54*5113495bSYour Name static inline
55*5113495bSYour Name void hif_write32_mb_reg_window(void *sc,
56*5113495bSYour Name 			       void __iomem *addr, uint32_t value);
57*5113495bSYour Name static inline
58*5113495bSYour Name uint32_t hif_read32_mb_reg_window(void *sc,
59*5113495bSYour Name 				  void __iomem *addr);
60*5113495bSYour Name #define hif_read32_mb(scn, addr) \
61*5113495bSYour Name 	hif_read32_mb_reg_window((void *)scn, \
62*5113495bSYour Name 				 (void __iomem *)addr)
63*5113495bSYour Name #define hif_write32_mb(scn, addr, value) \
64*5113495bSYour Name 	hif_write32_mb_reg_window((void *)scn, \
65*5113495bSYour Name 				  (void __iomem *)addr, value)
66*5113495bSYour Name 
67*5113495bSYour Name #else
68*5113495bSYour Name #define hif_read32_mb(scn, addr)         ioread32((void __iomem *)addr)
69*5113495bSYour Name #define hif_write32_mb(scn, addr, value) \
70*5113495bSYour Name 	iowrite32((u32)(value), (void __iomem *)(addr))
71*5113495bSYour Name #endif
72*5113495bSYour Name 
73*5113495bSYour Name #define Q_TARGET_ACCESS_BEGIN(scn) \
74*5113495bSYour Name 	hif_target_sleep_state_adjust(scn, false, true)
75*5113495bSYour Name #define Q_TARGET_ACCESS_END(scn) \
76*5113495bSYour Name 	hif_target_sleep_state_adjust(scn, true, false)
77*5113495bSYour Name #define TARGET_REGISTER_ACCESS_ALLOWED(scn)\
78*5113495bSYour Name 		hif_is_target_register_access_allowed(scn)
79*5113495bSYour Name 
80*5113495bSYour Name /*
81*5113495bSYour Name  * A_TARGET_ACCESS_LIKELY will not wait for the target to wake up before
82*5113495bSYour Name  * continuing execution.  Because A_TARGET_ACCESS_LIKELY does not guarantee
83*5113495bSYour Name  * that the target is awake before continuing, Q_TARGET_ACCESS macros must
84*5113495bSYour Name  * protect the actual target access.  Since Q_TARGET_ACCESS protect the actual
85*5113495bSYour Name  * target access, A_TARGET_ACCESS_LIKELY hints are optional.
86*5113495bSYour Name  *
87*5113495bSYour Name  * To ignore "LIKELY" hints, set CONFIG_TARGET_ACCESS_LIKELY to 0
88*5113495bSYour Name  * (slightly worse performance, less power)
89*5113495bSYour Name  *
90*5113495bSYour Name  * To use "LIKELY" hints, set CONFIG_TARGET_ACCESS_LIKELY to 1
91*5113495bSYour Name  * (slightly better performance, more power)
92*5113495bSYour Name  *
93*5113495bSYour Name  * note: if a bus doesn't use hif_target_sleep_state_adjust, this will have
94*5113495bSYour Name  * no impact.
95*5113495bSYour Name  */
96*5113495bSYour Name #define CONFIG_TARGET_ACCESS_LIKELY 0
97*5113495bSYour Name #if CONFIG_TARGET_ACCESS_LIKELY
98*5113495bSYour Name #define A_TARGET_ACCESS_LIKELY(scn) \
99*5113495bSYour Name 	hif_target_sleep_state_adjust(scn, false, false)
100*5113495bSYour Name #define A_TARGET_ACCESS_UNLIKELY(scn) \
101*5113495bSYour Name 	hif_target_sleep_state_adjust(scn, true, false)
102*5113495bSYour Name #else                           /* CONFIG_ATH_PCIE_ACCESS_LIKELY */
103*5113495bSYour Name #define A_TARGET_ACCESS_LIKELY(scn) \
104*5113495bSYour Name 	do { \
105*5113495bSYour Name 		unsigned long unused = (unsigned long)(scn); \
106*5113495bSYour Name 		unused = unused; \
107*5113495bSYour Name 	} while (0)
108*5113495bSYour Name 
109*5113495bSYour Name #define A_TARGET_ACCESS_UNLIKELY(scn) \
110*5113495bSYour Name 	do { \
111*5113495bSYour Name 		unsigned long unused = (unsigned long)(scn); \
112*5113495bSYour Name 		unused = unused; \
113*5113495bSYour Name 	} while (0)
114*5113495bSYour Name #endif /* CONFIG_ATH_PCIE_ACCESS_LIKELY */
115*5113495bSYour Name 
116*5113495bSYour Name 
117*5113495bSYour Name #ifdef HIF_PCI
118*5113495bSYour Name #include "hif_io32_pci.h"
119*5113495bSYour Name #endif
120*5113495bSYour Name #ifdef HIF_SNOC
121*5113495bSYour Name #include "hif_io32_snoc.h"
122*5113495bSYour Name #endif
123*5113495bSYour Name #ifdef HIF_IPCI
124*5113495bSYour Name #include "hif_io32_ipci.h"
125*5113495bSYour Name #endif
126*5113495bSYour Name 
127*5113495bSYour Name #ifdef HIF_IPCI
128*5113495bSYour Name /**
129*5113495bSYour Name  * hif_target_access_allowed(): Check if target access is allowed
130*5113495bSYour Name  *
131*5113495bSYour Name  * @scn: HIF handler
132*5113495bSYour Name  *
133*5113495bSYour Name  * Return: True if access is allowed else False
134*5113495bSYour Name  */
135*5113495bSYour Name static inline
hif_target_access_allowed(struct hif_softc * scn)136*5113495bSYour Name bool hif_target_access_allowed(struct hif_softc *scn)
137*5113495bSYour Name {
138*5113495bSYour Name 	return !(scn->recovery);
139*5113495bSYour Name }
140*5113495bSYour Name 
141*5113495bSYour Name #define TARGET_ACCESS_ALLOWED(scn) \
142*5113495bSYour Name 	hif_target_access_allowed(scn)
143*5113495bSYour Name #else
144*5113495bSYour Name #define TARGET_ACCESS_ALLOWED(scn) (1)
145*5113495bSYour Name #endif
146*5113495bSYour Name 
147*5113495bSYour Name #if defined(HIF_REG_WINDOW_SUPPORT) && defined(HIF_PCI)
148*5113495bSYour Name 
149*5113495bSYour Name #include "qdf_lock.h"
150*5113495bSYour Name #include "qdf_util.h"
151*5113495bSYour Name 
152*5113495bSYour Name /**
153*5113495bSYour Name  * hif_reg_write_result_check() - check register writing result
154*5113495bSYour Name  * @sc: hif pcie context
155*5113495bSYour Name  * @offset: register offset to read
156*5113495bSYour Name  * @exp_val: the expected value of register
157*5113495bSYour Name  *
158*5113495bSYour Name  * Return: none
159*5113495bSYour Name  */
hif_reg_write_result_check(struct hif_pci_softc * sc,uint32_t offset,uint32_t exp_val)160*5113495bSYour Name static inline void hif_reg_write_result_check(struct hif_pci_softc *sc,
161*5113495bSYour Name 					      uint32_t offset,
162*5113495bSYour Name 					      uint32_t exp_val)
163*5113495bSYour Name {
164*5113495bSYour Name 	uint32_t value;
165*5113495bSYour Name 
166*5113495bSYour Name 	value = qdf_ioread32(sc->mem + offset);
167*5113495bSYour Name 	if (exp_val != value) {
168*5113495bSYour Name 		hif_err("Reg write failed. write val 0x%x read val 0x%x offset 0x%x",
169*5113495bSYour Name 			exp_val,
170*5113495bSYour Name 			value,
171*5113495bSYour Name 			offset);
172*5113495bSYour Name 	}
173*5113495bSYour Name }
174*5113495bSYour Name 
175*5113495bSYour Name #ifdef PCIE_REG_WINDOW_LOCAL_NO_CACHE
176*5113495bSYour Name /**
177*5113495bSYour Name  * hif_select_window_confirm(): Update the register window
178*5113495bSYour Name  * @sc: HIF pci handle
179*5113495bSYour Name  * @offset: reg offset to read from or write to
180*5113495bSYour Name  *
181*5113495bSYour Name  * Calculate the window using the offset provided and update
182*5113495bSYour Name  * the window reg value accordingly for windowed read/write reg
183*5113495bSYour Name  * access.
184*5113495bSYour Name  * Read back to make sure the window is written to the register.
185*5113495bSYour Name  * Return: None
186*5113495bSYour Name  */
187*5113495bSYour Name static inline
hif_select_window_confirm(struct hif_pci_softc * sc,uint32_t offset)188*5113495bSYour Name void hif_select_window_confirm(struct hif_pci_softc *sc, uint32_t offset)
189*5113495bSYour Name {
190*5113495bSYour Name 	uint32_t window = (offset >> WINDOW_SHIFT) & WINDOW_VALUE_MASK;
191*5113495bSYour Name 
192*5113495bSYour Name 	qdf_iowrite32(sc->mem + WINDOW_REG_ADDRESS,
193*5113495bSYour Name 		      WINDOW_ENABLE_BIT | window);
194*5113495bSYour Name 	sc->register_window = window;
195*5113495bSYour Name 	hif_reg_write_result_check(sc, WINDOW_REG_ADDRESS,
196*5113495bSYour Name 				   WINDOW_ENABLE_BIT | window);
197*5113495bSYour Name }
198*5113495bSYour Name #else /* PCIE_REG_WINDOW_LOCAL_NO_CACHE */
199*5113495bSYour Name 
200*5113495bSYour Name static inline
hif_select_window_confirm(struct hif_pci_softc * sc,uint32_t offset)201*5113495bSYour Name void hif_select_window_confirm(struct hif_pci_softc *sc, uint32_t offset)
202*5113495bSYour Name {
203*5113495bSYour Name 	uint32_t window = (offset >> WINDOW_SHIFT) & WINDOW_VALUE_MASK;
204*5113495bSYour Name 
205*5113495bSYour Name 	if (window != sc->register_window) {
206*5113495bSYour Name 		qdf_iowrite32(sc->mem + WINDOW_REG_ADDRESS,
207*5113495bSYour Name 			      WINDOW_ENABLE_BIT | window);
208*5113495bSYour Name 		sc->register_window = window;
209*5113495bSYour Name 		hif_reg_write_result_check(sc, WINDOW_REG_ADDRESS,
210*5113495bSYour Name 					   WINDOW_ENABLE_BIT | window);
211*5113495bSYour Name 	}
212*5113495bSYour Name }
213*5113495bSYour Name #endif /* PCIE_REG_WINDOW_LOCAL_NO_CACHE */
214*5113495bSYour Name 
215*5113495bSYour Name #ifdef WINDOW_REG_PLD_LOCK_ENABLE
216*5113495bSYour Name /**
217*5113495bSYour Name  * hif_lock_reg_access() - Lock window register access spinlock
218*5113495bSYour Name  * @sc: HIF handle
219*5113495bSYour Name  * @flags: variable pointer to save CPU states
220*5113495bSYour Name  *
221*5113495bSYour Name  * Lock register window spinlock
222*5113495bSYour Name  *
223*5113495bSYour Name  * Return: void
224*5113495bSYour Name  */
hif_lock_reg_access(struct hif_pci_softc * sc,unsigned long * flags)225*5113495bSYour Name static inline void hif_lock_reg_access(struct hif_pci_softc *sc,
226*5113495bSYour Name 				       unsigned long *flags)
227*5113495bSYour Name {
228*5113495bSYour Name 	pld_lock_reg_window(sc->dev, flags);
229*5113495bSYour Name }
230*5113495bSYour Name 
231*5113495bSYour Name /**
232*5113495bSYour Name  * hif_unlock_reg_access() - Unlock window register access spinlock
233*5113495bSYour Name  * @sc: HIF handle
234*5113495bSYour Name  * @flags: variable pointer to save CPU states
235*5113495bSYour Name  *
236*5113495bSYour Name  * Unlock register window spinlock
237*5113495bSYour Name  *
238*5113495bSYour Name  * Return: void
239*5113495bSYour Name  */
hif_unlock_reg_access(struct hif_pci_softc * sc,unsigned long * flags)240*5113495bSYour Name static inline void hif_unlock_reg_access(struct hif_pci_softc *sc,
241*5113495bSYour Name 					 unsigned long *flags)
242*5113495bSYour Name {
243*5113495bSYour Name 	pld_unlock_reg_window(sc->dev, flags);
244*5113495bSYour Name }
245*5113495bSYour Name #else
hif_lock_reg_access(struct hif_pci_softc * sc,unsigned long * flags)246*5113495bSYour Name static inline void hif_lock_reg_access(struct hif_pci_softc *sc,
247*5113495bSYour Name 				       unsigned long *flags)
248*5113495bSYour Name {
249*5113495bSYour Name 	qdf_spin_lock_irqsave(&sc->register_access_lock);
250*5113495bSYour Name }
251*5113495bSYour Name 
hif_unlock_reg_access(struct hif_pci_softc * sc,unsigned long * flags)252*5113495bSYour Name static inline void hif_unlock_reg_access(struct hif_pci_softc *sc,
253*5113495bSYour Name 					 unsigned long *flags)
254*5113495bSYour Name {
255*5113495bSYour Name 	qdf_spin_unlock_irqrestore(&sc->register_access_lock);
256*5113495bSYour Name }
257*5113495bSYour Name #endif
258*5113495bSYour Name 
259*5113495bSYour Name /*
260*5113495bSYour Name  * note1: WINDOW_RANGE_MASK = (1 << WINDOW_SHIFT) -1
261*5113495bSYour Name  * note2: 1 << WINDOW_SHIFT = MAX_UNWINDOWED_ADDRESS
262*5113495bSYour Name  * note3: WINDOW_VALUE_MASK = big enough that trying to write past that window
263*5113495bSYour Name  *				would be a bug
264*5113495bSYour Name  */
hif_write32_mb_reg_window(void * scn,void __iomem * addr,uint32_t value)265*5113495bSYour Name static inline void hif_write32_mb_reg_window(void *scn,
266*5113495bSYour Name 					     void __iomem *addr, uint32_t value)
267*5113495bSYour Name {
268*5113495bSYour Name 	struct hif_pci_softc *sc = HIF_GET_PCI_SOFTC(scn);
269*5113495bSYour Name 	uint32_t offset = addr - sc->mem;
270*5113495bSYour Name 	unsigned long flags;
271*5113495bSYour Name 
272*5113495bSYour Name 	if (!sc->use_register_windowing ||
273*5113495bSYour Name 	    offset < MAX_UNWINDOWED_ADDRESS) {
274*5113495bSYour Name 		qdf_iowrite32(addr, value);
275*5113495bSYour Name 	} else {
276*5113495bSYour Name 		hif_lock_reg_access(sc, &flags);
277*5113495bSYour Name 		hif_select_window_confirm(sc, offset);
278*5113495bSYour Name 		qdf_iowrite32(sc->mem + WINDOW_START +
279*5113495bSYour Name 			  (offset & WINDOW_RANGE_MASK), value);
280*5113495bSYour Name 		hif_unlock_reg_access(sc, &flags);
281*5113495bSYour Name 	}
282*5113495bSYour Name }
283*5113495bSYour Name 
hif_read32_mb_reg_window(void * scn,void __iomem * addr)284*5113495bSYour Name static inline uint32_t hif_read32_mb_reg_window(void *scn, void __iomem *addr)
285*5113495bSYour Name {
286*5113495bSYour Name 	struct hif_pci_softc *sc = HIF_GET_PCI_SOFTC(scn);
287*5113495bSYour Name 	uint32_t ret;
288*5113495bSYour Name 	uint32_t offset = addr - sc->mem;
289*5113495bSYour Name 	unsigned long flags;
290*5113495bSYour Name 
291*5113495bSYour Name 	if (!sc->use_register_windowing ||
292*5113495bSYour Name 	    offset < MAX_UNWINDOWED_ADDRESS) {
293*5113495bSYour Name 		return qdf_ioread32(addr);
294*5113495bSYour Name 	}
295*5113495bSYour Name 	hif_lock_reg_access(sc, &flags);
296*5113495bSYour Name 	hif_select_window_confirm(sc, offset);
297*5113495bSYour Name 	ret = qdf_ioread32(sc->mem + WINDOW_START +
298*5113495bSYour Name 		       (offset & WINDOW_RANGE_MASK));
299*5113495bSYour Name 	hif_unlock_reg_access(sc, &flags);
300*5113495bSYour Name 
301*5113495bSYour Name 	return ret;
302*5113495bSYour Name }
303*5113495bSYour Name #endif
304*5113495bSYour Name 
305*5113495bSYour Name #if defined(HIF_HAL_REG_ACCESS_SUPPORT)
306*5113495bSYour Name #define A_TARGET_READ(scn, offset) \
307*5113495bSYour Name 	hif_reg_window_read(scn, offset)
308*5113495bSYour Name #define A_TARGET_WRITE(scn, offset, value) \
309*5113495bSYour Name 	hif_reg_window_write(scn, offset, value)
310*5113495bSYour Name #elif defined(CONFIG_IO_MEM_ACCESS_DEBUG)
311*5113495bSYour Name uint32_t hif_target_read_checked(struct hif_softc *scn,
312*5113495bSYour Name 					uint32_t offset);
313*5113495bSYour Name void hif_target_write_checked(struct hif_softc *scn, uint32_t offset,
314*5113495bSYour Name 				     uint32_t value);
315*5113495bSYour Name 
316*5113495bSYour Name #define A_TARGET_READ(scn, offset) \
317*5113495bSYour Name 	hif_target_read_checked(scn, (offset))
318*5113495bSYour Name #define A_TARGET_WRITE(scn, offset, value) \
319*5113495bSYour Name 	hif_target_write_checked(scn, (offset), (value))
320*5113495bSYour Name #else                           /* CONFIG_ATH_PCIE_ACCESS_DEBUG */
321*5113495bSYour Name #define A_TARGET_READ(scn, offset) \
322*5113495bSYour Name 	hif_read32_mb(scn, scn->mem + (offset))
323*5113495bSYour Name #define A_TARGET_WRITE(scn, offset, value) \
324*5113495bSYour Name 	hif_write32_mb(scn, (scn->mem) + (offset), value)
325*5113495bSYour Name #endif
326*5113495bSYour Name 
327*5113495bSYour Name #ifdef FEATURE_HIF_DELAYED_REG_WRITE
328*5113495bSYour Name #define A_TARGET_DELAYED_REG_WRITE(scn, ctrl_addr, val) \
329*5113495bSYour Name 	hif_delayed_reg_write(scn, ctrl_addr, val)
330*5113495bSYour Name #endif
331*5113495bSYour Name 
332*5113495bSYour Name void hif_irq_enable(struct hif_softc *scn, int irq_id);
333*5113495bSYour Name void hif_irq_disable(struct hif_softc *scn, int irq_id);
334*5113495bSYour Name 
335*5113495bSYour Name #endif /* __HIF_IO32_H__ */
336