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>
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
;
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
);
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
);
62 BCMSDH_ERROR(("bcmsdh_attach: out of memory"));
66 /* save the handler locally */
69 bcmsdh
->sdioh
= sdioh_attach(cfghdl
, irq
);
71 bcmsdh_detach(bcmsdh
);
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
;
84 int bcmsdh_detach(void *sdh
)
86 bcmsdh_info_t
*bcmsdh
= (bcmsdh_info_t
*) sdh
;
90 sdioh_detach(bcmsdh
->sdioh
);
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
;
115 status
= sdioh_interrupt_query(bcmsdh
->sdioh
, &on
);
116 if (SDIOH_API_SUCCESS(status
))
122 int bcmsdh_intr_enable(void *sdh
)
124 bcmsdh_info_t
*bcmsdh
= (bcmsdh_info_t
*) sdh
;
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
;
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
;
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
;
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
;
168 return sdioh_interrupt_pending(bcmsdh
->sdioh
);
172 int bcmsdh_devremove_reg(void *sdh
, bcmsdh_cb_fn_t fn
, void *argh
)
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
;
184 #ifdef SDIOH_API_ACCESS_RETRY_LIMIT
192 ASSERT(bcmsdh
->init_success
);
194 #ifdef SDIOH_API_ACCESS_RETRY_LIMIT
196 if (retry
) /* wait for 1 ms till bus get settled down */
200 sdioh_cfg_read(bcmsdh
->sdioh
, fnc_num
, addr
,
202 #ifdef SDIOH_API_ACCESS_RETRY_LIMIT
203 } while (!SDIOH_API_SUCCESS(status
)
204 && (retry
++ < SDIOH_API_ACCESS_RETRY_LIMIT
));
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
));
216 bcmsdh_cfg_write(void *sdh
, uint fnc_num
, u32 addr
, u8 data
, int *err
)
218 bcmsdh_info_t
*bcmsdh
= (bcmsdh_info_t
*) sdh
;
220 #ifdef SDIOH_API_ACCESS_RETRY_LIMIT
227 ASSERT(bcmsdh
->init_success
);
229 #ifdef SDIOH_API_ACCESS_RETRY_LIMIT
231 if (retry
) /* wait for 1 ms till bus get settled down */
235 sdioh_cfg_write(bcmsdh
->sdioh
, fnc_num
, addr
,
237 #ifdef SDIOH_API_ACCESS_RETRY_LIMIT
238 } while (!SDIOH_API_SUCCESS(status
)
239 && (retry
++ < SDIOH_API_ACCESS_RETRY_LIMIT
));
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
;
257 ASSERT(bcmsdh
->init_success
);
260 sdioh_request_word(bcmsdh
->sdioh
, SDIOH_CMD_TYPE_NORMAL
, SDIOH_READ
,
261 fnc_num
, addr
, &data
, 4);
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
));
273 bcmsdh_cfg_write_word(void *sdh
, uint fnc_num
, u32 addr
, u32 data
,
276 bcmsdh_info_t
*bcmsdh
= (bcmsdh_info_t
*) sdh
;
282 ASSERT(bcmsdh
->init_success
);
285 sdioh_request_word(bcmsdh
->sdioh
, SDIOH_CMD_TYPE_NORMAL
,
286 SDIOH_WRITE
, fnc_num
, addr
, &data
, 4);
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
;
300 u8
*tmp_buf
, *tmp_ptr
;
302 bool ascii
= func
& ~0xf;
308 ASSERT(bcmsdh
->init_success
);
310 ASSERT(length
<= SBSDIO_CIS_SIZE_LIMIT
);
312 status
= sdioh_cis_read(bcmsdh
->sdioh
, func
, cis
, length
);
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__
));
322 memcpy(tmp_buf
, cis
, length
);
323 for (tmp_ptr
= tmp_buf
, ptr
= cis
; ptr
< (cis
+ length
- 4);
325 ptr
+= sprintf((char *)ptr
, "%.2x ", *tmp_ptr
& 0xff);
326 if ((((tmp_ptr
- tmp_buf
) + 1) & 0xf) == 0)
327 ptr
+= sprintf((char *)ptr
, "\n");
332 return SDIOH_API_SUCCESS(status
) ? 0 : BCME_ERROR
;
335 static int bcmsdhsdio_set_sbaddr_window(void *sdh
, u32 address
)
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
);
342 bcmsdh_cfg_write(bcmsdh
, SDIO_FUNC_1
, SBSDIO_FUNC1_SBADDRMID
,
343 (address
>> 16) & SBSDIO_SBADDRMID_MASK
, &err
);
345 bcmsdh_cfg_write(bcmsdh
, SDIO_FUNC_1
, SBSDIO_FUNC1_SBADDRHIGH
,
346 (address
>> 24) & SBSDIO_SBADDRHIGH_MASK
,
352 u32
bcmsdh_reg_read(void *sdh
, u32 addr
, uint size
)
354 bcmsdh_info_t
*bcmsdh
= (bcmsdh_info_t
*) sdh
;
357 uint bar0
= addr
& ~SBSDIO_SB_OFT_ADDR_MASK
;
359 BCMSDH_INFO(("%s:fun = 1, addr = 0x%x, ", __func__
, addr
));
364 ASSERT(bcmsdh
->init_success
);
366 if (bar0
!= bcmsdh
->sbwad
) {
367 if (bcmsdhsdio_set_sbaddr_window(bcmsdh
, bar0
))
370 bcmsdh
->sbwad
= bar0
;
373 addr
&= SBSDIO_SB_OFT_ADDR_MASK
;
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
)) {
390 return word
& 0xffff;
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__
,
405 u32
bcmsdh_reg_write(void *sdh
, u32 addr
, uint size
, u32 data
)
407 bcmsdh_info_t
*bcmsdh
= (bcmsdh_info_t
*) sdh
;
409 uint bar0
= addr
& ~SBSDIO_SB_OFT_ADDR_MASK
;
412 BCMSDH_INFO(("%s:fun = 1, addr = 0x%x, uint%ddata = 0x%x\n",
413 __func__
, addr
, size
* 8, data
));
418 ASSERT(bcmsdh
->init_success
);
420 if (bar0
!= bcmsdh
->sbwad
) {
421 err
= bcmsdhsdio_set_sbaddr_window(bcmsdh
, bar0
);
425 bcmsdh
->sbwad
= bar0
;
428 addr
&= SBSDIO_SB_OFT_ADDR_MASK
;
430 addr
|= SBSDIO_SB_ACCESS_2_4B_FLAG
;
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
))
439 BCMSDH_ERROR(("%s: error writing 0x%08x to addr 0x%04x size %d\n",
440 __func__
, data
, addr
, size
));
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
;
458 uint bar0
= addr
& ~SBSDIO_SB_OFT_ADDR_MASK
;
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
);
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;
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
,
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
;
503 uint bar0
= addr
& ~SBSDIO_SB_OFT_ADDR_MASK
;
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
);
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;
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
,
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
;
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
;
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
;
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
)
610 /* Function to pass device-status bits to DHD. */
611 u32
bcmsdh_get_dstatus(void *sdh
)
616 u32
bcmsdh_cur_sbwad(void *sdh
)
618 bcmsdh_info_t
*bcmsdh
= (bcmsdh_info_t
*) sdh
;
623 return bcmsdh
->sbwad
;
626 void bcmsdh_chipinfo(void *sdh
, u32 chip
, u32 chiprev
)