2 * Copyright (c) 2010 Broadcom Corporation
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 /* Linux Kernel: File Operations: start */
23 extern void *osl_os_open_image(char *filename);
24 extern int osl_os_get_image_block(char *buf, int len, void *image);
25 extern void osl_os_close_image(void *image);
26 /* Linux Kernel: File Operations: end */
28 extern osl_t *osl_attach(void *pdev, uint bustype, bool pkttag);
29 extern void osl_detach(osl_t * osh);
31 extern uint32 g_assert_type;
33 #if defined(BCMDBG_ASSERT)
35 do { if (!(exp)) osl_assert(#exp, __FILE__, __LINE__); } while (0)
36 extern void osl_assert(char *exp, char *file, int line);
40 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
41 #if GCC_VERSION > 30100
42 #define ASSERT(exp) do {} while (0)
44 /* ASSERT could cause segmentation fault on GCC3.1, use empty instead */
46 #endif /* GCC_VERSION > 30100 */
48 #endif /* defined(BCMDBG_ASSERT) */
50 #define OSL_DELAY(usec) osl_delay(usec)
51 extern void osl_delay(uint usec);
53 #define OSL_PCMCIA_READ_ATTR(osh, offset, buf, size) \
54 osl_pcmcia_read_attr((osh), (offset), (buf), (size))
55 #define OSL_PCMCIA_WRITE_ATTR(osh, offset, buf, size) \
56 osl_pcmcia_write_attr((osh), (offset), (buf), (size))
57 extern void osl_pcmcia_read_attr(osl_t * osh, uint offset, void *buf, int size);
58 extern void osl_pcmcia_write_attr(osl_t * osh, uint offset, void *buf,
61 /* PCI configuration space access macros */
62 #define OSL_PCI_READ_CONFIG(osh, offset, size) \
63 osl_pci_read_config((osh), (offset), (size))
64 #define OSL_PCI_WRITE_CONFIG(osh, offset, size, val) \
65 osl_pci_write_config((osh), (offset), (size), (val))
66 extern uint32 osl_pci_read_config(osl_t * osh, uint offset, uint size);
67 extern void osl_pci_write_config(osl_t * osh, uint offset, uint size, uint val);
69 /* PCI device bus # and slot # */
70 #define OSL_PCI_BUS(osh) osl_pci_bus(osh)
71 #define OSL_PCI_SLOT(osh) osl_pci_slot(osh)
72 extern uint osl_pci_bus(osl_t * osh);
73 extern uint osl_pci_slot(osl_t * osh);
75 /* Pkttag flag should be part of public information */
78 uint pktalloced; /* Number of allocated packet buffers */
79 bool mmbus; /* Bus supports memory-mapped register accesses */
80 pktfree_cb_fn_t tx_fn; /* Callback function for PKTFREE */
81 void *tx_ctx; /* Context to the callback function */
83 osl_rreg_fn_t rreg_fn; /* Read Register function */
84 osl_wreg_fn_t wreg_fn; /* Write Register function */
85 void *reg_ctx; /* Context to the reg callback functions */
89 #define PKTFREESETCB(osh, _tx_fn, _tx_ctx) \
91 ((osl_pubinfo_t*)osh)->tx_fn = _tx_fn; \
92 ((osl_pubinfo_t*)osh)->tx_ctx = _tx_ctx; \
96 #define REGOPSSET(osh, rreg, wreg, ctx) \
98 ((osl_pubinfo_t*)osh)->rreg_fn = rreg; \
99 ((osl_pubinfo_t*)osh)->wreg_fn = wreg; \
100 ((osl_pubinfo_t*)osh)->reg_ctx = ctx; \
104 #define BUS_SWAP32(v) (v)
106 #define MALLOC(osh, size) osl_malloc((osh), (size))
107 #define MFREE(osh, addr, size) osl_mfree((osh), (addr), (size))
108 #define MALLOCED(osh) osl_malloced((osh))
109 extern void *osl_malloc(osl_t * osh, uint size);
110 extern void osl_mfree(osl_t * osh, void *addr, uint size);
111 extern uint osl_malloced(osl_t * osh);
113 #define NATIVE_MALLOC(osh, size) kmalloc(size, GFP_ATOMIC)
114 #define NATIVE_MFREE(osh, addr, size) kfree(addr)
115 #define MALLOC_FAILED(osh) osl_malloc_failed((osh))
116 extern uint osl_malloc_failed(osl_t * osh);
118 /* allocate/free shared (dma-able) consistent memory */
119 #define DMA_CONSISTENT_ALIGN osl_dma_consistent_align()
120 #define DMA_ALLOC_CONSISTENT(osh, size, align, tot, pap, dmah) \
121 osl_dma_alloc_consistent((osh), (size), (align), (tot), (pap))
122 #define DMA_FREE_CONSISTENT(osh, va, size, pa, dmah) \
123 osl_dma_free_consistent((osh), (void*)(va), (size), (pa))
124 extern uint osl_dma_consistent_align(void);
125 extern void *osl_dma_alloc_consistent(osl_t * osh, uint size, uint16 align,
126 uint * tot, ulong * pap);
127 extern void osl_dma_free_consistent(osl_t * osh, void *va, uint size, ulong pa);
129 /* map/unmap direction */
130 #define DMA_TX 1 /* TX direction for DMA */
131 #define DMA_RX 2 /* RX direction for DMA */
133 /* map/unmap shared (dma-able) memory */
134 #define DMA_MAP(osh, va, size, direction, p, dmah) \
135 osl_dma_map((osh), (va), (size), (direction))
136 #define DMA_UNMAP(osh, pa, size, direction, p, dmah) \
137 osl_dma_unmap((osh), (pa), (size), (direction))
138 extern uint osl_dma_map(osl_t * osh, void *va, uint size, int direction);
139 extern void osl_dma_unmap(osl_t * osh, uint pa, uint size, int direction);
141 /* API for DMA addressing capability */
142 #define OSL_DMADDRWIDTH(osh, addrwidth) do {} while (0)
144 /* register access macros */
147 #define OSL_WRITE_REG(osh, r, v) (bcmsdh_reg_write(NULL, (uintptr)(r), sizeof(*(r)), (v)))
148 #define OSL_READ_REG(osh, r) (bcmsdh_reg_read(NULL, (uintptr)(r), sizeof(*(r))))
152 #define SELECT_BUS_WRITE(osh, mmap_op, bus_op) if (((osl_pubinfo_t*)(osh))->mmbus) \
154 #define SELECT_BUS_READ(osh, mmap_op, bus_op) (((osl_pubinfo_t*)(osh))->mmbus) ? \
157 #define SELECT_BUS_WRITE(osh, mmap_op, bus_op) mmap_op
158 #define SELECT_BUS_READ(osh, mmap_op, bus_op) mmap_op
161 #define OSL_ERROR(bcmerror) osl_error(bcmerror)
162 extern int osl_error(int bcmerror);
164 /* the largest reasonable packet buffer driver uses for ethernet MTU in bytes */
165 #define PKTBUFSZ 2048 /* largest reasonable packet buffer, driver uses for ethernet MTU */
167 #define OSL_SYSUPTIME() ((uint32)jiffies * (1000 / HZ))
168 #define printf(fmt, args...) printk(fmt , ## args)
169 #include <linux/kernel.h> /* for vsn/printf's */
170 #include <linux/string.h> /* for mem*, str* */
171 /* bcopy's: Linux kernel doesn't provide these (anymore) */
172 #define bcopy(src, dst, len) memcpy((dst), (src), (len))
173 #define bcmp(b1, b2, len) memcmp((b1), (b2), (len))
174 #define bzero(b, len) memset((b), '\0', (len))
176 /* register access macros */
177 #if defined(OSLREGOPS)
181 #define R_REG(osh, r) (\
182 SELECT_BUS_READ(osh, sizeof(*(r)) == sizeof(uint8) ? readb((volatile uint8*)(r)) : \
183 sizeof(*(r)) == sizeof(uint16) ? readw((volatile uint16*)(r)) : \
184 readl((volatile uint32*)(r)), OSL_READ_REG(osh, r)) \
187 #define R_REG(osh, r) (\
188 SELECT_BUS_READ(osh, \
190 __typeof(*(r)) __osl_v; \
191 __asm__ __volatile__("sync"); \
192 switch (sizeof(*(r))) { \
193 case sizeof(uint8): __osl_v = \
194 readb((volatile uint8*)(r)); break; \
195 case sizeof(uint16): __osl_v = \
196 readw((volatile uint16*)(r)); break; \
197 case sizeof(uint32): __osl_v = \
198 readl((volatile uint32*)(r)); break; \
200 __asm__ __volatile__("sync"); \
204 __typeof(*(r)) __osl_v; \
205 __asm__ __volatile__("sync"); \
206 __osl_v = OSL_READ_REG(osh, r); \
207 __asm__ __volatile__("sync"); \
211 #endif /* __mips__ */
213 #define W_REG(osh, r, v) do { \
214 SELECT_BUS_WRITE(osh, \
215 switch (sizeof(*(r))) { \
216 case sizeof(uint8): \
217 writeb((uint8)(v), (volatile uint8*)(r)); break; \
218 case sizeof(uint16): \
219 writew((uint16)(v), (volatile uint16*)(r)); break; \
220 case sizeof(uint32): \
221 writel((uint32)(v), (volatile uint32*)(r)); break; \
223 (OSL_WRITE_REG(osh, r, v))); \
225 #else /* IL_BIGENDIAN */
226 #define R_REG(osh, r) (\
227 SELECT_BUS_READ(osh, \
229 __typeof(*(r)) __osl_v; \
230 switch (sizeof(*(r))) { \
231 case sizeof(uint8): __osl_v = \
232 readb((volatile uint8*)((uintptr)(r)^3)); break; \
233 case sizeof(uint16): __osl_v = \
234 readw((volatile uint16*)((uintptr)(r)^2)); break; \
235 case sizeof(uint32): __osl_v = \
236 readl((volatile uint32*)(r)); break; \
240 OSL_READ_REG(osh, r)) \
242 #define W_REG(osh, r, v) do { \
243 SELECT_BUS_WRITE(osh, \
244 switch (sizeof(*(r))) { \
245 case sizeof(uint8): \
247 (volatile uint8*)((uintptr)(r)^3)); break; \
248 case sizeof(uint16): \
249 writew((uint16)(v), \
250 (volatile uint16*)((uintptr)(r)^2)); break; \
251 case sizeof(uint32): \
252 writel((uint32)(v), \
253 (volatile uint32*)(r)); break; \
255 (OSL_WRITE_REG(osh, r, v))); \
257 #endif /* IL_BIGENDIAN */
259 #endif /* OSLREGOPS */
261 #define AND_REG(osh, r, v) W_REG(osh, (r), R_REG(osh, r) & (v))
262 #define OR_REG(osh, r, v) W_REG(osh, (r), R_REG(osh, r) | (v))
264 /* bcopy, bcmp, and bzero functions */
265 #define bcopy(src, dst, len) memcpy((dst), (src), (len))
266 #define bcmp(b1, b2, len) memcmp((b1), (b2), (len))
267 #define bzero(b, len) memset((b), '\0', (len))
269 /* uncached/cached virtual address */
271 #include <asm/addrspace.h>
272 #define OSL_UNCACHED(va) ((void *)KSEG1ADDR((va)))
273 #define OSL_CACHED(va) ((void *)KSEG0ADDR((va)))
275 #define OSL_UNCACHED(va) ((void *)va)
276 #define OSL_CACHED(va) ((void *)va)
280 #define OSL_GETCYCLES(x) ((x) = read_c0_count() * 2)
281 #elif defined(__i386__)
282 #define OSL_GETCYCLES(x) rdtscl((x))
284 #define OSL_GETCYCLES(x) ((x) = 0)
285 #endif /* defined(mips) */
287 /* dereference an address that may cause a bus exception */
289 #define BUSPROBE(val, addr) get_dbe((val), (addr))
290 #include <asm/paccess.h>
292 #define BUSPROBE(val, addr) ({ (val) = R_REG(NULL, (addr)); 0; })
295 /* map/unmap physical to virtual I/O */
296 #if !defined(CONFIG_MMC_MSM7X00A)
297 #define REG_MAP(pa, size) ioremap_nocache((unsigned long)(pa), (unsigned long)(size))
299 #define REG_MAP(pa, size) (void *)(0)
300 #endif /* !defined(CONFIG_MMC_MSM7X00A */
301 #define REG_UNMAP(va) iounmap((va))
304 #define W_SM(r, v) (*(r) = (v))
305 #define BZERO_SM(r, len) memset((r), '\0', (len))
307 #include <linuxver.h> /* use current 2.4.x calling conventions */
309 /* packet primitives */
310 #define PKTGET(osh, len, send) osl_pktget((osh), (len))
311 #define PKTDUP(osh, skb) osl_pktdup((osh), (skb))
312 #define PKTFREE(osh, skb, send) osl_pktfree((osh), (skb), (send))
313 #define PKTDATA(skb) (((struct sk_buff*)(skb))->data)
314 #define PKTLEN(skb) (((struct sk_buff*)(skb))->len)
315 #define PKTHEADROOM(skb) (PKTDATA(skb)-(((struct sk_buff*)(skb))->head))
316 #define PKTTAILROOM(skb) ((((struct sk_buff*)(skb))->end)-(((struct sk_buff*)(skb))->tail))
317 #define PKTNEXT(skb) (((struct sk_buff*)(skb))->next)
318 #define PKTSETNEXT(skb, x) (((struct sk_buff*)(skb))->next = (struct sk_buff*)(x))
319 #define PKTSETLEN(skb, len) __skb_trim((struct sk_buff*)(skb), (len))
320 #define PKTPUSH(skb, bytes) skb_push((struct sk_buff*)(skb), (bytes))
321 #define PKTPULL(skb, bytes) skb_pull((struct sk_buff*)(skb), (bytes))
322 #define PKTTAG(skb) ((void*)(((struct sk_buff*)(skb))->cb))
323 #define PKTALLOCED(osh) ((osl_pubinfo_t *)(osh))->pktalloced
324 #define PKTSETPOOL(osh, skb, x, y) do {} while (0)
325 #define PKTPOOL(osh, skb) FALSE
327 #define PKTUNALLOC(osh) ((osl_pubinfo_t *)(osh))->pktalloced--
329 #define PKTSETSKIPCT(osh, skb)
330 #define PKTCLRSKIPCT(osh, skb)
331 #define PKTSKIPCT(osh, skb)
333 extern void osl_pktfree(osl_t * osh, void *skb, bool send);
335 extern void *osl_pktget(osl_t * osh, uint len);
336 extern void *osl_pktdup(osl_t * osh, void *skb);
338 #define PKTLINK(skb) (((struct sk_buff*)(skb))->prev)
339 #define PKTSETLINK(skb, x) (((struct sk_buff*)(skb))->prev = (struct sk_buff*)(x))
340 #define PKTPRIO(skb) (((struct sk_buff*)(skb))->priority)
341 #define PKTSETPRIO(skb, x) (((struct sk_buff*)(skb))->priority = (x))
342 #define PKTSUMNEEDED(skb) (((struct sk_buff*)(skb))->ip_summed == CHECKSUM_HW)
343 #define PKTSETSUMGOOD(skb, x) (((struct sk_buff*)(skb))->ip_summed = \
344 ((x) ? CHECKSUM_UNNECESSARY : CHECKSUM_NONE))
345 /* PKTSETSUMNEEDED and PKTSUMGOOD are not possible because skb->ip_summed is overloaded */
346 #define PKTSHARED(skb) (((struct sk_buff*)(skb))->cloned)
349 #define RPC_READ_REG(osh, r) (\
350 sizeof(*(r)) == sizeof(uint8) ? osl_readb((osh), (volatile uint8*)(r)) : \
351 sizeof(*(r)) == sizeof(uint16) ? osl_readw((osh), (volatile uint16*)(r)) : \
352 osl_readl((osh), (volatile uint32*)(r)) \
354 #define RPC_WRITE_REG(osh, r, v) do { \
355 switch (sizeof(*(r))) { \
356 case sizeof(uint8): osl_writeb((osh), (volatile uint8*)(r), (uint8)(v)); break; \
357 case sizeof(uint16): osl_writew((osh), (volatile uint16*)(r), (uint16)(v)); break; \
358 case sizeof(uint32): osl_writel((osh), (volatile uint32*)(r), (uint32)(v)); break; \
362 extern uint8 osl_readb(osl_t * osh, volatile uint8 * r);
363 extern uint16 osl_readw(osl_t * osh, volatile uint16 * r);
364 extern uint32 osl_readl(osl_t * osh, volatile uint32 * r);
365 extern void osl_writeb(osl_t * osh, volatile uint8 * r, uint8 v);
366 extern void osl_writew(osl_t * osh, volatile uint16 * r, uint16 v);
367 extern void osl_writel(osl_t * osh, volatile uint32 * r, uint32 v);
370 #endif /* _linux_osl_h_ */