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 <linux/sched.h>
24 #include <brcm_hw_ids.h>
25 #include <brcmu_utils.h>
26 #include <brcmu_wifi.h>
28 #include "bcmsdbus.h" /* common SDIO/controller interface */
29 #include "sbsdio.h" /* BRCM sdio device core */
30 #include "dngl_stats.h"
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 brcmf_sdioh_enable_hw_oob_intr(void *sdioh, bool enable);
50 void brcmf_sdcard_enable_hw_oob_intr(bcmsdh_info_t *sdh, bool enable)
52 brcmf_sdioh_enable_hw_oob_intr(sdh->sdioh, enable);
56 bcmsdh_info_t *brcmf_sdcard_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 = brcmf_sdioh_attach(cfghdl, irq);
71 brcmf_sdcard_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 brcmf_sdcard_detach(void *sdh)
86 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
90 brcmf_sdioh_detach(bcmsdh->sdioh);
101 brcmf_sdcard_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 brcmf_sdioh_iovar_op(bcmsdh->sdioh, name, params, plen, arg,
109 bool brcmf_sdcard_intr_query(void *sdh)
111 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
116 status = brcmf_sdioh_interrupt_query(bcmsdh->sdioh, &on);
117 if (SDIOH_API_SUCCESS(status))
123 int brcmf_sdcard_intr_enable(void *sdh)
125 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
129 status = brcmf_sdioh_interrupt_set(bcmsdh->sdioh, true);
130 return SDIOH_API_SUCCESS(status) ? 0 : -EIO;
133 int brcmf_sdcard_intr_disable(void *sdh)
135 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
139 status = brcmf_sdioh_interrupt_set(bcmsdh->sdioh, false);
140 return SDIOH_API_SUCCESS(status) ? 0 : -EIO;
143 int brcmf_sdcard_intr_reg(void *sdh, bcmsdh_cb_fn_t fn, void *argh)
145 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
149 status = brcmf_sdioh_interrupt_register(bcmsdh->sdioh, fn, argh);
150 return SDIOH_API_SUCCESS(status) ? 0 : -EIO;
153 int brcmf_sdcard_intr_dereg(void *sdh)
155 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
159 status = brcmf_sdioh_interrupt_deregister(bcmsdh->sdioh);
160 return SDIOH_API_SUCCESS(status) ? 0 : -EIO;
163 #if defined(DHD_DEBUG)
164 bool brcmf_sdcard_intr_pending(void *sdh)
166 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
169 return brcmf_sdioh_interrupt_pending(bcmsdh->sdioh);
173 int brcmf_sdcard_devremove_reg(void *sdh, bcmsdh_cb_fn_t fn, void *argh)
177 /* don't support yet */
181 u8 brcmf_sdcard_cfg_read(void *sdh, uint fnc_num, u32 addr, int *err)
183 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
185 #ifdef SDIOH_API_ACCESS_RETRY_LIMIT
193 ASSERT(bcmsdh->init_success);
195 #ifdef SDIOH_API_ACCESS_RETRY_LIMIT
197 if (retry) /* wait for 1 ms till bus get settled down */
201 brcmf_sdioh_cfg_read(bcmsdh->sdioh, fnc_num, addr,
203 #ifdef SDIOH_API_ACCESS_RETRY_LIMIT
204 } while (!SDIOH_API_SUCCESS(status)
205 && (retry++ < SDIOH_API_ACCESS_RETRY_LIMIT));
208 *err = (SDIOH_API_SUCCESS(status) ? 0 : -EIO);
210 BCMSDH_INFO(("%s:fun = %d, addr = 0x%x, u8data = 0x%x\n",
211 __func__, fnc_num, addr, data));
217 brcmf_sdcard_cfg_write(void *sdh, uint fnc_num, u32 addr, u8 data, int *err)
219 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
221 #ifdef SDIOH_API_ACCESS_RETRY_LIMIT
228 ASSERT(bcmsdh->init_success);
230 #ifdef SDIOH_API_ACCESS_RETRY_LIMIT
232 if (retry) /* wait for 1 ms till bus get settled down */
236 brcmf_sdioh_cfg_write(bcmsdh->sdioh, fnc_num, addr,
238 #ifdef SDIOH_API_ACCESS_RETRY_LIMIT
239 } while (!SDIOH_API_SUCCESS(status)
240 && (retry++ < SDIOH_API_ACCESS_RETRY_LIMIT));
243 *err = SDIOH_API_SUCCESS(status) ? 0 : -EIO;
245 BCMSDH_INFO(("%s:fun = %d, addr = 0x%x, u8data = 0x%x\n",
246 __func__, fnc_num, addr, data));
249 u32 brcmf_sdcard_cfg_read_word(void *sdh, uint fnc_num, u32 addr, int *err)
251 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
258 ASSERT(bcmsdh->init_success);
260 status = brcmf_sdioh_request_word(bcmsdh->sdioh, SDIOH_CMD_TYPE_NORMAL,
261 SDIOH_READ, fnc_num, addr, &data, 4);
264 *err = (SDIOH_API_SUCCESS(status) ? 0 : -EIO);
266 BCMSDH_INFO(("%s:fun = %d, addr = 0x%x, u32data = 0x%x\n",
267 __func__, fnc_num, addr, data));
273 brcmf_sdcard_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 brcmf_sdioh_request_word(bcmsdh->sdioh, SDIOH_CMD_TYPE_NORMAL,
286 SDIOH_WRITE, fnc_num, addr, &data, 4);
289 *err = (SDIOH_API_SUCCESS(status) ? 0 : -EIO);
291 BCMSDH_INFO(("%s:fun = %d, addr = 0x%x, u32data = 0x%x\n",
292 __func__, fnc_num, addr, data));
295 int brcmf_sdcard_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 = brcmf_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 : -EIO;
335 static int brcmf_sdcard_set_sbaddr_window(void *sdh, u32 address)
338 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
339 brcmf_sdcard_cfg_write(bcmsdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRLOW,
340 (address >> 8) & SBSDIO_SBADDRLOW_MASK, &err);
342 brcmf_sdcard_cfg_write(bcmsdh, SDIO_FUNC_1,
343 SBSDIO_FUNC1_SBADDRMID,
344 (address >> 16) & SBSDIO_SBADDRMID_MASK,
347 brcmf_sdcard_cfg_write(bcmsdh, SDIO_FUNC_1,
348 SBSDIO_FUNC1_SBADDRHIGH,
349 (address >> 24) & SBSDIO_SBADDRHIGH_MASK,
355 u32 brcmf_sdcard_reg_read(void *sdh, u32 addr, uint size)
357 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
360 uint bar0 = addr & ~SBSDIO_SB_OFT_ADDR_MASK;
362 BCMSDH_INFO(("%s:fun = 1, addr = 0x%x, ", __func__, addr));
367 ASSERT(bcmsdh->init_success);
369 if (bar0 != bcmsdh->sbwad) {
370 if (brcmf_sdcard_set_sbaddr_window(bcmsdh, bar0))
373 bcmsdh->sbwad = bar0;
376 addr &= SBSDIO_SB_OFT_ADDR_MASK;
378 addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
380 status = brcmf_sdioh_request_word(bcmsdh->sdioh, SDIOH_CMD_TYPE_NORMAL,
381 SDIOH_READ, SDIO_FUNC_1, addr, &word, size);
383 bcmsdh->regfail = !(SDIOH_API_SUCCESS(status));
385 BCMSDH_INFO(("u32data = 0x%x\n", word));
387 /* if ok, return appropriately masked word */
388 if (SDIOH_API_SUCCESS(status)) {
393 return word & 0xffff;
397 bcmsdh->regfail = true;
402 /* otherwise, bad sdio access or invalid size */
403 BCMSDH_ERROR(("%s: error reading addr 0x%04x size %d\n", __func__,
408 u32 brcmf_sdcard_reg_write(void *sdh, u32 addr, uint size, u32 data)
410 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
412 uint bar0 = addr & ~SBSDIO_SB_OFT_ADDR_MASK;
415 BCMSDH_INFO(("%s:fun = 1, addr = 0x%x, uint%ddata = 0x%x\n",
416 __func__, addr, size * 8, data));
421 ASSERT(bcmsdh->init_success);
423 if (bar0 != bcmsdh->sbwad) {
424 err = brcmf_sdcard_set_sbaddr_window(bcmsdh, bar0);
428 bcmsdh->sbwad = bar0;
431 addr &= SBSDIO_SB_OFT_ADDR_MASK;
433 addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
435 brcmf_sdioh_request_word(bcmsdh->sdioh, SDIOH_CMD_TYPE_NORMAL,
436 SDIOH_WRITE, SDIO_FUNC_1, addr, &data, size);
437 bcmsdh->regfail = !(SDIOH_API_SUCCESS(status));
439 if (SDIOH_API_SUCCESS(status))
442 BCMSDH_ERROR(("%s: error writing 0x%08x to addr 0x%04x size %d\n",
443 __func__, data, addr, size));
447 bool brcmf_sdcard_regfail(void *sdh)
449 return ((bcmsdh_info_t *) sdh)->regfail;
453 brcmf_sdcard_recv_buf(bcmsdh_info_t *bcmsdh, u32 addr, uint fn, uint flags,
454 u8 *buf, uint nbytes, struct sk_buff *pkt,
455 bcmsdh_cmplt_fn_t complete, void *handle)
460 uint bar0 = addr & ~SBSDIO_SB_OFT_ADDR_MASK;
464 ASSERT(bcmsdh->init_success);
466 BCMSDH_INFO(("%s:fun = %d, addr = 0x%x, size = %d\n",
467 __func__, fn, addr, nbytes));
469 /* Async not implemented yet */
470 ASSERT(!(flags & SDIO_REQ_ASYNC));
471 if (flags & SDIO_REQ_ASYNC)
474 if (bar0 != bcmsdh->sbwad) {
475 err = brcmf_sdcard_set_sbaddr_window(bcmsdh, bar0);
479 bcmsdh->sbwad = bar0;
482 addr &= SBSDIO_SB_OFT_ADDR_MASK;
484 incr_fix = (flags & SDIO_REQ_FIXED) ? SDIOH_DATA_FIX : SDIOH_DATA_INC;
485 width = (flags & SDIO_REQ_4BYTE) ? 4 : 2;
487 addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
489 status = brcmf_sdioh_request_buffer(bcmsdh->sdioh, SDIOH_DATA_PIO,
490 incr_fix, SDIOH_READ, fn, addr, width, nbytes, buf, pkt);
492 return SDIOH_API_SUCCESS(status) ? 0 : -EIO;
496 brcmf_sdcard_send_buf(void *sdh, u32 addr, uint fn, uint flags,
497 u8 *buf, uint nbytes, void *pkt,
498 bcmsdh_cmplt_fn_t complete, void *handle)
500 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
504 uint bar0 = addr & ~SBSDIO_SB_OFT_ADDR_MASK;
508 ASSERT(bcmsdh->init_success);
510 BCMSDH_INFO(("%s:fun = %d, addr = 0x%x, size = %d\n",
511 __func__, fn, addr, nbytes));
513 /* Async not implemented yet */
514 ASSERT(!(flags & SDIO_REQ_ASYNC));
515 if (flags & SDIO_REQ_ASYNC)
518 if (bar0 != bcmsdh->sbwad) {
519 err = brcmf_sdcard_set_sbaddr_window(bcmsdh, bar0);
523 bcmsdh->sbwad = bar0;
526 addr &= SBSDIO_SB_OFT_ADDR_MASK;
528 incr_fix = (flags & SDIO_REQ_FIXED) ? SDIOH_DATA_FIX : SDIOH_DATA_INC;
529 width = (flags & SDIO_REQ_4BYTE) ? 4 : 2;
531 addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
533 status = brcmf_sdioh_request_buffer(bcmsdh->sdioh, SDIOH_DATA_PIO,
534 incr_fix, SDIOH_WRITE, fn, addr, width, nbytes, buf, pkt);
536 return SDIOH_API_SUCCESS(status) ? 0 : -EIO;
539 int brcmf_sdcard_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;
551 status = brcmf_sdioh_request_buffer(bcmsdh->sdioh, SDIOH_DATA_PIO,
552 SDIOH_DATA_INC, (rw ? SDIOH_WRITE : SDIOH_READ), SDIO_FUNC_1,
553 addr, 4, nbytes, buf, NULL);
555 return SDIOH_API_SUCCESS(status) ? 0 : -EIO;
558 int brcmf_sdcard_abort(void *sdh, uint fn)
560 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
562 return brcmf_sdioh_abort(bcmsdh->sdioh, fn);
565 int brcmf_sdcard_start(void *sdh, int stage)
567 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
569 return brcmf_sdioh_start(bcmsdh->sdioh, stage);
572 int brcmf_sdcard_stop(void *sdh)
574 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
576 return brcmf_sdioh_stop(bcmsdh->sdioh);
579 int brcmf_sdcard_query_device(void *sdh)
581 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
582 bcmsdh->vendevid = (PCI_VENDOR_ID_BROADCOM << 16) | 0;
583 return bcmsdh->vendevid;
586 uint brcmf_sdcard_query_iofnum(void *sdh)
588 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
593 return brcmf_sdioh_query_iofnum(bcmsdh->sdioh);
596 int brcmf_sdcard_reset(bcmsdh_info_t *sdh)
598 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
600 return brcmf_sdioh_reset(bcmsdh->sdioh);
603 void *brcmf_sdcard_get_sdioh(bcmsdh_info_t *sdh)
609 /* Function to pass device-status bits to DHD. */
610 u32 brcmf_sdcard_get_dstatus(void *sdh)
615 u32 brcmf_sdcard_cur_sbwad(void *sdh)
617 bcmsdh_info_t *bcmsdh = (bcmsdh_info_t *) sdh;
622 return bcmsdh->sbwad;
625 void brcmf_sdcard_chipinfo(void *sdh, u32 chip, u32 chiprev)