xref: /OK3568_Linux_fs/external/rkwifibt/drivers/bcmdhd/include/linux_osl.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Linux OS Independent Layer
3  *
4  * Copyright (C) 2020, Broadcom.
5  *
6  *      Unless you and Broadcom execute a separate written software license
7  * agreement governing use of this software, this software is licensed to you
8  * under the terms of the GNU General Public License version 2 (the "GPL"),
9  * available at http://www.broadcom.com/licenses/GPLv2.php, with the
10  * following added to such license:
11  *
12  *      As a special exception, the copyright holders of this software give you
13  * permission to link this software with independent modules, and to copy and
14  * distribute the resulting executable under terms of your choice, provided that
15  * you also meet, for each linked independent module, the terms and conditions of
16  * the license of that module.  An independent module is a module which is not
17  * derived from this software.  The special exception does not apply to any
18  * modifications of the software.
19  *
20  *
21  * <<Broadcom-WL-IPTag/Dual:>>
22  */
23 
24 #ifndef _linux_osl_h_
25 #define _linux_osl_h_
26 
27 #include <typedefs.h>
28 #define DECLSPEC_ALIGN(x)	__attribute__ ((aligned(x)))
29 
30 /* Linux Kernel: File Operations: start */
osl_os_open_image(char * filename)31 static INLINE void * osl_os_open_image(char * filename)
32 	{ return NULL; }
osl_os_close_image(void * image)33 static INLINE void osl_os_close_image(void * image)
34 	{ return; }
osl_os_get_image_block(char * buf,int len,void * image)35 static INLINE int osl_os_get_image_block(char * buf, int len, void * image)
36 	{ return 0; }
osl_os_image_size(void * image)37 static INLINE int osl_os_image_size(void *image)
38 	{ return 0; }
39 /* Linux Kernel: File Operations: end */
40 
41 #ifdef BCMDRIVER
42 
43 /* OSL initialization */
44 #ifdef SHARED_OSL_CMN
45 extern osl_t *osl_attach(void *pdev, uint bustype, bool pkttag, void **osh_cmn);
46 #else
47 extern osl_t *osl_attach(void *pdev, uint bustype, bool pkttag);
48 #endif /* SHARED_OSL_CMN */
49 
50 extern void osl_detach(osl_t *osh);
51 extern int osl_static_mem_init(osl_t *osh, void *adapter);
52 extern int osl_static_mem_deinit(osl_t *osh, void *adapter);
53 extern void osl_set_bus_handle(osl_t *osh, void *bus_handle);
54 extern void* osl_get_bus_handle(osl_t *osh);
55 #ifdef DHD_MAP_LOGGING
56 extern void osl_dma_map_dump(osl_t *osh);
57 #define OSL_DMA_MAP_DUMP(osh)	osl_dma_map_dump(osh)
58 #else
59 #define OSL_DMA_MAP_DUMP(osh)	do {} while (0)
60 #endif /* DHD_MAP_LOGGING */
61 
62 /* Global ASSERT type */
63 extern uint32 g_assert_type;
64 
65 #ifdef CONFIG_PHYS_ADDR_T_64BIT
66 #define PRI_FMT_x       "llx"
67 #define PRI_FMT_X       "llX"
68 #define PRI_FMT_o       "llo"
69 #define PRI_FMT_d       "lld"
70 #else
71 #define PRI_FMT_x       "x"
72 #define PRI_FMT_X       "X"
73 #define PRI_FMT_o       "o"
74 #define PRI_FMT_d       "d"
75 #endif /* CONFIG_PHYS_ADDR_T_64BIT */
76 /* ASSERT */
77 #ifndef ASSERT
78 #if (defined(BCMDBG_ASSERT) || defined(BCMASSERT_LOG)) && !defined(BINCMP)
79 	#define ASSERT(exp) \
80 		do { if (!(exp)) osl_assert(#exp, __FILE__, __LINE__); } while (0)
81 extern void osl_assert(const char *exp, const char *file, int line);
82 #else
83 #ifdef __GNUC__
84 	#define GCC_VERSION \
85 		(__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
86 #if GCC_VERSION > 30100
87 	#define ASSERT(exp)	do {} while (0)
88 #else
89 	/* ASSERT could cause segmentation fault on GCC3.1, use empty instead */
90 	#define ASSERT(exp)
91 #endif /* GCC_VERSION > 30100 */
92 #endif /* __GNUC__ */
93 #endif /* (BCMDBG_ASSERT || BCMASSERT_LOG) && !BINCMP */
94 #endif /* ASSERT */
95 
96 #define ASSERT_FP(exp) ASSERT(exp)
97 
98 /* microsecond delay */
99 #define	OSL_DELAY(usec)		osl_delay(usec)
100 extern void osl_delay(uint usec);
101 
102 #define OSL_SLEEP(ms)			osl_sleep(ms)
103 extern void osl_sleep(uint ms);
104 
105 /* PCI configuration space access macros */
106 #define	OSL_PCI_READ_CONFIG(osh, offset, size) \
107 	osl_pci_read_config((osh), (offset), (size))
108 #define	OSL_PCI_WRITE_CONFIG(osh, offset, size, val) \
109 	osl_pci_write_config((osh), (offset), (size), (val))
110 extern uint32 osl_pci_read_config(osl_t *osh, uint offset, uint size);
111 extern void osl_pci_write_config(osl_t *osh, uint offset, uint size, uint val);
112 
113 #ifdef BCMPCIE
114 /* PCI device bus # and slot # */
115 #define OSL_PCI_BUS(osh)	osl_pci_bus(osh)
116 #define OSL_PCI_SLOT(osh)	osl_pci_slot(osh)
117 #define OSL_PCIE_DOMAIN(osh)	osl_pcie_domain(osh)
118 #define OSL_PCIE_BUS(osh)	osl_pcie_bus(osh)
119 extern uint osl_pci_bus(osl_t *osh);
120 extern uint osl_pci_slot(osl_t *osh);
121 extern uint osl_pcie_domain(osl_t *osh);
122 extern uint osl_pcie_bus(osl_t *osh);
123 extern struct pci_dev *osl_pci_device(osl_t *osh);
124 #endif
125 
126 /* precommit failed when this is removed */
127 /* BLAZAR_BRANCH_101_10_DHD_003/build/dhd/linux-fc30/brix-brcm */
128 /* TBD: Revisit later */
129 #if defined(BCMINTERNAL)
130 /* Flags that can be used to handle OSL specifcs */
131 #define OSL_PHYS_MEM_LESS_THAN_16MB	(1<<0L)
132 #endif /* BCMINTERNAL */
133 
134 #define OSL_ACP_COHERENCE		(1<<1L)
135 #define OSL_FWDERBUF			(1<<2L)
136 
137 /* Pkttag flag should be part of public information */
138 typedef struct {
139 	bool pkttag;
140 	bool mmbus;		/**< Bus supports memory-mapped register accesses */
141 	pktfree_cb_fn_t tx_fn;  /**< Callback function for PKTFREE */
142 	void *tx_ctx;		/**< Context to the callback function */
143 #ifdef OSLREGOPS
144 	osl_rreg_fn_t rreg_fn;	/**< Read Register function */
145 	osl_wreg_fn_t wreg_fn;	/**< Write Register function */
146 	void *reg_ctx;		/**< Context to the reg callback functions */
147 #else
148 	void	*unused[3];	/**< temp fix for USBAP cftpool handle currption */
149 #endif
150 	void (*rx_fn)(void *rx_ctx, void *p);
151 	void *rx_ctx;
152 } osl_pubinfo_t;
153 
154 extern void osl_flag_set(osl_t *osh, uint32 mask);
155 extern void osl_flag_clr(osl_t *osh, uint32 mask);
156 extern bool osl_is_flag_set(osl_t *osh, uint32 mask);
157 
158 #define PKTFREESETCB(osh, _tx_fn, _tx_ctx)		\
159 	do {						\
160 	   ((osl_pubinfo_t*)osh)->tx_fn = _tx_fn;	\
161 	   ((osl_pubinfo_t*)osh)->tx_ctx = _tx_ctx;	\
162 	} while (0)
163 
164 #define PKTFREESETRXCB(osh, _rx_fn, _rx_ctx)		\
165 	do {						\
166 	   ((osl_pubinfo_t*)osh)->rx_fn = _rx_fn;	\
167 	   ((osl_pubinfo_t*)osh)->rx_ctx = _rx_ctx;	\
168 	} while (0)
169 
170 #ifdef OSLREGOPS
171 #define REGOPSSET(osh, rreg, wreg, ctx)			\
172 	do {						\
173 	   ((osl_pubinfo_t*)osh)->rreg_fn = rreg;	\
174 	   ((osl_pubinfo_t*)osh)->wreg_fn = wreg;	\
175 	   ((osl_pubinfo_t*)osh)->reg_ctx = ctx;	\
176 	} while (0)
177 #endif /* OSLREGOPS */
178 
179 /* host/bus architecture-specific byte swap */
180 #define BUS_SWAP32(v)		(v)
181 
182 #if defined(BCMDBG_MEM) && !defined(BINCMP)
183 	#define MALLOC(osh, size)	osl_debug_malloc((osh), (size), __LINE__, __FILE__)
184 	#define MALLOCZ(osh, size)	osl_debug_mallocz((osh), (size), __LINE__, __FILE__)
185 	#define MFREE(osh, addr, size)	\
186 	({osl_debug_mfree((osh), ((void *)addr), (size), __LINE__, __FILE__);(addr) = NULL;})
187 	#define VMALLOC(osh, size)	osl_debug_vmalloc((osh), (size), __LINE__, __FILE__)
188 	#define VMALLOCZ(osh, size)	osl_debug_vmallocz((osh), (size), __LINE__, __FILE__)
189 	#define VMFREE(osh, addr, size)	osl_debug_vmfree((osh), (addr), (size), __LINE__, __FILE__)
190 	#define MALLOCED(osh)		osl_malloced((osh))
191 	#define MEMORY_LEFTOVER(osh) osl_check_memleak(osh)
192 	#define MALLOC_DUMP(osh, b)	osl_debug_memdump((osh), (b))
193 	extern void *osl_debug_malloc(osl_t *osh, uint size, int line, const char* file);
194 	extern void *osl_debug_mallocz(osl_t *osh, uint size, int line, const char* file);
195 	extern void osl_debug_mfree(osl_t *osh, void *addr, uint size, int line, const char* file);
196 	extern void *osl_debug_vmalloc(osl_t *osh, uint size, int line, const char* file);
197 	extern void *osl_debug_vmallocz(osl_t *osh, uint size, int line, const char* file);
198 	extern void osl_debug_vmfree(osl_t *osh, void *addr, uint size, int line, const char* file);
199 	extern uint osl_malloced(osl_t *osh);
200 	struct bcmstrbuf;
201 	extern int osl_debug_memdump(osl_t *osh, struct bcmstrbuf *b);
202 	extern uint osl_check_memleak(osl_t *osh);
203 #else /* BCMDBG_MEM && !BINCMP */
204 	#define MALLOC(osh, size)	osl_malloc((osh), (size))
205 	#define MALLOCZ(osh, size)	osl_mallocz((osh), (size))
206 	#define MALLOC_RA(osh, size, callsite)	osl_mallocz((osh), (size))
207 	#define MFREE(osh, addr, size) ({osl_mfree((osh), ((void *)addr), (size));(addr) = NULL;})
208 	#define VMALLOC(osh, size)	osl_vmalloc((osh), (size))
209 	#define VMALLOCZ(osh, size)	osl_vmallocz((osh), (size))
210 	#define VMFREE(osh, addr, size)	osl_vmfree((osh), (addr), (size))
211 	#define MALLOCED(osh)		osl_malloced((osh))
212 	#define MEMORY_LEFTOVER(osh) osl_check_memleak(osh)
213 	extern void *osl_malloc(osl_t *osh, uint size);
214 	extern void *osl_mallocz(osl_t *osh, uint size);
215 	extern void osl_mfree(osl_t *osh, void *addr, uint size);
216 	extern void *osl_vmalloc(osl_t *osh, uint size);
217 	extern void *osl_vmallocz(osl_t *osh, uint size);
218 	extern void osl_vmfree(osl_t *osh, void *addr, uint size);
219 	extern uint osl_malloced(osl_t *osh);
220 	extern uint osl_check_memleak(osl_t *osh);
221 #endif /* BCMDBG_MEM && !BINCMP */
222 
223 extern int memcpy_s(void *dest, size_t destsz, const void *src, size_t n);
224 extern int memset_s(void *dest, size_t destsz, int c, size_t n);
225 #define	MALLOC_FAILED(osh)	osl_malloc_failed((osh))
226 extern uint osl_malloc_failed(osl_t *osh);
227 
228 /* allocate/free shared (dma-able) consistent memory */
229 #define	DMA_CONSISTENT_ALIGN	osl_dma_consistent_align()
230 #define	DMA_ALLOC_CONSISTENT(osh, size, align, tot, pap, dmah) \
231 	osl_dma_alloc_consistent((osh), (size), (align), (tot), (pap))
232 #define	DMA_FREE_CONSISTENT(osh, va, size, pa, dmah) \
233 	osl_dma_free_consistent((osh), (void*)(va), (size), (pa))
234 
235 #define	DMA_ALLOC_CONSISTENT_FORCE32(osh, size, align, tot, pap, dmah) \
236 	osl_dma_alloc_consistent((osh), (size), (align), (tot), (pap))
237 #define	DMA_FREE_CONSISTENT_FORCE32(osh, va, size, pa, dmah) \
238 	osl_dma_free_consistent((osh), (void*)(va), (size), (pa))
239 
240 extern uint osl_dma_consistent_align(void);
241 extern void *osl_dma_alloc_consistent(osl_t *osh, uint size, uint16 align,
242 	uint *tot, dmaaddr_t *pap);
243 extern void osl_dma_free_consistent(osl_t *osh, void *va, uint size, dmaaddr_t pa);
244 
245 /* map/unmap direction */
246 #define DMA_NO	0	/* Used to skip cache op */
247 #define	DMA_TX	1	/* TX direction for DMA */
248 #define	DMA_RX	2	/* RX direction for DMA */
249 
250 /* map/unmap shared (dma-able) memory */
251 #define	DMA_UNMAP(osh, pa, size, direction, p, dmah) \
252 	osl_dma_unmap((osh), (pa), (size), (direction))
253 extern void osl_dma_flush(osl_t *osh, void *va, uint size, int direction, void *p,
254 	hnddma_seg_map_t *txp_dmah);
255 extern dmaaddr_t osl_dma_map(osl_t *osh, void *va, uint size, int direction, void *p,
256 	hnddma_seg_map_t *txp_dmah);
257 extern void osl_dma_unmap(osl_t *osh, dmaaddr_t pa, uint size, int direction);
258 
259 #ifndef PHYS_TO_VIRT
260 #define	PHYS_TO_VIRT(pa)	osl_phys_to_virt(pa)
261 #endif
262 #ifndef VIRT_TO_PHYS
263 #define	VIRT_TO_PHYS(va)	osl_virt_to_phys(va)
264 #endif
265 extern void * osl_phys_to_virt(void * pa);
266 extern void * osl_virt_to_phys(void * va);
267 
268 /* API for DMA addressing capability */
269 #define OSL_DMADDRWIDTH(osh, addrwidth) ({BCM_REFERENCE(osh); BCM_REFERENCE(addrwidth);})
270 
271 #define OSL_SMP_WMB()	smp_wmb()
272 
273 /* API for CPU relax */
274 extern void osl_cpu_relax(void);
275 #define OSL_CPU_RELAX() osl_cpu_relax()
276 
277 extern void osl_preempt_disable(osl_t *osh);
278 extern void osl_preempt_enable(osl_t *osh);
279 #define OSL_DISABLE_PREEMPTION(osh)	osl_preempt_disable(osh)
280 #define OSL_ENABLE_PREEMPTION(osh)	osl_preempt_enable(osh)
281 
282 #if (defined(BCMPCIE) && !defined(DHD_USE_COHERENT_MEM_FOR_RING) && defined(__ARM_ARCH_7A__))
283 
284 	extern void osl_cache_flush(void *va, uint size);
285 	extern void osl_cache_inv(void *va, uint size);
286 	extern void osl_prefetch(const void *ptr);
287 	#define OSL_CACHE_FLUSH(va, len)	osl_cache_flush((void *)(va), len)
288 	#define OSL_CACHE_INV(va, len)		osl_cache_inv((void *)(va), len)
289 	#define OSL_PREFETCH(ptr)			osl_prefetch(ptr)
290 #else  /* !__ARM_ARCH_7A__ */
291 	#define OSL_CACHE_FLUSH(va, len)	BCM_REFERENCE(va)
292 	#define OSL_CACHE_INV(va, len)		BCM_REFERENCE(va)
293 	#define OSL_PREFETCH(ptr)		BCM_REFERENCE(ptr)
294 #endif /* !__ARM_ARCH_7A__ */
295 
296 #ifdef AXI_TIMEOUTS_NIC
297 extern void osl_set_bpt_cb(osl_t *osh, void *bpt_cb, void *bpt_ctx);
298 extern void osl_bpt_rreg(osl_t *osh, ulong addr, volatile void *v, uint size);
299 #endif /* AXI_TIMEOUTS_NIC */
300 
301 /* register access macros */
302 #if defined(BCMSDIO)
303 	#include <bcmsdh.h>
304 	#define OSL_WRITE_REG(osh, r, v) (bcmsdh_reg_write(osl_get_bus_handle(osh), \
305 		(uintptr)(r), sizeof(*(r)), (v)))
306 	#define OSL_READ_REG(osh, r) (bcmsdh_reg_read(osl_get_bus_handle(osh), \
307 		(uintptr)(r), sizeof(*(r))))
308 #elif defined(AXI_TIMEOUTS_NIC)
309 #define OSL_READ_REG(osh, r) \
310 	({\
311 		__typeof(*(r)) __osl_v; \
312 		osl_bpt_rreg(osh, (uintptr)(r), &__osl_v, sizeof(*(r))); \
313 		__osl_v; \
314 	})
315 #endif
316 
317 #if defined(AXI_TIMEOUTS_NIC)
318 	#define SELECT_BUS_WRITE(osh, mmap_op, bus_op) ({BCM_REFERENCE(osh); mmap_op;})
319 	#define SELECT_BUS_READ(osh, mmap_op, bus_op) ({BCM_REFERENCE(osh); bus_op;})
320 #else /* !AXI_TIMEOUTS_NIC */
321 #if defined(BCMSDIO)
322 	#define SELECT_BUS_WRITE(osh, mmap_op, bus_op) if (((osl_pubinfo_t*)(osh))->mmbus) \
323 		mmap_op else bus_op
324 	#define SELECT_BUS_READ(osh, mmap_op, bus_op) (((osl_pubinfo_t*)(osh))->mmbus) ? \
325 		mmap_op : bus_op
326 #else
327 	#define SELECT_BUS_WRITE(osh, mmap_op, bus_op) ({BCM_REFERENCE(osh); mmap_op;})
328 	#define SELECT_BUS_READ(osh, mmap_op, bus_op) ({BCM_REFERENCE(osh); mmap_op;})
329 #endif /* defined(BCMSDIO) */
330 #endif /* AXI_TIMEOUTS_NIC */
331 
332 #define OSL_ERROR(bcmerror)	osl_error(bcmerror)
333 extern int osl_error(int bcmerror);
334 
335 /* the largest reasonable packet buffer driver uses for ethernet MTU in bytes */
336 #define	PKTBUFSZ	2048   /* largest reasonable packet buffer, driver uses for ethernet MTU */
337 
338 #define OSH_NULL   NULL
339 
340 /*
341  * BINOSL selects the slightly slower function-call-based binary compatible osl.
342  * Macros expand to calls to functions defined in linux_osl.c .
343  */
344 #ifndef BINOSL
345 #include <linuxver.h>           /* use current 2.4.x calling conventions */
346 #include <linux/kernel.h>       /* for vsn/printf's */
347 #include <linux/string.h>       /* for mem*, str* */
348 extern uint64 osl_sysuptime_us(void);
349 #define OSL_SYSUPTIME()		((uint32)jiffies_to_msecs(jiffies))
350 #define OSL_SYSUPTIME_US()	osl_sysuptime_us()
351 extern uint64 osl_localtime_ns(void);
352 extern void osl_get_localtime(uint64 *sec, uint64 *usec);
353 extern uint64 osl_systztime_us(void);
354 #define OSL_LOCALTIME_NS()	osl_localtime_ns()
355 #define OSL_GET_LOCALTIME(sec, usec)	osl_get_localtime((sec), (usec))
356 #define OSL_SYSTZTIME_US()	osl_systztime_us()
357 #define	printf(fmt, args...)	printk(PERCENT_S DHD_LOG_PREFIXS fmt, PRINTF_SYSTEM_TIME, ## args)
358 #include <linux/kernel.h>	/* for vsn/printf's */
359 #include <linux/string.h>	/* for mem*, str* */
360 /* bcopy's: Linux kernel doesn't provide these (anymore) */
361 #define	bcopy_hw(src, dst, len)		memcpy((dst), (src), (len))
362 #define	bcopy_hw_async(src, dst, len)	memcpy((dst), (src), (len))
363 #define	bcopy_hw_poll_for_completion()
364 #define	bcopy(src, dst, len)	memcpy((dst), (src), (len))
365 #define	bcmp(b1, b2, len)	memcmp((b1), (b2), (len))
366 #define	bzero(b, len)		memset((b), '\0', (len))
367 
368 #if defined(CONFIG_SOC_EXYNOS9810) || defined(CONFIG_SOC_EXYNOS9820) || \
369 	defined(CONFIG_SOC_EXYNOS9830) || defined(CONFIG_SOC_GS101)
370 extern int pcie_ch_num;
371 extern int exynos_pcie_l1_exit(int ch_num);
372 #endif /* CONFIG_SOC_EXYNOS9810 || CONFIG_SOC_EXYNOS9820
373 	* CONFIG_SOC_EXYNOS9830 || CONFIG_SOC_GS101
374 	*/
375 
376 /* register access macros */
377 #if defined(OSLREGOPS)
378 #define R_REG(osh, r) (\
379 	sizeof(*(r)) == sizeof(uint8) ? osl_readb((osh), (volatile uint8*)(r)) : \
380 	sizeof(*(r)) == sizeof(uint16) ? osl_readw((osh), (volatile uint16*)(r)) : \
381 	sizeof(*(r)) == sizeof(uint32) ? osl_readl((osh), (volatile uint32*)(r)) : \
382 	osl_readq((osh), (volatile uint64*)(r)) \
383 )
384 
385 #define W_REG(osh, r, v) do { \
386 	switch (sizeof(*(r))) { \
387 	case sizeof(uint8):	osl_writeb((osh), (volatile uint8*)(r), (uint8)(v)); break; \
388 	case sizeof(uint16):	osl_writew((osh), (volatile uint16*)(r), (uint16)(v)); break; \
389 	case sizeof(uint32):	osl_writel((osh), (volatile uint32*)(r), (uint32)(v)); break; \
390 	case sizeof(uint64):	osl_writeq((osh), (volatile uint64*)(r), (uint64)(v)); break; \
391 	} \
392 } while (0)
393 
394 extern uint8 osl_readb(osl_t *osh, volatile uint8 *r);
395 extern uint16 osl_readw(osl_t *osh, volatile uint16 *r);
396 extern uint32 osl_readl(osl_t *osh, volatile uint32 *r);
397 extern uint32 osl_readq(osl_t *osh, volatile uint64 *r);
398 extern void osl_writeb(osl_t *osh, volatile uint8 *r, uint8 v);
399 extern void osl_writew(osl_t *osh, volatile uint16 *r, uint16 v);
400 extern void osl_writel(osl_t *osh, volatile uint32 *r, uint32 v);
401 extern void osl_writeq(osl_t *osh, volatile uint64 *r, uint64 v);
402 
403 #else /* OSLREGOPS */
404 
405 #ifndef IL_BIGENDIAN
406 #ifdef CONFIG_64BIT
407 /* readq is defined only for 64 bit platform */
408 #if defined(CONFIG_SOC_EXYNOS9810) || defined(CONFIG_SOC_EXYNOS9820) || \
409 	defined(CONFIG_SOC_EXYNOS9830) || defined(CONFIG_SOC_GS101)
410 #define R_REG(osh, r) (\
411 	SELECT_BUS_READ(osh, \
412 		({ \
413 			__typeof(*(r)) __osl_v = 0; \
414 			exynos_pcie_l1_exit(pcie_ch_num); \
415 			BCM_REFERENCE(osh);	\
416 			switch (sizeof(*(r))) { \
417 				case sizeof(uint8):	__osl_v = \
418 					readb((volatile uint8*)(r)); break; \
419 				case sizeof(uint16):	__osl_v = \
420 					readw((volatile uint16*)(r)); break; \
421 				case sizeof(uint32):	__osl_v = \
422 					readl((volatile uint32*)(r)); break; \
423 				case sizeof(uint64):	__osl_v = \
424 					readq((volatile uint64*)(r)); break; \
425 			} \
426 			__osl_v; \
427 		}), \
428 		OSL_READ_REG(osh, r)) \
429 )
430 #else
431 #define R_REG(osh, r) (\
432 	SELECT_BUS_READ(osh, \
433 		({ \
434 			__typeof(*(r)) __osl_v = 0; \
435 			BCM_REFERENCE(osh);	\
436 			switch (sizeof(*(r))) { \
437 				case sizeof(uint8):	__osl_v = \
438 					readb((volatile uint8*)(r)); break; \
439 				case sizeof(uint16):	__osl_v = \
440 					readw((volatile uint16*)(r)); break; \
441 				case sizeof(uint32):	__osl_v = \
442 					readl((volatile uint32*)(r)); break; \
443 				case sizeof(uint64):	__osl_v = \
444 					readq((volatile uint64*)(r)); break; \
445 			} \
446 			__osl_v; \
447 		}), \
448 		OSL_READ_REG(osh, r)) \
449 )
450 #endif /* CONFIG_SOC_EXYNOS9810 || CONFIG_SOC_EXYNOS9820
451 	* CONFIG_SOC_EXYNOS9830 || CONFIG_SOC_GS101
452 	*/
453 #else /* !CONFIG_64BIT */
454 #define R_REG(osh, r) (\
455 	SELECT_BUS_READ(osh, \
456 		({ \
457 			__typeof(*(r)) __osl_v = 0; \
458 			switch (sizeof(*(r))) { \
459 				case sizeof(uint8):	__osl_v = \
460 					readb((volatile uint8*)(r)); break; \
461 				case sizeof(uint16):	__osl_v = \
462 					readw((volatile uint16*)(r)); break; \
463 				case sizeof(uint32):	__osl_v = \
464 					readl((volatile uint32*)(r)); break; \
465 			} \
466 			__osl_v; \
467 		}), \
468 		OSL_READ_REG(osh, r)) \
469 )
470 #endif /* CONFIG_64BIT */
471 
472 #ifdef CONFIG_64BIT
473 /* writeq is defined only for 64 bit platform */
474 #if defined(CONFIG_SOC_EXYNOS9810) || defined(CONFIG_SOC_EXYNOS9820) || \
475 	defined(CONFIG_SOC_EXYNOS9830) || defined(CONFIG_SOC_GS101)
476 #define W_REG(osh, r, v) do { \
477 	SELECT_BUS_WRITE(osh, \
478 		({ \
479 			exynos_pcie_l1_exit(pcie_ch_num); \
480 			switch (sizeof(*(r))) { \
481 				case sizeof(uint8):	writeb((uint8)(v), \
482 						(volatile uint8*)(r)); break; \
483 				case sizeof(uint16):	writew((uint16)(v), \
484 						(volatile uint16*)(r)); break; \
485 				case sizeof(uint32):	writel((uint32)(v), \
486 						(volatile uint32*)(r)); break; \
487 				case sizeof(uint64):	writeq((uint64)(v), \
488 						(volatile uint64*)(r)); break; \
489 			} \
490 		 }), \
491 		(OSL_WRITE_REG(osh, r, v))); \
492 	} while (0)
493 #else
494 #define W_REG(osh, r, v) do { \
495 	SELECT_BUS_WRITE(osh, \
496 		switch (sizeof(*(r))) { \
497 			case sizeof(uint8):	writeb((uint8)(v), (volatile uint8*)(r)); break; \
498 			case sizeof(uint16):	writew((uint16)(v), (volatile uint16*)(r)); break; \
499 			case sizeof(uint32):	writel((uint32)(v), (volatile uint32*)(r)); break; \
500 			case sizeof(uint64):	writeq((uint64)(v), (volatile uint64*)(r)); break; \
501 		}, \
502 		(OSL_WRITE_REG(osh, r, v))); \
503 	} while (0)
504 #endif /* CONFIG_SOC_EXYNOS9810 || CONFIG_SOC_EXYNOS9820
505 	* CONFIG_SOC_EXYNOS9830 || CONFIG_SOC_GS101
506 	*/
507 #else /* !CONFIG_64BIT */
508 #define W_REG(osh, r, v) do { \
509 	SELECT_BUS_WRITE(osh, \
510 		switch (sizeof(*(r))) { \
511 			case sizeof(uint8):	writeb((uint8)(v), (volatile uint8*)(r)); break; \
512 			case sizeof(uint16):	writew((uint16)(v), (volatile uint16*)(r)); break; \
513 			case sizeof(uint32):	writel((uint32)(v), (volatile uint32*)(r)); break; \
514 		}, \
515 		(OSL_WRITE_REG(osh, r, v))); \
516 	} while (0)
517 #endif /* CONFIG_64BIT */
518 
519 #else	/* IL_BIGENDIAN */
520 
521 #ifdef CONFIG_64BIT
522 /* readq and writeq is defined only for 64 bit platform */
523 #define R_REG(osh, r) (\
524 	SELECT_BUS_READ(osh, \
525 		({ \
526 			__typeof(*(r)) __osl_v = 0; \
527 			switch (sizeof(*(r))) { \
528 				case sizeof(uint8):	__osl_v = \
529 					readb((volatile uint8*)((uintptr)(r)^3)); break; \
530 				case sizeof(uint16):	__osl_v = \
531 					readw((volatile uint16*)((uintptr)(r)^2)); break; \
532 				case sizeof(uint32):	__osl_v = \
533 					readl((volatile uint32*)(r)); break; \
534 				case sizeof(uint64):    __osl_v = \
535 					readq((volatile uint64*)(r)); break; \
536 			} \
537 			__osl_v; \
538 		}), \
539 		OSL_READ_REG(osh, r)) \
540 )
541 #define W_REG(osh, r, v) do { \
542 	SELECT_BUS_WRITE(osh, \
543 		switch (sizeof(*(r))) { \
544 			case sizeof(uint8):	writeb((uint8)(v), \
545 					(volatile uint8*)((uintptr)(r)^3)); break; \
546 			case sizeof(uint16):	writew((uint16)(v), \
547 					(volatile uint16*)((uintptr)(r)^2)); break; \
548 			case sizeof(uint32):	writel((uint32)(v), \
549 					(volatile uint32*)(r)); break; \
550 			case sizeof(uint64):	writeq((uint64)(v), \
551 					(volatile uint64*)(r)); break; \
552 		}, \
553 		(OSL_WRITE_REG(osh, r, v))); \
554 	} while (0)
555 
556 #else /* !CONFIG_64BIT */
557 #define R_REG(osh, r) (\
558 	SELECT_BUS_READ(osh, \
559 		({ \
560 			__typeof(*(r)) __osl_v = 0; \
561 			switch (sizeof(*(r))) { \
562 				case sizeof(uint8):	__osl_v = \
563 					readb((volatile uint8*)((uintptr)(r)^3)); break; \
564 				case sizeof(uint16):	__osl_v = \
565 					readw((volatile uint16*)((uintptr)(r)^2)); break; \
566 				case sizeof(uint32):	__osl_v = \
567 					readl((volatile uint32*)(r)); break; \
568 			} \
569 			__osl_v; \
570 		}), \
571 		OSL_READ_REG(osh, r)) \
572 )
573 #define W_REG(osh, r, v) do { \
574 	SELECT_BUS_WRITE(osh, \
575 		switch (sizeof(*(r))) { \
576 			case sizeof(uint8):	writeb((uint8)(v), \
577 					(volatile uint8*)((uintptr)(r)^3)); break; \
578 			case sizeof(uint16):	writew((uint16)(v), \
579 					(volatile uint16*)((uintptr)(r)^2)); break; \
580 			case sizeof(uint32):	writel((uint32)(v), \
581 					(volatile uint32*)(r)); break; \
582 		}, \
583 		(OSL_WRITE_REG(osh, r, v))); \
584 	} while (0)
585 #endif /* CONFIG_64BIT */
586 #endif /* IL_BIGENDIAN */
587 
588 #endif /* OSLREGOPS */
589 
590 #define	AND_REG(osh, r, v)		W_REG(osh, (r), R_REG(osh, r) & (v))
591 #define	OR_REG(osh, r, v)		W_REG(osh, (r), R_REG(osh, r) | (v))
592 
593 /* bcopy, bcmp, and bzero functions */
594 #define	bcopy(src, dst, len)	memcpy((dst), (src), (len))
595 #define	bcmp(b1, b2, len)	memcmp((b1), (b2), (len))
596 #define	bzero(b, len)		memset((b), '\0', (len))
597 
598 /* uncached/cached virtual address */
599 #define OSL_UNCACHED(va)	((void *)va)
600 #define OSL_CACHED(va)		((void *)va)
601 
602 #define OSL_PREF_RANGE_LD(va, sz) BCM_REFERENCE(va)
603 #define OSL_PREF_RANGE_ST(va, sz) BCM_REFERENCE(va)
604 
605 /* get processor cycle count */
606 #if defined(__i386__)
607 #define	OSL_GETCYCLES(x)	rdtscl((x))
608 #else
609 #define OSL_GETCYCLES(x)	((x) = 0)
610 #endif /* __i386__ */
611 
612 /* dereference an address that may cause a bus exception */
613 #define	BUSPROBE(val, addr)	({ (val) = R_REG(NULL, (addr)); 0; })
614 
615 /* map/unmap physical to virtual I/O */
616 #if !defined(CONFIG_MMC_MSM7X00A)
617 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0))
618 #define	REG_MAP(pa, size)	ioremap((unsigned long)(pa), (unsigned long)(size))
619 #else
620 #define	REG_MAP(pa, size)	ioremap_nocache((unsigned long)(pa), (unsigned long)(size))
621 #endif
622 #else
623 #define REG_MAP(pa, size)       (void *)(0)
624 #endif /* !defined(CONFIG_MMC_MSM7X00A */
625 #define	REG_UNMAP(va)		iounmap((va))
626 
627 /* shared (dma-able) memory access macros */
628 #define	R_SM(r)			*(r)
629 #define	W_SM(r, v)		(*(r) = (v))
630 #define	OR_SM(r, v)		(*(r) |= (v))
631 #define	BZERO_SM(r, len)	memset((r), '\0', (len))
632 
633 /* Because the non BINOSL implemenation of the PKT OSL routines are macros (for
634  * performance reasons),  we need the Linux headers.
635  */
636 #include <linuxver.h>		/* use current 2.4.x calling conventions */
637 
638 #else	/* BINOSL */
639 
640 /* Where to get the declarations for mem, str, printf, bcopy's? Two basic approaches.
641  *
642  * First, use the Linux header files and the C standard library replacmenent versions
643  * built-in to the kernel.  Use this approach when compiling non hybrid code or compling
644  * the OS port files.  The second approach is to use our own defines/prototypes and
645  * functions we have provided in the Linux OSL, i.e. linux_osl.c.  Use this approach when
646  * compiling the files that make up the hybrid binary.  We are ensuring we
647  * don't directly link to the kernel replacement routines from the hybrid binary.
648  *
649  * NOTE: The issue we are trying to avoid is any questioning of whether the
650  * hybrid binary is derived from Linux.  The wireless common code (wlc) is designed
651  * to be OS independent through the use of the OSL API and thus the hybrid binary doesn't
652  * derive from the Linux kernel at all.  But since we defined our OSL API to include
653  * a small collection of standard C library routines and these routines are provided in
654  * the kernel we want to avoid even the appearance of deriving at all even though clearly
655  * usage of a C standard library API doesn't represent a derivation from Linux.  Lastly
656  * note at the time of this checkin 4 references to memcpy/memset could not be eliminated
657  * from the binary because they are created internally by GCC as part of things like
658  * structure assignment.  I don't think the compiler should be doing this but there is
659  * no options to disable it on Intel architectures (there is for MIPS so somebody must
660  * agree with me).  I may be able to even remove these references eventually with
661  * a GNU binutil such as objcopy via a symbol rename (i.e. memcpy to osl_memcpy).
662  */
663 	#define	printf(fmt, args...)	printk(fmt , ## args)
664 	#include <linux/kernel.h>	/* for vsn/printf's */
665 	#include <linux/string.h>	/* for mem*, str* */
666 	/* bcopy's: Linux kernel doesn't provide these (anymore) */
667 	#define	bcopy(src, dst, len)	memcpy((dst), (src), (len))
668 	#define	bcmp(b1, b2, len)	memcmp((b1), (b2), (len))
669 	#define	bzero(b, len)		memset((b), '\0', (len))
670 
671 	/* These are provided only because when compiling linux_osl.c there
672 	 * must be an explicit prototype (separate from the definition) because
673 	 * we are compiling with GCC option -Wstrict-prototypes.  Conversely
674 	 * these could be placed directly in linux_osl.c.
675 	 */
676 	extern int osl_printf(const char *format, ...);
677 	extern int osl_sprintf(char *buf, const char *format, ...);
678 	extern int osl_snprintf(char *buf, size_t n, const char *format, ...);
679 	extern int osl_vsprintf(char *buf, const char *format, va_list ap);
680 	extern int osl_vsnprintf(char *buf, size_t n, const char *format, va_list ap);
681 	extern int osl_strcmp(const char *s1, const char *s2);
682 	extern int osl_strncmp(const char *s1, const char *s2, uint n);
683 	extern int osl_strlen(const char *s);
684 	extern char* osl_strcpy(char *d, const char *s);
685 	extern char* osl_strncpy(char *d, const char *s, uint n);
686 	extern char* osl_strchr(const char *s, int c);
687 	extern char* osl_strrchr(const char *s, int c);
688 	extern void *osl_memset(void *d, int c, size_t n);
689 	extern void *osl_memcpy(void *d, const void *s, size_t n);
690 	extern void *osl_memmove(void *d, const void *s, size_t n);
691 	extern int osl_memcmp(const void *s1, const void *s2, size_t n);
692 
693 /* register access macros */
694 #if !defined(BCMSDIO)
695 #define R_REG(osh, r) \
696 	({ \
697 	 BCM_REFERENCE(osh); \
698 	 sizeof(*(r)) == sizeof(uint8) ? osl_readb((volatile uint8*)(r)) : \
699 	 sizeof(*(r)) == sizeof(uint16) ? osl_readw((volatile uint16*)(r)) : \
700 	 sizeof(*(r)) == sizeof(uint32) ? osl_readl((volatile uint32*)(r)) : \
701 	 osl_readq((volatile uint64*)(r)); \
702 	 })
703 #define W_REG(osh, r, v) do { \
704 	BCM_REFERENCE(osh); \
705 	switch (sizeof(*(r))) { \
706 	case sizeof(uint8):	osl_writeb((uint8)(v), (volatile uint8*)(r)); break; \
707 	case sizeof(uint16):	osl_writew((uint16)(v), (volatile uint16*)(r)); break; \
708 	case sizeof(uint32):	osl_writel((uint32)(v), (volatile uint32*)(r)); break; \
709 	case sizeof(uint64):	osl_writeq((uint64)(v), (volatile uint64*)(r)); break; \
710 	} \
711 } while (0)
712 
713 #else
714 #define R_REG(osh, r) OSL_READ_REG(osh, r)
715 #define W_REG(osh, r, v) do { OSL_WRITE_REG(osh, r, v); } while (0)
716 #endif /* !defined(BCMSDIO) */
717 
718 #define	AND_REG(osh, r, v)		W_REG(osh, (r), R_REG(osh, r) & (v))
719 #define	OR_REG(osh, r, v)		W_REG(osh, (r), R_REG(osh, r) | (v))
720 extern uint8 osl_readb(volatile uint8 *r);
721 extern uint16 osl_readw(volatile uint16 *r);
722 extern uint32 osl_readl(volatile uint32 *r);
723 extern uint64 osl_readq(volatile uint64 *r);
724 extern void osl_writeb(uint8 v, volatile uint8 *r);
725 extern void osl_writew(uint16 v, volatile uint16 *r);
726 extern void osl_writel(uint32 v, volatile uint32 *r);
727 extern void osl_writeq(uint64 v, volatile uint64 *r);
728 
729 /* system up time in ms */
730 #define OSL_SYSUPTIME()		osl_sysuptime()
731 extern uint32 osl_sysuptime(void);
732 
733 /* uncached/cached virtual address */
734 #define OSL_UNCACHED(va)	osl_uncached((va))
735 extern void *osl_uncached(void *va);
736 #define OSL_CACHED(va)		osl_cached((va))
737 extern void *osl_cached(void *va);
738 
739 #define OSL_PREF_RANGE_LD(va, sz)
740 #define OSL_PREF_RANGE_ST(va, sz)
741 
742 /* get processor cycle count */
743 #define OSL_GETCYCLES(x)	((x) = osl_getcycles())
744 extern uint osl_getcycles(void);
745 
746 /* dereference an address that may target abort */
747 #define	BUSPROBE(val, addr)	osl_busprobe(&(val), (addr))
748 extern int osl_busprobe(uint32 *val, uint32 addr);
749 
750 /* map/unmap physical to virtual */
751 #define	REG_MAP(pa, size)	osl_reg_map((pa), (size))
752 #define	REG_UNMAP(va)		osl_reg_unmap((va))
753 extern void *osl_reg_map(uint32 pa, uint size);
754 extern void osl_reg_unmap(void *va);
755 
756 /* shared (dma-able) memory access macros */
757 #define	R_SM(r)			*(r)
758 #define	W_SM(r, v)		(*(r) = (v))
759 #define	OR_SM(r, v)		(*(r) |= (v))
760 #define	BZERO_SM(r, len)	bzero((r), (len))
761 
762 #endif	/* BINOSL */
763 
764 #define OSL_RAND()		osl_rand()
765 extern uint32 osl_rand(void);
766 
767 #define	DMA_FLUSH(osh, va, size, direction, p, dmah) \
768 	osl_dma_flush((osh), (va), (size), (direction), (p), (dmah))
769 #define	DMA_MAP(osh, va, size, direction, p, dmah) \
770 	osl_dma_map((osh), (va), (size), (direction), (p), (dmah))
771 
772 #else /* ! BCMDRIVER */
773 
774 /* ASSERT */
775 #ifdef BCMDBG_ASSERT
776 	#include <assert.h>
777 	#define ASSERT assert
778 #else /* BCMDBG_ASSERT */
779 	#define ASSERT(exp)	do {} while (0)
780 #endif /* BCMDBG_ASSERT */
781 
782 #define ASSERT_FP(exp) ASSERT(exp)
783 
784 /* MALLOC and MFREE */
785 #define MALLOC(o, l) malloc(l)
786 #define MFREE(o, p, l) free(p)
787 #include <stdlib.h>
788 
789 /* str* and mem* functions */
790 #include <string.h>
791 
792 /* *printf functions */
793 #include <stdio.h>
794 
795 /* bcopy, bcmp, and bzero */
796 extern void bcopy(const void *src, void *dst, size_t len);
797 extern int bcmp(const void *b1, const void *b2, size_t len);
798 extern void bzero(void *b, size_t len);
799 #endif /* ! BCMDRIVER */
800 
801 typedef struct sk_buff_head PKT_LIST;
802 #define PKTLIST_INIT(x)		skb_queue_head_init((x))
803 #define PKTLIST_ENQ(x, y)	skb_queue_head((struct sk_buff_head *)(x), (struct sk_buff *)(y))
804 #define PKTLIST_DEQ(x)		skb_dequeue((struct sk_buff_head *)(x))
805 #define PKTLIST_UNLINK(x, y)	skb_unlink((struct sk_buff *)(y), (struct sk_buff_head *)(x))
806 #define PKTLIST_FINI(x)		skb_queue_purge((struct sk_buff_head *)(x))
807 
808 #ifndef _linuxver_h_
809 typedef struct timer_list_compat timer_list_compat_t;
810 #endif /* _linuxver_h_ */
811 typedef struct osl_timer {
812 	timer_list_compat_t *timer;
813 	bool   set;
814 #ifdef BCMDBG
815 	char    *name;          /* Desription of the timer */
816 #endif
817 } osl_timer_t;
818 
819 typedef void (*linux_timer_fn)(ulong arg);
820 
821 extern osl_timer_t * osl_timer_init(osl_t *osh, const char *name, void (*fn)(void *arg), void *arg);
822 extern void osl_timer_add(osl_t *osh, osl_timer_t *t, uint32 ms, bool periodic);
823 extern void osl_timer_update(osl_t *osh, osl_timer_t *t, uint32 ms, bool periodic);
824 extern bool osl_timer_del(osl_t *osh, osl_timer_t *t);
825 
826 #ifdef BCMDRIVER
827 typedef atomic_t osl_atomic_t;
828 #define OSL_ATOMIC_SET(osh, v, x)	atomic_set(v, x)
829 #define OSL_ATOMIC_INIT(osh, v)		atomic_set(v, 0)
830 #define OSL_ATOMIC_INC(osh, v)		atomic_inc(v)
831 #define OSL_ATOMIC_INC_RETURN(osh, v)	atomic_inc_return(v)
832 #define OSL_ATOMIC_DEC(osh, v)		atomic_dec(v)
833 #define OSL_ATOMIC_DEC_RETURN(osh, v)	atomic_dec_return(v)
834 #define OSL_ATOMIC_READ(osh, v)		atomic_read(v)
835 #define OSL_ATOMIC_ADD(osh, v, x)	atomic_add(v, x)
836 
837 #ifndef atomic_set_mask
838 #define OSL_ATOMIC_OR(osh, v, x)	atomic_or(x, v)
839 #define OSL_ATOMIC_AND(osh, v, x)	atomic_and(x, v)
840 #else
841 #define OSL_ATOMIC_OR(osh, v, x)	atomic_set_mask(x, v)
842 #define OSL_ATOMIC_AND(osh, v, x)	atomic_clear_mask(~x, v)
843 #endif
844 #endif /* BCMDRIVER */
845 
846 extern void *osl_spin_lock_init(osl_t *osh);
847 extern void osl_spin_lock_deinit(osl_t *osh, void *lock);
848 extern unsigned long osl_spin_lock(void *lock);
849 extern void osl_spin_unlock(void *lock, unsigned long flags);
850 extern unsigned long osl_spin_lock_irq(void *lock);
851 extern void osl_spin_unlock_irq(void *lock, unsigned long flags);
852 extern unsigned long osl_spin_lock_bh(void *lock);
853 extern void osl_spin_unlock_bh(void *lock, unsigned long flags);
854 
855 extern void *osl_mutex_lock_init(osl_t *osh);
856 extern void osl_mutex_lock_deinit(osl_t *osh, void *lock);
857 extern unsigned long osl_mutex_lock(void *lock);
858 void osl_mutex_unlock(void *lock, unsigned long flags);
859 
860 typedef struct osl_timespec {
861 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0))
862 	__kernel_old_time_t	tv_sec;			/* seconds */
863 #else
864 	__kernel_time_t	tv_sec;			/* seconds */
865 #endif
866 	__kernel_suseconds_t	tv_usec;	/* microseconds */
867 	long		tv_nsec;		/* nanoseconds */
868 } osl_timespec_t;
869 extern void osl_do_gettimeofday(struct osl_timespec *ts);
870 extern void osl_get_monotonic_boottime(struct osl_timespec *ts);
871 extern uint32 osl_do_gettimediff(struct osl_timespec *cur_ts, struct osl_timespec *old_ts);
872 #endif	/* _linux_osl_h_ */
873