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 */
246 #define SELECT_BUS_WRITE(mmap_op, bus_op) bus_op
247 #define SELECT_BUS_READ(mmap_op, bus_op) bus_op
249 #define SELECT_BUS_WRITE(mmap_op, bus_op) mmap_op
250 #define SELECT_BUS_READ(mmap_op, bus_op) mmap_op
253 /* the largest reasonable packet buffer driver uses for ethernet MTU in bytes */
254 #define PKTBUFSZ 2048
256 #define OSL_SYSUPTIME() ((u32)jiffies * (1000 / HZ))
258 #include <linux/kernel.h> /* for vsn/printf's */
259 #include <linux/string.h> /* for mem*, str* */
262 /* register access macros */
266 SELECT_BUS_READ(sizeof(*(r)) == sizeof(u8) ? \
267 readb((volatile u8*)(r)) : \
268 sizeof(*(r)) == sizeof(u16) ? readw((volatile u16*)(r)) : \
269 readl((volatile u32*)(r)), bcmsdh_reg_read(NULL, (unsigned long)r, sizeof(*r))) \
275 __typeof(*(r)) __osl_v; \
276 __asm__ __volatile__("sync"); \
277 switch (sizeof(*(r))) { \
279 __osl_v = readb((volatile u8*)(r)); \
282 __osl_v = readw((volatile u16*)(r)); \
286 readl((volatile u32*)(r)); \
289 __asm__ __volatile__("sync"); \
293 __typeof(*(r)) __osl_v; \
294 __asm__ __volatile__("sync"); \
295 __osl_v = bcmsdh_reg_read(NULL, (unsigned long)r, sizeof(*r)); \
296 __asm__ __volatile__("sync"); \
300 #endif /* __mips__ */
302 #define W_REG(r, v) do { \
304 switch (sizeof(*(r))) { \
306 writeb((u8)(v), (volatile u8*)(r)); break; \
308 writew((u16)(v), (volatile u16*)(r)); break; \
310 writel((u32)(v), (volatile u32*)(r)); break; \
312 bcmsdh_reg_write(NULL, (unsigned long)r, sizeof(*r), (v))); \
314 #else /* __BIG_ENDIAN */
318 __typeof(*(r)) __osl_v; \
319 switch (sizeof(*(r))) { \
322 readb((volatile u8*)((r)^3)); \
326 readw((volatile u16*)((r)^2)); \
329 __osl_v = readl((volatile u32*)(r)); \
334 bcmsdh_reg_read(NULL, (unsigned long)r, sizeof(*r))) \
336 #define W_REG(r, v) do { \
338 switch (sizeof(*(r))) { \
341 (volatile u8*)((r)^3)); break; \
344 (volatile u16*)((r)^2)); break; \
347 (volatile u32*)(r)); break; \
349 bcmsdh_reg_write(NULL, (unsigned long)r, sizeof(*r), v)); \
351 #endif /* __BIG_ENDIAN */
355 * bcm4716 (which includes 4717 & 4718), plus 4706 on PCIe can reorder
356 * transactions. As a fix, a read after write is performed on certain places
357 * in the code. Older chips and the newer 5357 family don't require this fix.
359 #define W_REG_FLUSH(r, v) ({ W_REG((r), (v)); (void)R_REG(r); })
361 #define W_REG_FLUSH(r, v) W_REG((r), (v))
362 #endif /* __mips__ */
364 #define AND_REG(r, v) W_REG((r), R_REG(r) & (v))
365 #define OR_REG(r, v) W_REG((r), R_REG(r) | (v))
367 #define SET_REG(r, mask, val) \
368 W_REG((r), ((R_REG(r) & ~(mask)) | (val)))
371 #ifndef NBBY /* the BSD family defines NBBY */
372 #define NBBY 8 /* 8 bits per byte */
373 #endif /* #ifndef NBBY */
374 #define setbit(a, i) (((u8 *)a)[(i)/NBBY] |= 1<<((i)%NBBY))
375 #define clrbit(a, i) (((u8 *)a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
376 #define isset(a, i) (((const u8 *)a)[(i)/NBBY] & (1<<((i)%NBBY)))
377 #define isclr(a, i) ((((const u8 *)a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
380 #define NBITS(type) (sizeof(type) * 8)
381 #define NBITVAL(nbits) (1 << (nbits))
382 #define MAXBITVAL(nbits) ((1 << (nbits)) - 1)
383 #define NBITMASK(nbits) MAXBITVAL(nbits)
384 #define MAXNBVAL(nbyte) MAXBITVAL((nbyte) * 8)
386 /* basic mux operation - can be optimized on several architectures */
387 #define MUX(pred, true, false) ((pred) ? (true) : (false))
389 /* modulo inc/dec - assumes x E [0, bound - 1] */
390 #define MODDEC(x, bound) MUX((x) == 0, (bound) - 1, (x) - 1)
391 #define MODINC(x, bound) MUX((x) == (bound) - 1, 0, (x) + 1)
393 /* modulo inc/dec, bound = 2^k */
394 #define MODDEC_POW2(x, bound) (((x) - 1) & ((bound) - 1))
395 #define MODINC_POW2(x, bound) (((x) + 1) & ((bound) - 1))
397 /* modulo add/sub - assumes x, y E [0, bound - 1] */
398 #define MODADD(x, y, bound) \
399 MUX((x) + (y) >= (bound), (x) + (y) - (bound), (x) + (y))
400 #define MODSUB(x, y, bound) \
401 MUX(((int)(x)) - ((int)(y)) < 0, (x) - (y) + (bound), (x) - (y))
403 /* module add/sub, bound = 2^k */
404 #define MODADD_POW2(x, y, bound) (((x) + (y)) & ((bound) - 1))
405 #define MODSUB_POW2(x, y, bound) (((x) - (y)) & ((bound) - 1))
408 #define CRC8_INIT_VALUE 0xff /* Initial CRC8 checksum value */
409 #define CRC8_GOOD_VALUE 0x9f /* Good final CRC8 checksum value */
410 #define CRC16_INIT_VALUE 0xffff /* Initial CRC16 checksum value */
411 #define CRC16_GOOD_VALUE 0xf0b8 /* Good final CRC16 checksum value */
413 /* brcmu_format_flags() bit description structure */
414 struct brcmu_bit_desc {
419 /* tag_ID/length/value_buffer tuple */
426 #define ETHER_ADDR_STR_LEN 18 /* 18-bytes of Ethernet address buffer length */
428 /* crypto utility function */
429 /* 128-bit xor: *dst = *src1 xor *src2. dst1, src1 and src2 may have any alignment */
431 xor_128bit_block(const u8 *src1, const u8 *src2, u8 *dst) {
436 (((unsigned long) src1 | (unsigned long) src2 | (unsigned long) dst) &
438 /* ARM CM3 rel time: 1229 (727 if alignment check could be omitted) */
439 /* x86 supports unaligned. This version runs 6x-9x faster on x86. */
441 ((const u32 *)src1)[0] ^ ((const u32 *)
444 ((const u32 *)src1)[1] ^ ((const u32 *)
447 ((const u32 *)src1)[2] ^ ((const u32 *)
450 ((const u32 *)src1)[3] ^ ((const u32 *)
453 /* ARM CM3 rel time: 4668 (4191 if alignment check could be omitted) */
455 for (k = 0; k < 16; k++)
456 dst[k] = src1[k] ^ src2[k];
462 extern u8 brcmu_crc8(u8 *p, uint nbytes, u8 crc);
466 extern int brcmu_format_flags(const struct brcmu_bit_desc *bd, u32 flags,
468 extern int brcmu_format_hex(char *str, const void *bytes, int len);
471 extern char *brcmu_chipname(uint chipid, char *buf, uint len);
473 extern struct brcmu_tlv *brcmu_parse_tlvs(void *buf, int buflen,
476 /* multi-bool data type: set of bools, mbool is true if any is set */
478 #define mboolset(mb, bit) ((mb) |= (bit)) /* set one bool */
479 #define mboolclr(mb, bit) ((mb) &= ~(bit)) /* clear one bool */
480 #define mboolisset(mb, bit) (((mb) & (bit)) != 0) /* true if one bool is set */
481 #define mboolmaskset(mb, mask, val) ((mb) = (((mb) & ~(mask)) | (val)))
483 /* power conversion */
484 extern u16 brcmu_qdbm_to_mw(u8 qdbm);
485 extern u8 brcmu_mw_to_qdbm(u16 mw);
487 extern void brcmu_binit(struct brcmu_strbuf *b, char *buf, uint size);
488 extern int brcmu_bprintf(struct brcmu_strbuf *b, const char *fmt, ...);
490 extern uint brcmu_mkiovar(char *name, char *data, uint datalen,
491 char *buf, uint len);
492 extern uint brcmu_bitcount(u8 *bitmap, uint bytelength);
494 #endif /* _brcmutils_h_ */