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 #include <linux/types.h>
17 #include <linux/netdevice.h>
18 #include <linux/mmc/sdio.h>
21 #include <brcmu_utils.h>
22 #include <brcmu_wifi.h>
23 #include <bcmsdbus.h> /* bcmsdh to/from specific controller APIs */
24 #include <sdiovar.h> /* ioctl/iovars */
26 #include <linux/mmc/core.h>
27 #include <linux/mmc/sdio_func.h>
28 #include <linux/mmc/sdio_ids.h>
29 #include <linux/suspend.h>
31 #include <dngl_stats.h>
34 #include "bcmsdh_sdmmc.h"
36 extern int sdio_function_init(void);
37 extern void sdio_function_cleanup(void);
39 #if !defined(OOB_INTR_ONLY)
40 static void IRQHandler(struct sdio_func *func);
41 static void IRQHandlerF2(struct sdio_func *func);
42 #endif /* !defined(OOB_INTR_ONLY) */
43 static int sdioh_sdmmc_get_cisaddr(sdioh_info_t *sd, u32 regaddr);
44 extern int sdio_reset_comm(struct mmc_card *card);
46 extern PBCMSDH_SDMMC_INSTANCE gInstance;
48 uint sd_f2_blocksize = 512; /* Default blocksize */
50 uint sd_msglevel = 0x01;
51 DHD_PM_RESUME_WAIT_INIT(sdioh_request_byte_wait);
52 DHD_PM_RESUME_WAIT_INIT(sdioh_request_word_wait);
53 DHD_PM_RESUME_WAIT_INIT(sdioh_request_packet_wait);
54 DHD_PM_RESUME_WAIT_INIT(sdioh_request_buffer_wait);
56 #define DMA_ALIGN_MASK 0x03
58 int sdioh_sdmmc_card_regread(sdioh_info_t *sd, int func, u32 regaddr,
59 int regsize, u32 *data);
61 void sdioh_sdio_set_host_pm_flags(int flag)
63 if (sdio_set_host_pm_flags(gInstance->func[1], flag))
64 printk(KERN_ERR "%s: Failed to set pm_flags 0x%08x\n",\
65 __func__, (unsigned int)flag);
68 static int sdioh_sdmmc_card_enablefuncs(sdioh_info_t *sd)
74 sd_trace(("%s\n", __func__));
76 /* Get the Card's common CIS address */
77 sd->com_cis_ptr = sdioh_sdmmc_get_cisaddr(sd, SDIO_CCCR_CIS);
78 sd->func_cis_ptr[0] = sd->com_cis_ptr;
79 sd_info(("%s: Card's Common CIS Ptr = 0x%x\n", __func__,
82 /* Get the Card's function CIS (for each function) */
83 for (fbraddr = SDIO_FBR_BASE(1), func = 1;
84 func <= sd->num_funcs; func++, fbraddr += SDIOD_FBR_SIZE) {
85 sd->func_cis_ptr[func] =
86 sdioh_sdmmc_get_cisaddr(sd, SDIO_FBR_CIS + fbraddr);
87 sd_info(("%s: Function %d CIS Ptr = 0x%x\n", __func__, func,
88 sd->func_cis_ptr[func]));
91 sd->func_cis_ptr[0] = sd->com_cis_ptr;
92 sd_info(("%s: Card's Common CIS Ptr = 0x%x\n", __func__,
95 /* Enable Function 1 */
96 sdio_claim_host(gInstance->func[1]);
97 err_ret = sdio_enable_func(gInstance->func[1]);
98 sdio_release_host(gInstance->func[1]);
100 sd_err(("bcmsdh_sdmmc: Failed to enable F1 Err: 0x%08x",
108 * Public entry points & extern's
110 sdioh_info_t *sdioh_attach(void *bar0, uint irq)
115 sd_trace(("%s\n", __func__));
117 if (gInstance == NULL) {
118 sd_err(("%s: SDIO Device not present\n", __func__));
122 sd = kzalloc(sizeof(sdioh_info_t), GFP_ATOMIC);
124 sd_err(("sdioh_attach: out of memory\n"));
127 if (sdioh_sdmmc_osinit(sd) != 0) {
128 sd_err(("%s:sdioh_sdmmc_osinit() failed\n", __func__));
134 sd->use_client_ints = true;
135 sd->client_block_size[0] = 64;
139 /* Claim host controller */
140 sdio_claim_host(gInstance->func[1]);
142 sd->client_block_size[1] = 64;
143 err_ret = sdio_set_block_size(gInstance->func[1], 64);
145 sd_err(("bcmsdh_sdmmc: Failed to set F1 blocksize\n"));
147 /* Release host controller F1 */
148 sdio_release_host(gInstance->func[1]);
150 if (gInstance->func[2]) {
151 /* Claim host controller F2 */
152 sdio_claim_host(gInstance->func[2]);
154 sd->client_block_size[2] = sd_f2_blocksize;
156 sdio_set_block_size(gInstance->func[2], sd_f2_blocksize);
158 sd_err(("bcmsdh_sdmmc: Failed to set F2 blocksize "
159 "to %d\n", sd_f2_blocksize));
161 /* Release host controller F2 */
162 sdio_release_host(gInstance->func[2]);
165 sdioh_sdmmc_card_enablefuncs(sd);
167 sd_trace(("%s: Done\n", __func__));
171 extern SDIOH_API_RC sdioh_detach(sdioh_info_t *sd)
173 sd_trace(("%s\n", __func__));
177 /* Disable Function 2 */
178 sdio_claim_host(gInstance->func[2]);
179 sdio_disable_func(gInstance->func[2]);
180 sdio_release_host(gInstance->func[2]);
182 /* Disable Function 1 */
183 sdio_claim_host(gInstance->func[1]);
184 sdio_disable_func(gInstance->func[1]);
185 sdio_release_host(gInstance->func[1]);
188 sdioh_sdmmc_osfree(sd);
192 return SDIOH_API_RC_SUCCESS;
195 #if defined(OOB_INTR_ONLY) && defined(HW_OOB)
197 extern SDIOH_API_RC sdioh_enable_func_intr(void)
202 if (gInstance->func[0]) {
203 sdio_claim_host(gInstance->func[0]);
205 reg = sdio_readb(gInstance->func[0], SDIOD_CCCR_INTEN, &err);
207 sd_err(("%s: error for read SDIO_CCCR_IENx : 0x%x\n",
209 sdio_release_host(gInstance->func[0]);
210 return SDIOH_API_RC_FAIL;
213 /* Enable F1 and F2 interrupts, set master enable */
215 (INTR_CTL_FUNC1_EN | INTR_CTL_FUNC2_EN |
218 sdio_writeb(gInstance->func[0], reg, SDIOD_CCCR_INTEN, &err);
219 sdio_release_host(gInstance->func[0]);
222 sd_err(("%s: error for write SDIO_CCCR_IENx : 0x%x\n",
224 return SDIOH_API_RC_FAIL;
228 return SDIOH_API_RC_SUCCESS;
231 extern SDIOH_API_RC sdioh_disable_func_intr(void)
236 if (gInstance->func[0]) {
237 sdio_claim_host(gInstance->func[0]);
238 reg = sdio_readb(gInstance->func[0], SDIOD_CCCR_INTEN, &err);
240 sd_err(("%s: error for read SDIO_CCCR_IENx : 0x%x\n",
242 sdio_release_host(gInstance->func[0]);
243 return SDIOH_API_RC_FAIL;
246 reg &= ~(INTR_CTL_FUNC1_EN | INTR_CTL_FUNC2_EN);
247 /* Disable master interrupt with the last function interrupt */
250 sdio_writeb(gInstance->func[0], reg, SDIOD_CCCR_INTEN, &err);
252 sdio_release_host(gInstance->func[0]);
254 sd_err(("%s: error for write SDIO_CCCR_IENx : 0x%x\n",
256 return SDIOH_API_RC_FAIL;
259 return SDIOH_API_RC_SUCCESS;
261 #endif /* defined(OOB_INTR_ONLY) && defined(HW_OOB) */
263 /* Configure callback to client when we receive client interrupt */
265 sdioh_interrupt_register(sdioh_info_t *sd, sdioh_cb_fn_t fn, void *argh)
267 sd_trace(("%s: Entering\n", __func__));
269 sd_err(("%s: interrupt handler is NULL, not registering\n",
271 return SDIOH_API_RC_FAIL;
273 #if !defined(OOB_INTR_ONLY)
274 sd->intr_handler = fn;
275 sd->intr_handler_arg = argh;
276 sd->intr_handler_valid = true;
278 /* register and unmask irq */
279 if (gInstance->func[2]) {
280 sdio_claim_host(gInstance->func[2]);
281 sdio_claim_irq(gInstance->func[2], IRQHandlerF2);
282 sdio_release_host(gInstance->func[2]);
285 if (gInstance->func[1]) {
286 sdio_claim_host(gInstance->func[1]);
287 sdio_claim_irq(gInstance->func[1], IRQHandler);
288 sdio_release_host(gInstance->func[1]);
290 #elif defined(HW_OOB)
291 sdioh_enable_func_intr();
292 #endif /* defined(OOB_INTR_ONLY) */
293 return SDIOH_API_RC_SUCCESS;
296 extern SDIOH_API_RC sdioh_interrupt_deregister(sdioh_info_t *sd)
298 sd_trace(("%s: Entering\n", __func__));
300 #if !defined(OOB_INTR_ONLY)
301 if (gInstance->func[1]) {
302 /* register and unmask irq */
303 sdio_claim_host(gInstance->func[1]);
304 sdio_release_irq(gInstance->func[1]);
305 sdio_release_host(gInstance->func[1]);
308 if (gInstance->func[2]) {
309 /* Claim host controller F2 */
310 sdio_claim_host(gInstance->func[2]);
311 sdio_release_irq(gInstance->func[2]);
312 /* Release host controller F2 */
313 sdio_release_host(gInstance->func[2]);
316 sd->intr_handler_valid = false;
317 sd->intr_handler = NULL;
318 sd->intr_handler_arg = NULL;
319 #elif defined(HW_OOB)
320 sdioh_disable_func_intr();
321 #endif /* !defined(OOB_INTR_ONLY) */
322 return SDIOH_API_RC_SUCCESS;
325 extern SDIOH_API_RC sdioh_interrupt_query(sdioh_info_t *sd, bool *onoff)
327 sd_trace(("%s: Entering\n", __func__));
328 *onoff = sd->client_intr_enabled;
329 return SDIOH_API_RC_SUCCESS;
332 #if defined(DHD_DEBUG)
333 extern bool sdioh_interrupt_pending(sdioh_info_t *sd)
339 uint sdioh_query_iofnum(sdioh_info_t *sd)
341 return sd->num_funcs;
355 const struct brcmu_iovar sdioh_iovars[] = {
356 {"sd_msglevel", IOV_MSGLEVEL, 0, IOVT_UINT32, 0},
357 {"sd_blocksize", IOV_BLOCKSIZE, 0, IOVT_UINT32, 0},/* ((fn << 16) |
359 {"sd_ints", IOV_USEINTS, 0, IOVT_BOOL, 0},
360 {"sd_numints", IOV_NUMINTS, 0, IOVT_UINT32, 0},
361 {"sd_devreg", IOV_DEVREG, 0, IOVT_BUFFER, sizeof(sdreg_t)}
363 {"sd_rxchain", IOV_RXCHAIN, 0, IOVT_BOOL, 0}
369 sdioh_iovar_op(sdioh_info_t *si, const char *name,
370 void *params, int plen, void *arg, int len, bool set)
372 const struct brcmu_iovar *vi = NULL;
382 /* Get must have return space; Set does not take qualifiers */
383 ASSERT(set || (arg && len));
384 ASSERT(!set || (!params && !plen));
386 sd_trace(("%s: Enter (%s %s)\n", __func__, (set ? "set" : "get"),
389 vi = brcmu_iovar_lookup(sdioh_iovars, name);
391 bcmerror = -ENOTSUPP;
395 bcmerror = brcmu_iovar_lencheck(vi, arg, len, set);
399 /* Set up params so get and set can share the convenience variables */
400 if (params == NULL) {
405 if (vi->type == IOVT_VOID)
407 else if (vi->type == IOVT_BUFFER)
410 val_size = sizeof(int);
412 if (plen >= (int)sizeof(int_val))
413 memcpy(&int_val, params, sizeof(int_val));
415 bool_val = (int_val != 0) ? true : false;
417 actionid = set ? IOV_SVAL(vi->varid) : IOV_GVAL(vi->varid);
419 case IOV_GVAL(IOV_MSGLEVEL):
420 int_val = (s32) sd_msglevel;
421 memcpy(arg, &int_val, val_size);
424 case IOV_SVAL(IOV_MSGLEVEL):
425 sd_msglevel = int_val;
428 case IOV_GVAL(IOV_BLOCKSIZE):
429 if ((u32) int_val > si->num_funcs) {
433 int_val = (s32) si->client_block_size[int_val];
434 memcpy(arg, &int_val, val_size);
437 case IOV_SVAL(IOV_BLOCKSIZE):
439 uint func = ((u32) int_val >> 16);
440 uint blksize = (u16) int_val;
443 if (func > si->num_funcs) {
453 maxsize = BLOCK_SIZE_4318;
456 maxsize = BLOCK_SIZE_4328;
461 if (blksize > maxsize) {
469 si->client_block_size[func] = blksize;
474 case IOV_GVAL(IOV_RXCHAIN):
476 memcpy(arg, &int_val, val_size);
479 case IOV_GVAL(IOV_USEINTS):
480 int_val = (s32) si->use_client_ints;
481 memcpy(arg, &int_val, val_size);
484 case IOV_SVAL(IOV_USEINTS):
485 si->use_client_ints = (bool) int_val;
486 if (si->use_client_ints)
487 si->intmask |= CLIENT_INTR;
489 si->intmask &= ~CLIENT_INTR;
493 case IOV_GVAL(IOV_NUMINTS):
494 int_val = (s32) si->intrcount;
495 memcpy(arg, &int_val, val_size);
498 case IOV_GVAL(IOV_DEVREG):
500 sdreg_t *sd_ptr = (sdreg_t *) params;
504 (si, sd_ptr->func, sd_ptr->offset, &data)) {
510 memcpy(arg, &int_val, sizeof(int_val));
514 case IOV_SVAL(IOV_DEVREG):
516 sdreg_t *sd_ptr = (sdreg_t *) params;
517 u8 data = (u8) sd_ptr->value;
520 (si, sd_ptr->func, sd_ptr->offset, &data)) {
528 bcmerror = -ENOTSUPP;
536 #if defined(OOB_INTR_ONLY) && defined(HW_OOB)
538 SDIOH_API_RC sdioh_enable_hw_oob_intr(sdioh_info_t *sd, bool enable)
544 data = 3; /* enable hw oob interrupt */
546 data = 4; /* disable hw oob interrupt */
547 data |= 4; /* Active HIGH */
549 status = sdioh_request_byte(sd, SDIOH_WRITE, 0, 0xf2, &data);
552 #endif /* defined(OOB_INTR_ONLY) && defined(HW_OOB) */
555 sdioh_cfg_read(sdioh_info_t *sd, uint fnc_num, u32 addr, u8 *data)
558 /* No lock needed since sdioh_request_byte does locking */
559 status = sdioh_request_byte(sd, SDIOH_READ, fnc_num, addr, data);
564 sdioh_cfg_write(sdioh_info_t *sd, uint fnc_num, u32 addr, u8 *data)
566 /* No lock needed since sdioh_request_byte does locking */
568 status = sdioh_request_byte(sd, SDIOH_WRITE, fnc_num, addr, data);
572 static int sdioh_sdmmc_get_cisaddr(sdioh_info_t *sd, u32 regaddr)
574 /* read 24 bits and return valid 17 bit addr */
576 u32 scratch, regdata;
577 u8 *ptr = (u8 *)&scratch;
578 for (i = 0; i < 3; i++) {
579 if ((sdioh_sdmmc_card_regread(sd, 0, regaddr, 1, ®data)) !=
581 sd_err(("%s: Can't read!\n", __func__));
583 *ptr++ = (u8) regdata;
587 /* Only the lower 17-bits are valid */
588 scratch = le32_to_cpu(scratch);
589 scratch &= 0x0001FFFF;
594 sdioh_cis_read(sdioh_info_t *sd, uint func, u8 *cisd, u32 length)
601 sd_trace(("%s: Func = %d\n", __func__, func));
603 if (!sd->func_cis_ptr[func]) {
604 memset(cis, 0, length);
605 sd_err(("%s: no func_cis_ptr[%d]\n", __func__, func));
606 return SDIOH_API_RC_FAIL;
609 sd_err(("%s: func_cis_ptr[%d]=0x%04x\n", __func__, func,
610 sd->func_cis_ptr[func]));
612 for (count = 0; count < length; count++) {
613 offset = sd->func_cis_ptr[func] + count;
614 if (sdioh_sdmmc_card_regread(sd, 0, offset, 1, &foo) < 0) {
615 sd_err(("%s: regread failed: Can't read CIS\n",
617 return SDIOH_API_RC_FAIL;
620 *cis = (u8) (foo & 0xff);
624 return SDIOH_API_RC_SUCCESS;
628 sdioh_request_byte(sdioh_info_t *sd, uint rw, uint func, uint regaddr,
633 sd_info(("%s: rw=%d, func=%d, addr=0x%05x\n", __func__, rw, func,
636 DHD_PM_RESUME_WAIT(sdioh_request_byte_wait);
637 DHD_PM_RESUME_RETURN_ERROR(SDIOH_API_RC_FAIL);
638 if (rw) { /* CMD52 Write */
640 /* Can only directly write to some F0 registers.
644 if (regaddr == SDIO_CCCR_IOEx) {
645 if (gInstance->func[2]) {
646 sdio_claim_host(gInstance->func[2]);
647 if (*byte & SDIO_FUNC_ENABLE_2) {
648 /* Enable Function 2 */
651 (gInstance->func[2]);
653 sd_err(("bcmsdh_sdmmc: enable F2 failed:%d",
656 /* Disable Function 2 */
659 (gInstance->func[2]);
661 sd_err(("bcmsdh_sdmmc: Disab F2 failed:%d",
664 sdio_release_host(gInstance->func[2]);
667 #if defined(MMC_SDIO_ABORT)
668 /* to allow abort command through F1 */
669 else if (regaddr == SDIO_CCCR_ABORT) {
670 sdio_claim_host(gInstance->func[func]);
672 * this sdio_f0_writeb() can be replaced
674 * depending upon MMC driver change.
675 * As of this time, this is temporaray one
677 sdio_writeb(gInstance->func[func], *byte,
679 sdio_release_host(gInstance->func[func]);
681 #endif /* MMC_SDIO_ABORT */
682 else if (regaddr < 0xF0) {
683 sd_err(("bcmsdh_sdmmc: F0 Wr:0x%02x: write "
684 "disallowed\n", regaddr));
686 /* Claim host controller, perform F0 write,
688 sdio_claim_host(gInstance->func[func]);
689 sdio_f0_writeb(gInstance->func[func], *byte,
691 sdio_release_host(gInstance->func[func]);
694 /* Claim host controller, perform Fn write,
696 sdio_claim_host(gInstance->func[func]);
697 sdio_writeb(gInstance->func[func], *byte, regaddr,
699 sdio_release_host(gInstance->func[func]);
701 } else { /* CMD52 Read */
702 /* Claim host controller, perform Fn read, and release */
703 sdio_claim_host(gInstance->func[func]);
707 sdio_f0_readb(gInstance->func[func], regaddr,
711 sdio_readb(gInstance->func[func], regaddr,
715 sdio_release_host(gInstance->func[func]);
719 sd_err(("bcmsdh_sdmmc: Failed to %s byte F%d:@0x%05x=%02x, "
720 "Err: %d\n", rw ? "Write" : "Read", func, regaddr,
723 return ((err_ret == 0) ? SDIOH_API_RC_SUCCESS : SDIOH_API_RC_FAIL);
727 sdioh_request_word(sdioh_info_t *sd, uint cmd_type, uint rw, uint func,
728 uint addr, u32 *word, uint nbytes)
730 int err_ret = SDIOH_API_RC_FAIL;
733 sd_err(("%s: Only CMD52 allowed to F0.\n", __func__));
734 return SDIOH_API_RC_FAIL;
737 sd_info(("%s: cmd_type=%d, rw=%d, func=%d, addr=0x%05x, nbytes=%d\n",
738 __func__, cmd_type, rw, func, addr, nbytes));
740 DHD_PM_RESUME_WAIT(sdioh_request_word_wait);
741 DHD_PM_RESUME_RETURN_ERROR(SDIOH_API_RC_FAIL);
742 /* Claim host controller */
743 sdio_claim_host(gInstance->func[func]);
745 if (rw) { /* CMD52 Write */
747 sdio_writel(gInstance->func[func], *word, addr,
749 } else if (nbytes == 2) {
750 sdio_writew(gInstance->func[func], (*word & 0xFFFF),
753 sd_err(("%s: Invalid nbytes: %d\n", __func__, nbytes));
755 } else { /* CMD52 Read */
758 sdio_readl(gInstance->func[func], addr, &err_ret);
759 } else if (nbytes == 2) {
761 sdio_readw(gInstance->func[func], addr,
764 sd_err(("%s: Invalid nbytes: %d\n", __func__, nbytes));
768 /* Release host controller */
769 sdio_release_host(gInstance->func[func]);
772 sd_err(("bcmsdh_sdmmc: Failed to %s word, Err: 0x%08x",
773 rw ? "Write" : "Read", err_ret));
776 return ((err_ret == 0) ? SDIOH_API_RC_SUCCESS : SDIOH_API_RC_FAIL);
780 sdioh_request_packet(sdioh_info_t *sd, uint fix_inc, uint write, uint func,
781 uint addr, struct sk_buff *pkt)
783 bool fifo = (fix_inc == SDIOH_DATA_FIX);
787 struct sk_buff *pnext;
789 sd_trace(("%s: Enter\n", __func__));
792 DHD_PM_RESUME_WAIT(sdioh_request_packet_wait);
793 DHD_PM_RESUME_RETURN_ERROR(SDIOH_API_RC_FAIL);
795 /* Claim host controller */
796 sdio_claim_host(gInstance->func[func]);
797 for (pnext = pkt; pnext; pnext = pnext->next) {
798 uint pkt_len = pnext->len;
800 pkt_len &= 0xFFFFFFFC;
802 #ifdef CONFIG_MMC_MSM7X00A
803 if ((pkt_len % 64) == 32) {
804 sd_trace(("%s: Rounding up TX packet +=32\n",
808 #endif /* CONFIG_MMC_MSM7X00A */
809 /* Make sure the packet is aligned properly.
810 * If it isn't, then this
811 * is the fault of sdioh_request_buffer() which
812 * is supposed to give
813 * us something we can work with.
815 ASSERT(((u32) (pkt->data) & DMA_ALIGN_MASK) == 0);
817 if ((write) && (!fifo)) {
818 err_ret = sdio_memcpy_toio(gInstance->func[func], addr,
819 ((u8 *) (pnext->data)),
822 err_ret = sdio_memcpy_toio(gInstance->func[func], addr,
823 ((u8 *) (pnext->data)),
826 err_ret = sdio_readsb(gInstance->func[func],
827 ((u8 *) (pnext->data)),
830 err_ret = sdio_memcpy_fromio(gInstance->func[func],
831 ((u8 *) (pnext->data)),
836 sd_err(("%s: %s FAILED %p[%d], addr=0x%05x, pkt_len=%d,"
837 "ERR=0x%08x\n", __func__,
838 (write) ? "TX" : "RX",
839 pnext, SGCount, addr, pkt_len, err_ret));
841 sd_trace(("%s: %s xfr'd %p[%d], addr=0x%05x, len=%d\n",
843 (write) ? "TX" : "RX",
844 pnext, SGCount, addr, pkt_len));
853 /* Release host controller */
854 sdio_release_host(gInstance->func[func]);
856 sd_trace(("%s: Exit\n", __func__));
857 return ((err_ret == 0) ? SDIOH_API_RC_SUCCESS : SDIOH_API_RC_FAIL);
861 * This function takes a buffer or packet, and fixes everything up
862 * so that in the end, a DMA-able packet is created.
864 * A buffer does not have an associated packet pointer,
865 * and may or may not be aligned.
866 * A packet may consist of a single packet, or a packet chain.
867 * If it is a packet chain, then all the packets in the chain
868 * must be properly aligned.
870 * If the packet data is not aligned, then there may only be
871 * one packet, and in this case, it is copied to a new
876 sdioh_request_buffer(sdioh_info_t *sd, uint pio_dma, uint fix_inc, uint write,
877 uint func, uint addr, uint reg_width, uint buflen_u,
878 u8 *buffer, struct sk_buff *pkt)
881 struct sk_buff *mypkt = NULL;
883 sd_trace(("%s: Enter\n", __func__));
885 DHD_PM_RESUME_WAIT(sdioh_request_buffer_wait);
886 DHD_PM_RESUME_RETURN_ERROR(SDIOH_API_RC_FAIL);
887 /* Case 1: we don't have a packet. */
889 sd_data(("%s: Creating new %s Packet, len=%d\n",
890 __func__, write ? "TX" : "RX", buflen_u));
891 mypkt = brcmu_pkt_buf_get_skb(buflen_u);
893 sd_err(("%s: brcmu_pkt_buf_get_skb failed: len %d\n",
894 __func__, buflen_u));
895 return SDIOH_API_RC_FAIL;
898 /* For a write, copy the buffer data into the packet. */
900 memcpy(mypkt->data, buffer, buflen_u);
903 sdioh_request_packet(sd, fix_inc, write, func, addr, mypkt);
905 /* For a read, copy the packet data back to the buffer. */
907 memcpy(buffer, mypkt->data, buflen_u);
909 brcmu_pkt_buf_free_skb(mypkt);
910 } else if (((u32) (pkt->data) & DMA_ALIGN_MASK) != 0) {
911 /* Case 2: We have a packet, but it is unaligned. */
913 /* In this case, we cannot have a chain. */
914 ASSERT(pkt->next == NULL);
916 sd_data(("%s: Creating aligned %s Packet, len=%d\n",
917 __func__, write ? "TX" : "RX", pkt->len));
918 mypkt = brcmu_pkt_buf_get_skb(pkt->len);
920 sd_err(("%s: brcmu_pkt_buf_get_skb failed: len %d\n",
921 __func__, pkt->len));
922 return SDIOH_API_RC_FAIL;
925 /* For a write, copy the buffer data into the packet. */
927 memcpy(mypkt->data, pkt->data, pkt->len);
930 sdioh_request_packet(sd, fix_inc, write, func, addr, mypkt);
932 /* For a read, copy the packet data back to the buffer. */
934 memcpy(pkt->data, mypkt->data, mypkt->len);
936 brcmu_pkt_buf_free_skb(mypkt);
937 } else { /* case 3: We have a packet and
939 sd_data(("%s: Aligned %s Packet, direct DMA\n",
940 __func__, write ? "Tx" : "Rx"));
942 sdioh_request_packet(sd, fix_inc, write, func, addr, pkt);
948 /* this function performs "abort" for both of host & device */
949 extern int sdioh_abort(sdioh_info_t *sd, uint func)
951 #if defined(MMC_SDIO_ABORT)
952 char t_func = (char)func;
953 #endif /* defined(MMC_SDIO_ABORT) */
954 sd_trace(("%s: Enter\n", __func__));
956 #if defined(MMC_SDIO_ABORT)
957 /* issue abort cmd52 command through F0 */
958 sdioh_request_byte(sd, SDIOH_WRITE, SDIO_FUNC_0, SDIO_CCCR_ABORT,
960 #endif /* defined(MMC_SDIO_ABORT) */
962 sd_trace(("%s: Exit\n", __func__));
963 return SDIOH_API_RC_SUCCESS;
966 /* Reset and re-initialize the device */
967 int sdioh_sdio_reset(sdioh_info_t *si)
969 sd_trace(("%s: Enter\n", __func__));
970 sd_trace(("%s: Exit\n", __func__));
971 return SDIOH_API_RC_SUCCESS;
974 /* Disable device interrupt */
975 void sdioh_sdmmc_devintr_off(sdioh_info_t *sd)
977 sd_trace(("%s: %d\n", __func__, sd->use_client_ints));
978 sd->intmask &= ~CLIENT_INTR;
981 /* Enable device interrupt */
982 void sdioh_sdmmc_devintr_on(sdioh_info_t *sd)
984 sd_trace(("%s: %d\n", __func__, sd->use_client_ints));
985 sd->intmask |= CLIENT_INTR;
988 /* Read client card reg */
990 sdioh_sdmmc_card_regread(sdioh_info_t *sd, int func, u32 regaddr,
991 int regsize, u32 *data)
994 if ((func == 0) || (regsize == 1)) {
997 sdioh_request_byte(sd, SDIOH_READ, func, regaddr, &temp);
1000 sd_data(("%s: byte read data=0x%02x\n", __func__, *data));
1002 sdioh_request_word(sd, 0, SDIOH_READ, func, regaddr, data,
1007 sd_data(("%s: word read data=0x%08x\n", __func__, *data));
1013 #if !defined(OOB_INTR_ONLY)
1014 /* bcmsdh_sdmmc interrupt handler */
1015 static void IRQHandler(struct sdio_func *func)
1019 sd_trace(("bcmsdh_sdmmc: ***IRQHandler\n"));
1023 sdio_release_host(gInstance->func[0]);
1025 if (sd->use_client_ints) {
1027 ASSERT(sd->intr_handler);
1028 ASSERT(sd->intr_handler_arg);
1029 (sd->intr_handler) (sd->intr_handler_arg);
1031 sd_err(("bcmsdh_sdmmc: ***IRQHandler\n"));
1033 sd_err(("%s: Not ready for intr: enabled %d, handler %p\n",
1034 __func__, sd->client_intr_enabled, sd->intr_handler));
1037 sdio_claim_host(gInstance->func[0]);
1040 /* bcmsdh_sdmmc interrupt handler for F2 (dummy handler) */
1041 static void IRQHandlerF2(struct sdio_func *func)
1045 sd_trace(("bcmsdh_sdmmc: ***IRQHandlerF2\n"));
1051 #endif /* !defined(OOB_INTR_ONLY) */
1054 /* Write client card reg */
1056 sdioh_sdmmc_card_regwrite(sdioh_info_t *sd, int func, u32 regaddr,
1057 int regsize, u32 data)
1060 if ((func == 0) || (regsize == 1)) {
1064 sdioh_request_byte(sd, SDIOH_READ, func, regaddr, &temp);
1065 sd_data(("%s: byte write data=0x%02x\n", __func__, data));
1070 sdioh_request_word(sd, 0, SDIOH_READ, func, regaddr, &data,
1073 sd_data(("%s: word write data=0x%08x\n", __func__, data));
1078 #endif /* NOTUSED */
1080 int sdioh_start(sdioh_info_t *si, int stage)
1085 int sdioh_stop(sdioh_info_t *si)