staging: brcm80211: Remove si sb utils from brcmfmac
[pohmelfs.git] / drivers / staging / brcm80211 / brcmfmac / bcmsdh.c
blob0627b7d2cc406cedab25bd18204ae4f20d68fc42
1 /*
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.
16 /* ****************** BCMSDH Interface Functions *************************** */
18 #include <linux/types.h>
19 #include <linux/netdevice.h>
20 #include <linux/pci_ids.h>
21 #include <bcmdefs.h>
22 #include <bcmdevs.h>
23 #include <bcmutils.h>
24 #include <hndsoc.h>
26 #include <bcmsdh.h> /* BRCM API for SDIO
27 clients (such as wl, dhd) */
28 #include <bcmsdbus.h> /* common SDIO/controller interface */
29 #include <sbsdio.h> /* BRCM sdio device core */
31 #include <sdio.h> /* sdio spec */
33 #define SDIOH_API_ACCESS_RETRY_LIMIT 2
34 const uint bcmsdh_msglevel = BCMSDH_ERROR_VAL;
36 struct bcmsdh_info {
37 bool init_success; /* underlying driver successfully attached */
38 void *sdioh; /* handler for sdioh */
39 u32 vendevid; /* Target Vendor and Device ID on SD bus */
40 bool regfail; /* Save status of last
41 reg_read/reg_write call */
42 u32 sbwad; /* Save backplane window address */
44 /* local copy of bcm sd handler */
45 bcmsdh_info_t *l_bcmsdh;
47 #if defined(OOB_INTR_ONLY) && defined(HW_OOB)
48 extern int sdioh_enable_hw_oob_intr(void *sdioh, bool enable);
50 void bcmsdh_enable_hw_oob_intr(bcmsdh_info_t *sdh, bool enable)
52 sdioh_enable_hw_oob_intr(sdh->sdioh, enable);
54 #endif
56 bcmsdh_info_t *bcmsdh_attach(void *cfghdl, void **regsva, uint irq)
58 bcmsdh_info_t *bcmsdh;
60 bcmsdh = kzalloc(sizeof(bcmsdh_info_t), GFP_ATOMIC);
61 if (bcmsdh == NULL) {
62 BCMSDH_ERROR(("bcmsdh_attach: out of memory"));
63 return NULL;
66 /* save the handler locally */
67 l_bcmsdh = bcmsdh;
69 bcmsdh->sdioh = sdioh_attach(cfghdl, irq);
70 if (!bcmsdh->sdioh) {
71 bcmsdh_detach(bcmsdh);
72 return NULL;
75 bcmsdh->init_success = true;
77 *regsva = (u32 *) SI_ENUM_BASE;
79 /* Report the BAR, to fix if needed */
80 bcmsdh->sbwad = SI_ENUM_BASE;
81 return bcmsdh;
84 int bcmsdh_detach(void *sdh)
86 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
88 if (bcmsdh != NULL) {
89 if (bcmsdh->sdioh) {
90 sdioh_detach(bcmsdh->sdioh);
91 bcmsdh->sdioh = NULL;
93 kfree(bcmsdh);
96 l_bcmsdh = NULL;
97 return 0;
101 bcmsdh_iovar_op(void *sdh, const char *name,
102 void *params, int plen, void *arg, int len, bool set)
104 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
105 return sdioh_iovar_op(bcmsdh->sdioh, name, params, plen, arg, len, set);
108 bool bcmsdh_intr_query(void *sdh)
110 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
111 SDIOH_API_RC status;
112 bool on;
114 ASSERT(bcmsdh);
115 status = sdioh_interrupt_query(bcmsdh->sdioh, &on);
116 if (SDIOH_API_SUCCESS(status))
117 return false;
118 else
119 return on;
122 int bcmsdh_intr_enable(void *sdh)
124 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
125 SDIOH_API_RC status;
126 ASSERT(bcmsdh);
128 status = sdioh_interrupt_set(bcmsdh->sdioh, true);
129 return SDIOH_API_SUCCESS(status) ? 0 : BCME_ERROR;
132 int bcmsdh_intr_disable(void *sdh)
134 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
135 SDIOH_API_RC status;
136 ASSERT(bcmsdh);
138 status = sdioh_interrupt_set(bcmsdh->sdioh, false);
139 return SDIOH_API_SUCCESS(status) ? 0 : BCME_ERROR;
142 int bcmsdh_intr_reg(void *sdh, bcmsdh_cb_fn_t fn, void *argh)
144 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
145 SDIOH_API_RC status;
146 ASSERT(bcmsdh);
148 status = sdioh_interrupt_register(bcmsdh->sdioh, fn, argh);
149 return SDIOH_API_SUCCESS(status) ? 0 : BCME_ERROR;
152 int bcmsdh_intr_dereg(void *sdh)
154 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
155 SDIOH_API_RC status;
156 ASSERT(bcmsdh);
158 status = sdioh_interrupt_deregister(bcmsdh->sdioh);
159 return SDIOH_API_SUCCESS(status) ? 0 : BCME_ERROR;
162 #if defined(DHD_DEBUG)
163 bool bcmsdh_intr_pending(void *sdh)
165 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
167 ASSERT(sdh);
168 return sdioh_interrupt_pending(bcmsdh->sdioh);
170 #endif
172 int bcmsdh_devremove_reg(void *sdh, bcmsdh_cb_fn_t fn, void *argh)
174 ASSERT(sdh);
176 /* don't support yet */
177 return BCME_UNSUPPORTED;
180 u8 bcmsdh_cfg_read(void *sdh, uint fnc_num, u32 addr, int *err)
182 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
183 SDIOH_API_RC status;
184 #ifdef SDIOH_API_ACCESS_RETRY_LIMIT
185 s32 retry = 0;
186 #endif
187 u8 data = 0;
189 if (!bcmsdh)
190 bcmsdh = l_bcmsdh;
192 ASSERT(bcmsdh->init_success);
194 #ifdef SDIOH_API_ACCESS_RETRY_LIMIT
195 do {
196 if (retry) /* wait for 1 ms till bus get settled down */
197 udelay(1000);
198 #endif
199 status =
200 sdioh_cfg_read(bcmsdh->sdioh, fnc_num, addr,
201 (u8 *) &data);
202 #ifdef SDIOH_API_ACCESS_RETRY_LIMIT
203 } while (!SDIOH_API_SUCCESS(status)
204 && (retry++ < SDIOH_API_ACCESS_RETRY_LIMIT));
205 #endif
206 if (err)
207 *err = (SDIOH_API_SUCCESS(status) ? 0 : BCME_SDIO_ERROR);
209 BCMSDH_INFO(("%s:fun = %d, addr = 0x%x, u8data = 0x%x\n",
210 __func__, fnc_num, addr, data));
212 return data;
215 void
216 bcmsdh_cfg_write(void *sdh, uint fnc_num, u32 addr, u8 data, int *err)
218 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
219 SDIOH_API_RC status;
220 #ifdef SDIOH_API_ACCESS_RETRY_LIMIT
221 s32 retry = 0;
222 #endif
224 if (!bcmsdh)
225 bcmsdh = l_bcmsdh;
227 ASSERT(bcmsdh->init_success);
229 #ifdef SDIOH_API_ACCESS_RETRY_LIMIT
230 do {
231 if (retry) /* wait for 1 ms till bus get settled down */
232 udelay(1000);
233 #endif
234 status =
235 sdioh_cfg_write(bcmsdh->sdioh, fnc_num, addr,
236 (u8 *) &data);
237 #ifdef SDIOH_API_ACCESS_RETRY_LIMIT
238 } while (!SDIOH_API_SUCCESS(status)
239 && (retry++ < SDIOH_API_ACCESS_RETRY_LIMIT));
240 #endif
241 if (err)
242 *err = SDIOH_API_SUCCESS(status) ? 0 : BCME_SDIO_ERROR;
244 BCMSDH_INFO(("%s:fun = %d, addr = 0x%x, u8data = 0x%x\n",
245 __func__, fnc_num, addr, data));
248 u32 bcmsdh_cfg_read_word(void *sdh, uint fnc_num, u32 addr, int *err)
250 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
251 SDIOH_API_RC status;
252 u32 data = 0;
254 if (!bcmsdh)
255 bcmsdh = l_bcmsdh;
257 ASSERT(bcmsdh->init_success);
259 status =
260 sdioh_request_word(bcmsdh->sdioh, SDIOH_CMD_TYPE_NORMAL, SDIOH_READ,
261 fnc_num, addr, &data, 4);
263 if (err)
264 *err = (SDIOH_API_SUCCESS(status) ? 0 : BCME_SDIO_ERROR);
266 BCMSDH_INFO(("%s:fun = %d, addr = 0x%x, u32data = 0x%x\n",
267 __func__, fnc_num, addr, data));
269 return data;
272 void
273 bcmsdh_cfg_write_word(void *sdh, uint fnc_num, u32 addr, u32 data,
274 int *err)
276 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
277 SDIOH_API_RC status;
279 if (!bcmsdh)
280 bcmsdh = l_bcmsdh;
282 ASSERT(bcmsdh->init_success);
284 status =
285 sdioh_request_word(bcmsdh->sdioh, SDIOH_CMD_TYPE_NORMAL,
286 SDIOH_WRITE, fnc_num, addr, &data, 4);
288 if (err)
289 *err = (SDIOH_API_SUCCESS(status) ? 0 : BCME_SDIO_ERROR);
291 BCMSDH_INFO(("%s:fun = %d, addr = 0x%x, u32data = 0x%x\n",
292 __func__, fnc_num, addr, data));
295 int bcmsdh_cis_read(void *sdh, uint func, u8 * cis, uint length)
297 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
298 SDIOH_API_RC status;
300 u8 *tmp_buf, *tmp_ptr;
301 u8 *ptr;
302 bool ascii = func & ~0xf;
303 func &= 0x7;
305 if (!bcmsdh)
306 bcmsdh = l_bcmsdh;
308 ASSERT(bcmsdh->init_success);
309 ASSERT(cis);
310 ASSERT(length <= SBSDIO_CIS_SIZE_LIMIT);
312 status = sdioh_cis_read(bcmsdh->sdioh, func, cis, length);
314 if (ascii) {
315 /* Move binary bits to tmp and format them
316 into the provided buffer. */
317 tmp_buf = kmalloc(length, GFP_ATOMIC);
318 if (tmp_buf == NULL) {
319 BCMSDH_ERROR(("%s: out of memory\n", __func__));
320 return BCME_NOMEM;
322 memcpy(tmp_buf, cis, length);
323 for (tmp_ptr = tmp_buf, ptr = cis; ptr < (cis + length - 4);
324 tmp_ptr++) {
325 ptr += sprintf((char *)ptr, "%.2x ", *tmp_ptr & 0xff);
326 if ((((tmp_ptr - tmp_buf) + 1) & 0xf) == 0)
327 ptr += sprintf((char *)ptr, "\n");
329 kfree(tmp_buf);
332 return SDIOH_API_SUCCESS(status) ? 0 : BCME_ERROR;
335 static int bcmsdhsdio_set_sbaddr_window(void *sdh, u32 address)
337 int err = 0;
338 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
339 bcmsdh_cfg_write(bcmsdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRLOW,
340 (address >> 8) & SBSDIO_SBADDRLOW_MASK, &err);
341 if (!err)
342 bcmsdh_cfg_write(bcmsdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRMID,
343 (address >> 16) & SBSDIO_SBADDRMID_MASK, &err);
344 if (!err)
345 bcmsdh_cfg_write(bcmsdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRHIGH,
346 (address >> 24) & SBSDIO_SBADDRHIGH_MASK,
347 &err);
349 return err;
352 u32 bcmsdh_reg_read(void *sdh, u32 addr, uint size)
354 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
355 SDIOH_API_RC status;
356 u32 word = 0;
357 uint bar0 = addr & ~SBSDIO_SB_OFT_ADDR_MASK;
359 BCMSDH_INFO(("%s:fun = 1, addr = 0x%x, ", __func__, addr));
361 if (!bcmsdh)
362 bcmsdh = l_bcmsdh;
364 ASSERT(bcmsdh->init_success);
366 if (bar0 != bcmsdh->sbwad) {
367 if (bcmsdhsdio_set_sbaddr_window(bcmsdh, bar0))
368 return 0xFFFFFFFF;
370 bcmsdh->sbwad = bar0;
373 addr &= SBSDIO_SB_OFT_ADDR_MASK;
374 if (size == 4)
375 addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
377 status = sdioh_request_word(bcmsdh->sdioh, SDIOH_CMD_TYPE_NORMAL,
378 SDIOH_READ, SDIO_FUNC_1, addr, &word, size);
380 bcmsdh->regfail = !(SDIOH_API_SUCCESS(status));
382 BCMSDH_INFO(("u32data = 0x%x\n", word));
384 /* if ok, return appropriately masked word */
385 if (SDIOH_API_SUCCESS(status)) {
386 switch (size) {
387 case sizeof(u8):
388 return word & 0xff;
389 case sizeof(u16):
390 return word & 0xffff;
391 case sizeof(u32):
392 return word;
393 default:
394 bcmsdh->regfail = true;
399 /* otherwise, bad sdio access or invalid size */
400 BCMSDH_ERROR(("%s: error reading addr 0x%04x size %d\n", __func__,
401 addr, size));
402 return 0xFFFFFFFF;
405 u32 bcmsdh_reg_write(void *sdh, u32 addr, uint size, u32 data)
407 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
408 SDIOH_API_RC status;
409 uint bar0 = addr & ~SBSDIO_SB_OFT_ADDR_MASK;
410 int err = 0;
412 BCMSDH_INFO(("%s:fun = 1, addr = 0x%x, uint%ddata = 0x%x\n",
413 __func__, addr, size * 8, data));
415 if (!bcmsdh)
416 bcmsdh = l_bcmsdh;
418 ASSERT(bcmsdh->init_success);
420 if (bar0 != bcmsdh->sbwad) {
421 err = bcmsdhsdio_set_sbaddr_window(bcmsdh, bar0);
422 if (err)
423 return err;
425 bcmsdh->sbwad = bar0;
428 addr &= SBSDIO_SB_OFT_ADDR_MASK;
429 if (size == 4)
430 addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
431 status =
432 sdioh_request_word(bcmsdh->sdioh, SDIOH_CMD_TYPE_NORMAL,
433 SDIOH_WRITE, SDIO_FUNC_1, addr, &data, size);
434 bcmsdh->regfail = !(SDIOH_API_SUCCESS(status));
436 if (SDIOH_API_SUCCESS(status))
437 return 0;
439 BCMSDH_ERROR(("%s: error writing 0x%08x to addr 0x%04x size %d\n",
440 __func__, data, addr, size));
441 return 0xFFFFFFFF;
444 bool bcmsdh_regfail(void *sdh)
446 return ((bcmsdh_info_t *) sdh)->regfail;
450 bcmsdh_recv_buf(void *sdh, u32 addr, uint fn, uint flags,
451 u8 *buf, uint nbytes, struct sk_buff *pkt,
452 bcmsdh_cmplt_fn_t complete, void *handle)
454 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
455 SDIOH_API_RC status;
456 uint incr_fix;
457 uint width;
458 uint bar0 = addr & ~SBSDIO_SB_OFT_ADDR_MASK;
459 int err = 0;
461 ASSERT(bcmsdh);
462 ASSERT(bcmsdh->init_success);
464 BCMSDH_INFO(("%s:fun = %d, addr = 0x%x, size = %d\n",
465 __func__, fn, addr, nbytes));
467 /* Async not implemented yet */
468 ASSERT(!(flags & SDIO_REQ_ASYNC));
469 if (flags & SDIO_REQ_ASYNC)
470 return BCME_UNSUPPORTED;
472 if (bar0 != bcmsdh->sbwad) {
473 err = bcmsdhsdio_set_sbaddr_window(bcmsdh, bar0);
474 if (err)
475 return err;
477 bcmsdh->sbwad = bar0;
480 addr &= SBSDIO_SB_OFT_ADDR_MASK;
482 incr_fix = (flags & SDIO_REQ_FIXED) ? SDIOH_DATA_FIX : SDIOH_DATA_INC;
483 width = (flags & SDIO_REQ_4BYTE) ? 4 : 2;
484 if (width == 4)
485 addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
487 status = sdioh_request_buffer(bcmsdh->sdioh, SDIOH_DATA_PIO, incr_fix,
488 SDIOH_READ, fn, addr, width, nbytes, buf,
489 pkt);
491 return SDIOH_API_SUCCESS(status) ? 0 : BCME_SDIO_ERROR;
495 bcmsdh_send_buf(void *sdh, u32 addr, uint fn, uint flags,
496 u8 *buf, uint nbytes, void *pkt,
497 bcmsdh_cmplt_fn_t complete, void *handle)
499 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
500 SDIOH_API_RC status;
501 uint incr_fix;
502 uint width;
503 uint bar0 = addr & ~SBSDIO_SB_OFT_ADDR_MASK;
504 int err = 0;
506 ASSERT(bcmsdh);
507 ASSERT(bcmsdh->init_success);
509 BCMSDH_INFO(("%s:fun = %d, addr = 0x%x, size = %d\n",
510 __func__, fn, addr, nbytes));
512 /* Async not implemented yet */
513 ASSERT(!(flags & SDIO_REQ_ASYNC));
514 if (flags & SDIO_REQ_ASYNC)
515 return BCME_UNSUPPORTED;
517 if (bar0 != bcmsdh->sbwad) {
518 err = bcmsdhsdio_set_sbaddr_window(bcmsdh, bar0);
519 if (err)
520 return err;
522 bcmsdh->sbwad = bar0;
525 addr &= SBSDIO_SB_OFT_ADDR_MASK;
527 incr_fix = (flags & SDIO_REQ_FIXED) ? SDIOH_DATA_FIX : SDIOH_DATA_INC;
528 width = (flags & SDIO_REQ_4BYTE) ? 4 : 2;
529 if (width == 4)
530 addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
532 status = sdioh_request_buffer(bcmsdh->sdioh, SDIOH_DATA_PIO, incr_fix,
533 SDIOH_WRITE, fn, addr, width, nbytes, buf,
534 pkt);
536 return SDIOH_API_SUCCESS(status) ? 0 : BCME_ERROR;
539 int bcmsdh_rwdata(void *sdh, uint rw, u32 addr, u8 *buf, uint nbytes)
541 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
542 SDIOH_API_RC status;
544 ASSERT(bcmsdh);
545 ASSERT(bcmsdh->init_success);
546 ASSERT((addr & SBSDIO_SBWINDOW_MASK) == 0);
548 addr &= SBSDIO_SB_OFT_ADDR_MASK;
549 addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
551 status =
552 sdioh_request_buffer(bcmsdh->sdioh, SDIOH_DATA_PIO, SDIOH_DATA_INC,
553 (rw ? SDIOH_WRITE : SDIOH_READ), SDIO_FUNC_1,
554 addr, 4, nbytes, buf, NULL);
556 return SDIOH_API_SUCCESS(status) ? 0 : BCME_ERROR;
559 int bcmsdh_abort(void *sdh, uint fn)
561 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
563 return sdioh_abort(bcmsdh->sdioh, fn);
566 int bcmsdh_start(void *sdh, int stage)
568 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
570 return sdioh_start(bcmsdh->sdioh, stage);
573 int bcmsdh_stop(void *sdh)
575 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
577 return sdioh_stop(bcmsdh->sdioh);
580 int bcmsdh_query_device(void *sdh)
582 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
583 bcmsdh->vendevid = (PCI_VENDOR_ID_BROADCOM << 16) | 0;
584 return bcmsdh->vendevid;
587 uint bcmsdh_query_iofnum(void *sdh)
589 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
591 if (!bcmsdh)
592 bcmsdh = l_bcmsdh;
594 return sdioh_query_iofnum(bcmsdh->sdioh);
597 int bcmsdh_reset(bcmsdh_info_t *sdh)
599 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
601 return sdioh_sdio_reset(bcmsdh->sdioh);
604 void *bcmsdh_get_sdioh(bcmsdh_info_t *sdh)
606 ASSERT(sdh);
607 return sdh->sdioh;
610 /* Function to pass device-status bits to DHD. */
611 u32 bcmsdh_get_dstatus(void *sdh)
613 return 0;
616 u32 bcmsdh_cur_sbwad(void *sdh)
618 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
620 if (!bcmsdh)
621 bcmsdh = l_bcmsdh;
623 return bcmsdh->sbwad;
626 void bcmsdh_chipinfo(void *sdh, u32 chip, u32 chiprev)
628 return;