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.
20 /* Buffer structure for collecting string-formatted data
21 * using brcmu_bprintf() API.
22 * Use brcmu_binit() to initialize before use
26 char *buf; /* pointer to current position in origbuf */
27 unsigned int size; /* current (residual) size in bytes */
28 char *origbuf; /* unmodified pointer to orignal buffer */
29 unsigned int origsize; /* unmodified orignal buffer size in bytes */
32 /* ** driver-only section ** */
34 #define GPIO_PIN_NOTDEFINED 0x20 /* Pin not defined */
37 * Spin at most 'us' microseconds while 'exp' is true.
38 * Caller should explicitly test 'exp' when this completes
39 * and take appropriate error action if 'exp' is still true.
41 #define SPINWAIT(exp, us) { \
42 uint countdown = (us) + 9; \
43 while ((exp) && (countdown >= 10)) {\
49 /* osl multi-precedence packet queue */
50 #ifndef PKTQ_LEN_DEFAULT
51 #define PKTQ_LEN_DEFAULT 128 /* Max 128 packets */
54 #define PKTQ_MAX_PREC 16 /* Maximum precedence levels */
58 struct sk_buff *head; /* first packet to dequeue */
59 struct sk_buff *tail; /* last packet to dequeue */
60 u16 len; /* number of queued packets */
61 u16 max; /* maximum number of queued packets */
64 /* multi-priority pkt queue */
66 u16 num_prec; /* number of precedences in use */
67 u16 hi_prec; /* rapid dequeue hint (>= highest non-empty prec) */
68 u16 max; /* total max packets */
69 u16 len; /* total number of packets */
70 /* q array must be last since # of elements can be either PKTQ_MAX_PREC or 1 */
71 struct pktq_prec q[PKTQ_MAX_PREC];
74 #define PKTQ_PREC_ITER(pq, prec) for (prec = (pq)->num_prec - 1; prec >= 0; prec--)
76 /* fn(pkt, arg). return true if pkt belongs to if */
77 typedef bool(*ifpkt_cb_t) (struct sk_buff *, void *);
79 /* operations on a specific precedence in packet queue */
81 #define pktq_psetmax(pq, prec, _max) ((pq)->q[prec].max = (_max))
82 #define pktq_plen(pq, prec) ((pq)->q[prec].len)
83 #define pktq_pavail(pq, prec) ((pq)->q[prec].max - (pq)->q[prec].len)
84 #define pktq_pfull(pq, prec) ((pq)->q[prec].len >= (pq)->q[prec].max)
85 #define pktq_pempty(pq, prec) ((pq)->q[prec].len == 0)
87 #define pktq_ppeek(pq, prec) ((pq)->q[prec].head)
88 #define pktq_ppeek_tail(pq, prec) ((pq)->q[prec].tail)
90 extern struct sk_buff *brcmu_pktq_penq(struct pktq *pq, int prec,
92 extern struct sk_buff *brcmu_pktq_penq_head(struct pktq *pq, int prec,
94 extern struct sk_buff *brcmu_pktq_pdeq(struct pktq *pq, int prec);
95 extern struct sk_buff *brcmu_pktq_pdeq_tail(struct pktq *pq, int prec);
97 /* packet primitives */
98 extern struct sk_buff *brcmu_pkt_buf_get_skb(uint len);
99 extern void brcmu_pkt_buf_free_skb(struct sk_buff *skb);
101 /* Empty the queue at particular precedence level */
102 extern void brcmu_pktq_pflush(struct pktq *pq, int prec,
103 bool dir, ifpkt_cb_t fn, void *arg);
105 /* operations on a set of precedences in packet queue */
107 extern int brcmu_pktq_mlen(struct pktq *pq, uint prec_bmp);
108 extern struct sk_buff *brcmu_pktq_mdeq(struct pktq *pq, uint prec_bmp,
111 /* operations on packet queue as a whole */
113 #define pktq_len(pq) ((int)(pq)->len)
114 #define pktq_max(pq) ((int)(pq)->max)
115 #define pktq_avail(pq) ((int)((pq)->max - (pq)->len))
116 #define pktq_full(pq) ((pq)->len >= (pq)->max)
117 #define pktq_empty(pq) ((pq)->len == 0)
119 /* operations for single precedence queues */
120 #define pktenq(pq, p) brcmu_pktq_penq(((struct pktq *)pq), 0, (p))
121 #define pktenq_head(pq, p)\
122 brcmu_pktq_penq_head(((struct pktq *)pq), 0, (p))
123 #define pktdeq(pq) brcmu_pktq_pdeq(((struct pktq *)pq), 0)
124 #define pktdeq_tail(pq) brcmu_pktq_pdeq_tail(((struct pktq *)pq), 0)
125 #define pktqinit(pq, len) brcmu_pktq_init(((struct pktq *)pq), 1, len)
127 extern void brcmu_pktq_init(struct pktq *pq, int num_prec, int max_len);
128 /* prec_out may be NULL if caller is not interested in return value */
129 extern struct sk_buff *brcmu_pktq_peek_tail(struct pktq *pq, int *prec_out);
130 extern void brcmu_pktq_flush(struct pktq *pq, bool dir,
131 ifpkt_cb_t fn, void *arg);
135 extern uint brcmu_pktfrombuf(struct sk_buff *p,
136 uint offset, int len, unsigned char *buf);
137 extern uint brcmu_pkttotlen(struct sk_buff *p);
139 /* ethernet address */
140 extern int brcmu_ether_atoe(char *p, u8 *ea);
146 extern void brcmu_prpkt(const char *msg, struct sk_buff *p0);
148 #define brcmu_prpkt(a, b)
151 /* Support for sharing code across in-driver iovar implementations.
152 * The intent is that a driver use this structure to map iovar names
153 * to its (private) iovar identifiers, and the lookup function to
154 * find the entry. Macros are provided to map ids and get/set actions
155 * into a single number space for a switch statement.
158 /* iovar structure */
160 const char *name; /* name for lookup and display */
161 u16 varid; /* id for switch */
162 u16 flags; /* driver-specific flag bits */
163 u16 type; /* base type of argument */
164 u16 minlen; /* min length for buffer vars */
167 /* varid definitions are per-driver, may use these get/set bits */
169 /* IOVar action bits for id mapping */
170 #define IOV_GET 0 /* Get an iovar */
171 #define IOV_SET 1 /* Set an iovar */
173 /* Varid to actionid mapping */
174 #define IOV_GVAL(id) ((id)*2)
175 #define IOV_SVAL(id) (((id)*2)+IOV_SET)
176 #define IOV_ISSET(actionid) ((actionid & IOV_SET) == IOV_SET)
177 #define IOV_ID(actionid) (actionid >> 1)
180 brcmu_iovar *brcmu_iovar_lookup(const struct brcmu_iovar *table,
182 extern int brcmu_iovar_lencheck(const struct brcmu_iovar *table, void *arg,
185 /* Base type definitions */
186 #define IOVT_VOID 0 /* no value (implictly set only) */
187 #define IOVT_BOOL 1 /* any value ok (zero/nonzero) */
188 #define IOVT_INT8 2 /* integer values are range-checked */
189 #define IOVT_UINT8 3 /* unsigned int 8 bits */
190 #define IOVT_INT16 4 /* int 16 bits */
191 #define IOVT_UINT16 5 /* unsigned int 16 bits */
192 #define IOVT_INT32 6 /* int 32 bits */
193 #define IOVT_UINT32 7 /* unsigned int 32 bits */
194 #define IOVT_BUFFER 8 /* buffer is size-checked as per minlen */
195 #define BCM_IOVT_VALID(type) (((unsigned int)(type)) <= IOVT_BUFFER)
197 /* Initializer for IOV type strings */
198 #define BCM_IOV_TYPE_INIT { \
210 #define BCM_IOVT_IS_INT(type) (\
211 (type == IOVT_BOOL) || \
212 (type == IOVT_INT8) || \
213 (type == IOVT_UINT8) || \
214 (type == IOVT_INT16) || \
215 (type == IOVT_UINT16) || \
216 (type == IOVT_INT32) || \
217 (type == IOVT_UINT32))
219 /* ** driver/apps-shared section ** */
221 #define BCME_STRLEN 64 /* Max string length for BCM errors */
224 #define ABS(a) (((a) < 0) ? -(a) : (a))
227 #define CEIL(x, y) (((x) + ((y)-1)) / (y))
228 #define ISPOWEROF2(x) ((((x)-1)&(x)) == 0)
230 /* map physical to virtual I/O */
231 #if !defined(CONFIG_MMC_MSM7X00A)
232 #define REG_MAP(pa, size) ioremap_nocache((unsigned long)(pa), \
233 (unsigned long)(size))
235 #define REG_MAP(pa, size) (void *)(0)
238 /* register access macros */
243 #define OSL_WRITE_REG(r, v) \
244 (bcmsdh_reg_write(NULL, (unsigned long)(r), sizeof(*(r)), (v)))
245 #define OSL_READ_REG(r) \
246 (bcmsdh_reg_read(NULL, (unsigned long)(r), sizeof(*(r))))
250 #define SELECT_BUS_WRITE(mmap_op, bus_op) bus_op
251 #define SELECT_BUS_READ(mmap_op, bus_op) bus_op
253 #define SELECT_BUS_WRITE(mmap_op, bus_op) mmap_op
254 #define SELECT_BUS_READ(mmap_op, bus_op) mmap_op
257 /* the largest reasonable packet buffer driver uses for ethernet MTU in bytes */
258 #define PKTBUFSZ 2048
260 #define OSL_SYSUPTIME() ((u32)jiffies * (1000 / HZ))
262 #include <linux/kernel.h> /* for vsn/printf's */
263 #include <linux/string.h> /* for mem*, str* */
266 /* register access macros */
270 SELECT_BUS_READ(sizeof(*(r)) == sizeof(u8) ? \
271 readb((volatile u8*)(r)) : \
272 sizeof(*(r)) == sizeof(u16) ? readw((volatile u16*)(r)) : \
273 readl((volatile u32*)(r)), OSL_READ_REG(r)) \
279 __typeof(*(r)) __osl_v; \
280 __asm__ __volatile__("sync"); \
281 switch (sizeof(*(r))) { \
283 __osl_v = readb((volatile u8*)(r)); \
286 __osl_v = readw((volatile u16*)(r)); \
290 readl((volatile u32*)(r)); \
293 __asm__ __volatile__("sync"); \
297 __typeof(*(r)) __osl_v; \
298 __asm__ __volatile__("sync"); \
299 __osl_v = OSL_READ_REG(r); \
300 __asm__ __volatile__("sync"); \
304 #endif /* __mips__ */
306 #define W_REG(r, v) do { \
308 switch (sizeof(*(r))) { \
310 writeb((u8)(v), (volatile u8*)(r)); break; \
312 writew((u16)(v), (volatile u16*)(r)); break; \
314 writel((u32)(v), (volatile u32*)(r)); break; \
316 (OSL_WRITE_REG(r, v))); \
318 #else /* __BIG_ENDIAN */
322 __typeof(*(r)) __osl_v; \
323 switch (sizeof(*(r))) { \
326 readb((volatile u8*)((r)^3)); \
330 readw((volatile u16*)((r)^2)); \
333 __osl_v = readl((volatile u32*)(r)); \
340 #define W_REG(r, v) do { \
342 switch (sizeof(*(r))) { \
345 (volatile u8*)((r)^3)); break; \
348 (volatile u16*)((r)^2)); break; \
351 (volatile u32*)(r)); break; \
353 (OSL_WRITE_REG(r, v))); \
355 #endif /* __BIG_ENDIAN */
359 * bcm4716 (which includes 4717 & 4718), plus 4706 on PCIe can reorder
360 * transactions. As a fix, a read after write is performed on certain places
361 * in the code. Older chips and the newer 5357 family don't require this fix.
363 #define W_REG_FLUSH(r, v) ({ W_REG((r), (v)); (void)R_REG(r); })
365 #define W_REG_FLUSH(r, v) W_REG((r), (v))
366 #endif /* __mips__ */
368 #define AND_REG(r, v) W_REG((r), R_REG(r) & (v))
369 #define OR_REG(r, v) W_REG((r), R_REG(r) | (v))
371 #define SET_REG(r, mask, val) \
372 W_REG((r), ((R_REG(r) & ~(mask)) | (val)))
375 #ifndef NBBY /* the BSD family defines NBBY */
376 #define NBBY 8 /* 8 bits per byte */
377 #endif /* #ifndef NBBY */
378 #define setbit(a, i) (((u8 *)a)[(i)/NBBY] |= 1<<((i)%NBBY))
379 #define clrbit(a, i) (((u8 *)a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
380 #define isset(a, i) (((const u8 *)a)[(i)/NBBY] & (1<<((i)%NBBY)))
381 #define isclr(a, i) ((((const u8 *)a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
384 #define NBITS(type) (sizeof(type) * 8)
385 #define NBITVAL(nbits) (1 << (nbits))
386 #define MAXBITVAL(nbits) ((1 << (nbits)) - 1)
387 #define NBITMASK(nbits) MAXBITVAL(nbits)
388 #define MAXNBVAL(nbyte) MAXBITVAL((nbyte) * 8)
390 /* basic mux operation - can be optimized on several architectures */
391 #define MUX(pred, true, false) ((pred) ? (true) : (false))
393 /* modulo inc/dec - assumes x E [0, bound - 1] */
394 #define MODDEC(x, bound) MUX((x) == 0, (bound) - 1, (x) - 1)
395 #define MODINC(x, bound) MUX((x) == (bound) - 1, 0, (x) + 1)
397 /* modulo inc/dec, bound = 2^k */
398 #define MODDEC_POW2(x, bound) (((x) - 1) & ((bound) - 1))
399 #define MODINC_POW2(x, bound) (((x) + 1) & ((bound) - 1))
401 /* modulo add/sub - assumes x, y E [0, bound - 1] */
402 #define MODADD(x, y, bound) \
403 MUX((x) + (y) >= (bound), (x) + (y) - (bound), (x) + (y))
404 #define MODSUB(x, y, bound) \
405 MUX(((int)(x)) - ((int)(y)) < 0, (x) - (y) + (bound), (x) - (y))
407 /* module add/sub, bound = 2^k */
408 #define MODADD_POW2(x, y, bound) (((x) + (y)) & ((bound) - 1))
409 #define MODSUB_POW2(x, y, bound) (((x) - (y)) & ((bound) - 1))
412 #define CRC8_INIT_VALUE 0xff /* Initial CRC8 checksum value */
413 #define CRC8_GOOD_VALUE 0x9f /* Good final CRC8 checksum value */
414 #define CRC16_INIT_VALUE 0xffff /* Initial CRC16 checksum value */
415 #define CRC16_GOOD_VALUE 0xf0b8 /* Good final CRC16 checksum value */
417 /* brcmu_format_flags() bit description structure */
418 struct brcmu_bit_desc {
423 /* tag_ID/length/value_buffer tuple */
430 #define ETHER_ADDR_STR_LEN 18 /* 18-bytes of Ethernet address buffer length */
432 /* crypto utility function */
433 /* 128-bit xor: *dst = *src1 xor *src2. dst1, src1 and src2 may have any alignment */
435 xor_128bit_block(const u8 *src1, const u8 *src2, u8 *dst) {
440 (((unsigned long) src1 | (unsigned long) src2 | (unsigned long) dst) &
442 /* ARM CM3 rel time: 1229 (727 if alignment check could be omitted) */
443 /* x86 supports unaligned. This version runs 6x-9x faster on x86. */
445 ((const u32 *)src1)[0] ^ ((const u32 *)
448 ((const u32 *)src1)[1] ^ ((const u32 *)
451 ((const u32 *)src1)[2] ^ ((const u32 *)
454 ((const u32 *)src1)[3] ^ ((const u32 *)
457 /* ARM CM3 rel time: 4668 (4191 if alignment check could be omitted) */
459 for (k = 0; k < 16; k++)
460 dst[k] = src1[k] ^ src2[k];
466 extern u8 brcmu_crc8(u8 *p, uint nbytes, u8 crc);
470 extern int brcmu_format_flags(const struct brcmu_bit_desc *bd, u32 flags,
472 extern int brcmu_format_hex(char *str, const void *bytes, int len);
475 extern char *brcmu_chipname(uint chipid, char *buf, uint len);
477 extern struct brcmu_tlv *brcmu_parse_tlvs(void *buf, int buflen,
480 /* multi-bool data type: set of bools, mbool is true if any is set */
482 #define mboolset(mb, bit) ((mb) |= (bit)) /* set one bool */
483 #define mboolclr(mb, bit) ((mb) &= ~(bit)) /* clear one bool */
484 #define mboolisset(mb, bit) (((mb) & (bit)) != 0) /* true if one bool is set */
485 #define mboolmaskset(mb, mask, val) ((mb) = (((mb) & ~(mask)) | (val)))
487 /* power conversion */
488 extern u16 brcmu_qdbm_to_mw(u8 qdbm);
489 extern u8 brcmu_mw_to_qdbm(u16 mw);
491 extern void brcmu_binit(struct brcmu_strbuf *b, char *buf, uint size);
492 extern int brcmu_bprintf(struct brcmu_strbuf *b, const char *fmt, ...);
494 extern uint brcmu_mkiovar(char *name, char *data, uint datalen,
495 char *buf, uint len);
496 extern uint brcmu_bitcount(u8 *bitmap, uint bytelength);
498 #endif /* _brcmutils_h_ */