1 /* 2 * Copyright (c) 2010 Broadcom Corporation 3 * 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. 7 * 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. 15 */ 16 /* ****************** SDIO CARD Interface Functions **************************/ 17 18 #include <linux/types.h> 19 #include <linux/netdevice.h> 20 #include <linux/pci.h> 21 #include <linux/pci_ids.h> 22 #include <linux/sched.h> 23 #include <linux/completion.h> 24 #include <linux/scatterlist.h> 25 #include <linux/mmc/sdio.h> 26 #include <linux/mmc/core.h> 27 #include <linux/mmc/sdio_func.h> 28 #include <linux/mmc/card.h> 29 #include <linux/mmc/host.h> 30 #include <linux/pm_runtime.h> 31 #include <linux/suspend.h> 32 #include <linux/errno.h> 33 #include <linux/module.h> 34 #include <linux/acpi.h> 35 #include <net/cfg80211.h> 36 37 #include <defs.h> 38 #include <brcm_hw_ids.h> 39 #include <brcmu_utils.h> 40 #include <brcmu_wifi.h> 41 #include <chipcommon.h> 42 #include <soc.h> 43 #include "chip.h" 44 #include "bus.h" 45 #include "debug.h" 46 #include "sdio.h" 47 #include "core.h" 48 #include "common.h" 49 50 #define SDIOH_API_ACCESS_RETRY_LIMIT 2 51 52 #define DMA_ALIGN_MASK 0x03 53 54 #define SDIO_FUNC1_BLOCKSIZE 64 55 #define SDIO_FUNC2_BLOCKSIZE 512 56 /* Maximum milliseconds to wait for F2 to come up */ 57 #define SDIO_WAIT_F2RDY 3000 58 59 #define BRCMF_DEFAULT_RXGLOM_SIZE 32 /* max rx frames in glom chain */ 60 61 struct brcmf_sdiod_freezer { 62 atomic_t freezing; 63 atomic_t thread_count; 64 u32 frozen_count; 65 wait_queue_head_t thread_freeze; 66 struct completion resumed; 67 }; 68 69 static irqreturn_t brcmf_sdiod_oob_irqhandler(int irq, void *dev_id) 70 { 71 struct brcmf_bus *bus_if = dev_get_drvdata(dev_id); 72 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio; 73 74 brcmf_dbg(INTR, "OOB intr triggered\n"); 75 76 /* out-of-band interrupt is level-triggered which won't 77 * be cleared until dpc 78 */ 79 if (sdiodev->irq_en) { 80 disable_irq_nosync(irq); 81 sdiodev->irq_en = false; 82 } 83 84 brcmf_sdio_isr(sdiodev->bus); 85 86 return IRQ_HANDLED; 87 } 88 89 static void brcmf_sdiod_ib_irqhandler(struct sdio_func *func) 90 { 91 struct brcmf_bus *bus_if = dev_get_drvdata(&func->dev); 92 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio; 93 94 brcmf_dbg(INTR, "IB intr triggered\n"); 95 96 brcmf_sdio_isr(sdiodev->bus); 97 } 98 99 /* dummy handler for SDIO function 2 interrupt */ 100 static void brcmf_sdiod_dummy_irqhandler(struct sdio_func *func) 101 { 102 } 103 104 int brcmf_sdiod_intr_register(struct brcmf_sdio_dev *sdiodev) 105 { 106 struct brcmfmac_sdio_pd *pdata; 107 int ret = 0; 108 u8 data; 109 u32 addr, gpiocontrol; 110 unsigned long flags; 111 112 pdata = &sdiodev->settings->bus.sdio; 113 if (pdata->oob_irq_supported) { 114 brcmf_dbg(SDIO, "Enter, register OOB IRQ %d\n", 115 pdata->oob_irq_nr); 116 ret = request_irq(pdata->oob_irq_nr, brcmf_sdiod_oob_irqhandler, 117 pdata->oob_irq_flags, "brcmf_oob_intr", 118 &sdiodev->func[1]->dev); 119 if (ret != 0) { 120 brcmf_err("request_irq failed %d\n", ret); 121 return ret; 122 } 123 sdiodev->oob_irq_requested = true; 124 spin_lock_init(&sdiodev->irq_en_lock); 125 spin_lock_irqsave(&sdiodev->irq_en_lock, flags); 126 sdiodev->irq_en = true; 127 spin_unlock_irqrestore(&sdiodev->irq_en_lock, flags); 128 129 ret = enable_irq_wake(pdata->oob_irq_nr); 130 if (ret != 0) { 131 brcmf_err("enable_irq_wake failed %d\n", ret); 132 return ret; 133 } 134 sdiodev->irq_wake = true; 135 136 sdio_claim_host(sdiodev->func[1]); 137 138 if (sdiodev->bus_if->chip == BRCM_CC_43362_CHIP_ID) { 139 /* assign GPIO to SDIO core */ 140 addr = CORE_CC_REG(SI_ENUM_BASE, gpiocontrol); 141 gpiocontrol = brcmf_sdiod_regrl(sdiodev, addr, &ret); 142 gpiocontrol |= 0x2; 143 brcmf_sdiod_regwl(sdiodev, addr, gpiocontrol, &ret); 144 145 brcmf_sdiod_regwb(sdiodev, SBSDIO_GPIO_SELECT, 0xf, 146 &ret); 147 brcmf_sdiod_regwb(sdiodev, SBSDIO_GPIO_OUT, 0, &ret); 148 brcmf_sdiod_regwb(sdiodev, SBSDIO_GPIO_EN, 0x2, &ret); 149 } 150 151 /* must configure SDIO_CCCR_IENx to enable irq */ 152 data = brcmf_sdiod_regrb(sdiodev, SDIO_CCCR_IENx, &ret); 153 data |= 1 << SDIO_FUNC_1 | 1 << SDIO_FUNC_2 | 1; 154 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_IENx, data, &ret); 155 156 /* redirect, configure and enable io for interrupt signal */ 157 data = SDIO_SEPINT_MASK | SDIO_SEPINT_OE; 158 if (pdata->oob_irq_flags & IRQF_TRIGGER_HIGH) 159 data |= SDIO_SEPINT_ACT_HI; 160 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_BRCM_SEPINT, data, &ret); 161 162 sdio_release_host(sdiodev->func[1]); 163 } else { 164 brcmf_dbg(SDIO, "Entering\n"); 165 sdio_claim_host(sdiodev->func[1]); 166 sdio_claim_irq(sdiodev->func[1], brcmf_sdiod_ib_irqhandler); 167 sdio_claim_irq(sdiodev->func[2], brcmf_sdiod_dummy_irqhandler); 168 sdio_release_host(sdiodev->func[1]); 169 } 170 171 return 0; 172 } 173 174 int brcmf_sdiod_intr_unregister(struct brcmf_sdio_dev *sdiodev) 175 { 176 struct brcmfmac_sdio_pd *pdata; 177 178 brcmf_dbg(SDIO, "Entering\n"); 179 180 pdata = &sdiodev->settings->bus.sdio; 181 if (pdata->oob_irq_supported) { 182 sdio_claim_host(sdiodev->func[1]); 183 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_BRCM_SEPINT, 0, NULL); 184 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_IENx, 0, NULL); 185 sdio_release_host(sdiodev->func[1]); 186 187 if (sdiodev->oob_irq_requested) { 188 sdiodev->oob_irq_requested = false; 189 if (sdiodev->irq_wake) { 190 disable_irq_wake(pdata->oob_irq_nr); 191 sdiodev->irq_wake = false; 192 } 193 free_irq(pdata->oob_irq_nr, &sdiodev->func[1]->dev); 194 sdiodev->irq_en = false; 195 } 196 } else { 197 sdio_claim_host(sdiodev->func[1]); 198 sdio_release_irq(sdiodev->func[2]); 199 sdio_release_irq(sdiodev->func[1]); 200 sdio_release_host(sdiodev->func[1]); 201 } 202 203 return 0; 204 } 205 206 void brcmf_sdiod_change_state(struct brcmf_sdio_dev *sdiodev, 207 enum brcmf_sdiod_state state) 208 { 209 if (sdiodev->state == BRCMF_SDIOD_NOMEDIUM || 210 state == sdiodev->state) 211 return; 212 213 brcmf_dbg(TRACE, "%d -> %d\n", sdiodev->state, state); 214 switch (sdiodev->state) { 215 case BRCMF_SDIOD_DATA: 216 /* any other state means bus interface is down */ 217 brcmf_bus_change_state(sdiodev->bus_if, BRCMF_BUS_DOWN); 218 break; 219 case BRCMF_SDIOD_DOWN: 220 /* transition from DOWN to DATA means bus interface is up */ 221 if (state == BRCMF_SDIOD_DATA) 222 brcmf_bus_change_state(sdiodev->bus_if, BRCMF_BUS_UP); 223 break; 224 default: 225 break; 226 } 227 sdiodev->state = state; 228 } 229 230 static inline int brcmf_sdiod_f0_writeb(struct sdio_func *func, 231 uint regaddr, u8 byte) 232 { 233 int err_ret; 234 235 /* 236 * Can only directly write to some F0 registers. 237 * Handle CCCR_IENx and CCCR_ABORT command 238 * as a special case. 239 */ 240 if ((regaddr == SDIO_CCCR_ABORT) || 241 (regaddr == SDIO_CCCR_IENx)) 242 sdio_writeb(func, byte, regaddr, &err_ret); 243 else 244 sdio_f0_writeb(func, byte, regaddr, &err_ret); 245 246 return err_ret; 247 } 248 249 static int brcmf_sdiod_request_data(struct brcmf_sdio_dev *sdiodev, u8 fn, 250 u32 addr, u8 regsz, void *data, bool write) 251 { 252 struct sdio_func *func; 253 int ret = -EINVAL; 254 255 brcmf_dbg(SDIO, "rw=%d, func=%d, addr=0x%05x, nbytes=%d\n", 256 write, fn, addr, regsz); 257 258 /* only allow byte access on F0 */ 259 if (WARN_ON(regsz > 1 && !fn)) 260 return -EINVAL; 261 func = sdiodev->func[fn]; 262 263 switch (regsz) { 264 case sizeof(u8): 265 if (write) { 266 if (fn) 267 sdio_writeb(func, *(u8 *)data, addr, &ret); 268 else 269 ret = brcmf_sdiod_f0_writeb(func, addr, 270 *(u8 *)data); 271 } else { 272 if (fn) 273 *(u8 *)data = sdio_readb(func, addr, &ret); 274 else 275 *(u8 *)data = sdio_f0_readb(func, addr, &ret); 276 } 277 break; 278 case sizeof(u16): 279 if (write) 280 sdio_writew(func, *(u16 *)data, addr, &ret); 281 else 282 *(u16 *)data = sdio_readw(func, addr, &ret); 283 break; 284 case sizeof(u32): 285 if (write) 286 sdio_writel(func, *(u32 *)data, addr, &ret); 287 else 288 *(u32 *)data = sdio_readl(func, addr, &ret); 289 break; 290 default: 291 brcmf_err("invalid size: %d\n", regsz); 292 break; 293 } 294 295 if (ret) 296 brcmf_dbg(SDIO, "failed to %s data F%d@0x%05x, err: %d\n", 297 write ? "write" : "read", fn, addr, ret); 298 299 return ret; 300 } 301 302 static int brcmf_sdiod_regrw_helper(struct brcmf_sdio_dev *sdiodev, u32 addr, 303 u8 regsz, void *data, bool write) 304 { 305 u8 func; 306 s32 retry = 0; 307 int ret; 308 309 if (sdiodev->state == BRCMF_SDIOD_NOMEDIUM) 310 return -ENOMEDIUM; 311 312 /* 313 * figure out how to read the register based on address range 314 * 0x00 ~ 0x7FF: function 0 CCCR and FBR 315 * 0x10000 ~ 0x1FFFF: function 1 miscellaneous registers 316 * The rest: function 1 silicon backplane core registers 317 */ 318 if ((addr & ~REG_F0_REG_MASK) == 0) 319 func = SDIO_FUNC_0; 320 else 321 func = SDIO_FUNC_1; 322 323 do { 324 if (!write) 325 memset(data, 0, regsz); 326 /* for retry wait for 1 ms till bus get settled down */ 327 if (retry) 328 usleep_range(1000, 2000); 329 ret = brcmf_sdiod_request_data(sdiodev, func, addr, regsz, 330 data, write); 331 } while (ret != 0 && ret != -ENOMEDIUM && 332 retry++ < SDIOH_API_ACCESS_RETRY_LIMIT); 333 334 if (ret == -ENOMEDIUM) 335 brcmf_sdiod_change_state(sdiodev, BRCMF_SDIOD_NOMEDIUM); 336 else if (ret != 0) { 337 /* 338 * SleepCSR register access can fail when 339 * waking up the device so reduce this noise 340 * in the logs. 341 */ 342 if (addr != SBSDIO_FUNC1_SLEEPCSR) 343 brcmf_err("failed to %s data F%d@0x%05x, err: %d\n", 344 write ? "write" : "read", func, addr, ret); 345 else 346 brcmf_dbg(SDIO, "failed to %s data F%d@0x%05x, err: %d\n", 347 write ? "write" : "read", func, addr, ret); 348 } 349 return ret; 350 } 351 352 static int 353 brcmf_sdiod_set_sbaddr_window(struct brcmf_sdio_dev *sdiodev, u32 address) 354 { 355 int err = 0, i; 356 u8 addr[3]; 357 358 if (sdiodev->state == BRCMF_SDIOD_NOMEDIUM) 359 return -ENOMEDIUM; 360 361 addr[0] = (address >> 8) & SBSDIO_SBADDRLOW_MASK; 362 addr[1] = (address >> 16) & SBSDIO_SBADDRMID_MASK; 363 addr[2] = (address >> 24) & SBSDIO_SBADDRHIGH_MASK; 364 365 for (i = 0; i < 3; i++) { 366 err = brcmf_sdiod_regrw_helper(sdiodev, 367 SBSDIO_FUNC1_SBADDRLOW + i, 368 sizeof(u8), &addr[i], true); 369 if (err) { 370 brcmf_err("failed at addr: 0x%0x\n", 371 SBSDIO_FUNC1_SBADDRLOW + i); 372 break; 373 } 374 } 375 376 return err; 377 } 378 379 static int 380 brcmf_sdiod_addrprep(struct brcmf_sdio_dev *sdiodev, uint width, u32 *addr) 381 { 382 uint bar0 = *addr & ~SBSDIO_SB_OFT_ADDR_MASK; 383 int err = 0; 384 385 if (bar0 != sdiodev->sbwad) { 386 err = brcmf_sdiod_set_sbaddr_window(sdiodev, bar0); 387 if (err) 388 return err; 389 390 sdiodev->sbwad = bar0; 391 } 392 393 *addr &= SBSDIO_SB_OFT_ADDR_MASK; 394 395 if (width == 4) 396 *addr |= SBSDIO_SB_ACCESS_2_4B_FLAG; 397 398 return 0; 399 } 400 401 u8 brcmf_sdiod_regrb(struct brcmf_sdio_dev *sdiodev, u32 addr, int *ret) 402 { 403 u8 data; 404 int retval; 405 406 brcmf_dbg(SDIO, "addr:0x%08x\n", addr); 407 retval = brcmf_sdiod_regrw_helper(sdiodev, addr, sizeof(data), &data, 408 false); 409 brcmf_dbg(SDIO, "data:0x%02x\n", data); 410 411 if (ret) 412 *ret = retval; 413 414 return data; 415 } 416 417 u32 brcmf_sdiod_regrl(struct brcmf_sdio_dev *sdiodev, u32 addr, int *ret) 418 { 419 u32 data; 420 int retval; 421 422 brcmf_dbg(SDIO, "addr:0x%08x\n", addr); 423 retval = brcmf_sdiod_addrprep(sdiodev, sizeof(data), &addr); 424 if (retval) 425 goto done; 426 retval = brcmf_sdiod_regrw_helper(sdiodev, addr, sizeof(data), &data, 427 false); 428 brcmf_dbg(SDIO, "data:0x%08x\n", data); 429 430 done: 431 if (ret) 432 *ret = retval; 433 434 return data; 435 } 436 437 void brcmf_sdiod_regwb(struct brcmf_sdio_dev *sdiodev, u32 addr, 438 u8 data, int *ret) 439 { 440 int retval; 441 442 brcmf_dbg(SDIO, "addr:0x%08x, data:0x%02x\n", addr, data); 443 retval = brcmf_sdiod_regrw_helper(sdiodev, addr, sizeof(data), &data, 444 true); 445 if (ret) 446 *ret = retval; 447 } 448 449 void brcmf_sdiod_regwl(struct brcmf_sdio_dev *sdiodev, u32 addr, 450 u32 data, int *ret) 451 { 452 int retval; 453 454 brcmf_dbg(SDIO, "addr:0x%08x, data:0x%08x\n", addr, data); 455 retval = brcmf_sdiod_addrprep(sdiodev, sizeof(data), &addr); 456 if (retval) 457 goto done; 458 retval = brcmf_sdiod_regrw_helper(sdiodev, addr, sizeof(data), &data, 459 true); 460 461 done: 462 if (ret) 463 *ret = retval; 464 } 465 466 static int brcmf_sdiod_buffrw(struct brcmf_sdio_dev *sdiodev, uint fn, 467 bool write, u32 addr, struct sk_buff *pkt) 468 { 469 unsigned int req_sz; 470 int err; 471 472 /* Single skb use the standard mmc interface */ 473 req_sz = pkt->len + 3; 474 req_sz &= (uint)~3; 475 476 if (write) 477 err = sdio_memcpy_toio(sdiodev->func[fn], addr, 478 ((u8 *)(pkt->data)), req_sz); 479 else if (fn == 1) 480 err = sdio_memcpy_fromio(sdiodev->func[fn], ((u8 *)(pkt->data)), 481 addr, req_sz); 482 else 483 /* function 2 read is FIFO operation */ 484 err = sdio_readsb(sdiodev->func[fn], ((u8 *)(pkt->data)), addr, 485 req_sz); 486 if (err == -ENOMEDIUM) 487 brcmf_sdiod_change_state(sdiodev, BRCMF_SDIOD_NOMEDIUM); 488 return err; 489 } 490 491 /** 492 * brcmf_sdiod_sglist_rw - SDIO interface function for block data access 493 * @sdiodev: brcmfmac sdio device 494 * @fn: SDIO function number 495 * @write: direction flag 496 * @addr: dongle memory address as source/destination 497 * @pkt: skb pointer 498 * 499 * This function takes the respbonsibility as the interface function to MMC 500 * stack for block data access. It assumes that the skb passed down by the 501 * caller has already been padded and aligned. 502 */ 503 static int brcmf_sdiod_sglist_rw(struct brcmf_sdio_dev *sdiodev, uint fn, 504 bool write, u32 addr, 505 struct sk_buff_head *pktlist) 506 { 507 unsigned int req_sz, func_blk_sz, sg_cnt, sg_data_sz, pkt_offset; 508 unsigned int max_req_sz, orig_offset, dst_offset; 509 unsigned short max_seg_cnt, seg_sz; 510 unsigned char *pkt_data, *orig_data, *dst_data; 511 struct sk_buff *pkt_next = NULL, *local_pkt_next; 512 struct sk_buff_head local_list, *target_list; 513 struct mmc_request mmc_req; 514 struct mmc_command mmc_cmd; 515 struct mmc_data mmc_dat; 516 struct scatterlist *sgl; 517 int ret = 0; 518 519 if (!pktlist->qlen) 520 return -EINVAL; 521 522 target_list = pktlist; 523 /* for host with broken sg support, prepare a page aligned list */ 524 __skb_queue_head_init(&local_list); 525 if (!write && sdiodev->settings->bus.sdio.broken_sg_support) { 526 req_sz = 0; 527 skb_queue_walk(pktlist, pkt_next) 528 req_sz += pkt_next->len; 529 req_sz = ALIGN(req_sz, sdiodev->func[fn]->cur_blksize); 530 while (req_sz > PAGE_SIZE) { 531 pkt_next = brcmu_pkt_buf_get_skb(PAGE_SIZE); 532 if (pkt_next == NULL) { 533 ret = -ENOMEM; 534 goto exit; 535 } 536 __skb_queue_tail(&local_list, pkt_next); 537 req_sz -= PAGE_SIZE; 538 } 539 pkt_next = brcmu_pkt_buf_get_skb(req_sz); 540 if (pkt_next == NULL) { 541 ret = -ENOMEM; 542 goto exit; 543 } 544 __skb_queue_tail(&local_list, pkt_next); 545 target_list = &local_list; 546 } 547 548 func_blk_sz = sdiodev->func[fn]->cur_blksize; 549 max_req_sz = sdiodev->max_request_size; 550 max_seg_cnt = min_t(unsigned short, sdiodev->max_segment_count, 551 target_list->qlen); 552 seg_sz = target_list->qlen; 553 pkt_offset = 0; 554 pkt_next = target_list->next; 555 556 memset(&mmc_req, 0, sizeof(struct mmc_request)); 557 memset(&mmc_cmd, 0, sizeof(struct mmc_command)); 558 memset(&mmc_dat, 0, sizeof(struct mmc_data)); 559 560 mmc_dat.sg = sdiodev->sgtable.sgl; 561 mmc_dat.blksz = func_blk_sz; 562 mmc_dat.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ; 563 mmc_cmd.opcode = SD_IO_RW_EXTENDED; 564 mmc_cmd.arg = write ? 1<<31 : 0; /* write flag */ 565 mmc_cmd.arg |= (fn & 0x7) << 28; /* SDIO func num */ 566 mmc_cmd.arg |= 1<<27; /* block mode */ 567 /* for function 1 the addr will be incremented */ 568 mmc_cmd.arg |= (fn == 1) ? 1<<26 : 0; 569 mmc_cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC; 570 mmc_req.cmd = &mmc_cmd; 571 mmc_req.data = &mmc_dat; 572 573 while (seg_sz) { 574 req_sz = 0; 575 sg_cnt = 0; 576 sgl = sdiodev->sgtable.sgl; 577 /* prep sg table */ 578 while (pkt_next != (struct sk_buff *)target_list) { 579 pkt_data = pkt_next->data + pkt_offset; 580 sg_data_sz = pkt_next->len - pkt_offset; 581 if (sg_data_sz > sdiodev->max_segment_size) 582 sg_data_sz = sdiodev->max_segment_size; 583 if (sg_data_sz > max_req_sz - req_sz) 584 sg_data_sz = max_req_sz - req_sz; 585 586 sg_set_buf(sgl, pkt_data, sg_data_sz); 587 588 sg_cnt++; 589 sgl = sg_next(sgl); 590 req_sz += sg_data_sz; 591 pkt_offset += sg_data_sz; 592 if (pkt_offset == pkt_next->len) { 593 pkt_offset = 0; 594 pkt_next = pkt_next->next; 595 } 596 597 if (req_sz >= max_req_sz || sg_cnt >= max_seg_cnt) 598 break; 599 } 600 seg_sz -= sg_cnt; 601 602 if (req_sz % func_blk_sz != 0) { 603 brcmf_err("sg request length %u is not %u aligned\n", 604 req_sz, func_blk_sz); 605 ret = -ENOTBLK; 606 goto exit; 607 } 608 609 mmc_dat.sg_len = sg_cnt; 610 mmc_dat.blocks = req_sz / func_blk_sz; 611 mmc_cmd.arg |= (addr & 0x1FFFF) << 9; /* address */ 612 mmc_cmd.arg |= mmc_dat.blocks & 0x1FF; /* block count */ 613 /* incrementing addr for function 1 */ 614 if (fn == 1) 615 addr += req_sz; 616 617 mmc_set_data_timeout(&mmc_dat, sdiodev->func[fn]->card); 618 mmc_wait_for_req(sdiodev->func[fn]->card->host, &mmc_req); 619 620 ret = mmc_cmd.error ? mmc_cmd.error : mmc_dat.error; 621 if (ret == -ENOMEDIUM) { 622 brcmf_sdiod_change_state(sdiodev, BRCMF_SDIOD_NOMEDIUM); 623 break; 624 } else if (ret != 0) { 625 brcmf_err("CMD53 sg block %s failed %d\n", 626 write ? "write" : "read", ret); 627 ret = -EIO; 628 break; 629 } 630 } 631 632 if (!write && sdiodev->settings->bus.sdio.broken_sg_support) { 633 local_pkt_next = local_list.next; 634 orig_offset = 0; 635 skb_queue_walk(pktlist, pkt_next) { 636 dst_offset = 0; 637 do { 638 req_sz = local_pkt_next->len - orig_offset; 639 req_sz = min_t(uint, pkt_next->len - dst_offset, 640 req_sz); 641 orig_data = local_pkt_next->data + orig_offset; 642 dst_data = pkt_next->data + dst_offset; 643 memcpy(dst_data, orig_data, req_sz); 644 orig_offset += req_sz; 645 dst_offset += req_sz; 646 if (orig_offset == local_pkt_next->len) { 647 orig_offset = 0; 648 local_pkt_next = local_pkt_next->next; 649 } 650 if (dst_offset == pkt_next->len) 651 break; 652 } while (!skb_queue_empty(&local_list)); 653 } 654 } 655 656 exit: 657 sg_init_table(sdiodev->sgtable.sgl, sdiodev->sgtable.orig_nents); 658 while ((pkt_next = __skb_dequeue(&local_list)) != NULL) 659 brcmu_pkt_buf_free_skb(pkt_next); 660 661 return ret; 662 } 663 664 int brcmf_sdiod_recv_buf(struct brcmf_sdio_dev *sdiodev, u8 *buf, uint nbytes) 665 { 666 struct sk_buff *mypkt; 667 int err; 668 669 mypkt = brcmu_pkt_buf_get_skb(nbytes); 670 if (!mypkt) { 671 brcmf_err("brcmu_pkt_buf_get_skb failed: len %d\n", 672 nbytes); 673 return -EIO; 674 } 675 676 err = brcmf_sdiod_recv_pkt(sdiodev, mypkt); 677 if (!err) 678 memcpy(buf, mypkt->data, nbytes); 679 680 brcmu_pkt_buf_free_skb(mypkt); 681 return err; 682 } 683 684 int brcmf_sdiod_recv_pkt(struct brcmf_sdio_dev *sdiodev, struct sk_buff *pkt) 685 { 686 u32 addr = sdiodev->sbwad; 687 int err = 0; 688 689 brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n", addr, pkt->len); 690 691 err = brcmf_sdiod_addrprep(sdiodev, 4, &addr); 692 if (err) 693 goto done; 694 695 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, false, addr, pkt); 696 697 done: 698 return err; 699 } 700 701 int brcmf_sdiod_recv_chain(struct brcmf_sdio_dev *sdiodev, 702 struct sk_buff_head *pktq, uint totlen) 703 { 704 struct sk_buff *glom_skb; 705 struct sk_buff *skb; 706 u32 addr = sdiodev->sbwad; 707 int err = 0; 708 709 brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n", 710 addr, pktq->qlen); 711 712 err = brcmf_sdiod_addrprep(sdiodev, 4, &addr); 713 if (err) 714 goto done; 715 716 if (pktq->qlen == 1) 717 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, false, addr, 718 pktq->next); 719 else if (!sdiodev->sg_support) { 720 glom_skb = brcmu_pkt_buf_get_skb(totlen); 721 if (!glom_skb) 722 return -ENOMEM; 723 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, false, addr, 724 glom_skb); 725 if (err) 726 goto done; 727 728 skb_queue_walk(pktq, skb) { 729 memcpy(skb->data, glom_skb->data, skb->len); 730 skb_pull(glom_skb, skb->len); 731 } 732 } else 733 err = brcmf_sdiod_sglist_rw(sdiodev, SDIO_FUNC_2, false, addr, 734 pktq); 735 736 done: 737 return err; 738 } 739 740 int brcmf_sdiod_send_buf(struct brcmf_sdio_dev *sdiodev, u8 *buf, uint nbytes) 741 { 742 struct sk_buff *mypkt; 743 u32 addr = sdiodev->sbwad; 744 int err; 745 746 mypkt = brcmu_pkt_buf_get_skb(nbytes); 747 if (!mypkt) { 748 brcmf_err("brcmu_pkt_buf_get_skb failed: len %d\n", 749 nbytes); 750 return -EIO; 751 } 752 753 memcpy(mypkt->data, buf, nbytes); 754 755 err = brcmf_sdiod_addrprep(sdiodev, 4, &addr); 756 757 if (!err) 758 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, true, addr, 759 mypkt); 760 761 brcmu_pkt_buf_free_skb(mypkt); 762 return err; 763 764 } 765 766 int brcmf_sdiod_send_pkt(struct brcmf_sdio_dev *sdiodev, 767 struct sk_buff_head *pktq) 768 { 769 struct sk_buff *skb; 770 u32 addr = sdiodev->sbwad; 771 int err; 772 773 brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n", addr, pktq->qlen); 774 775 err = brcmf_sdiod_addrprep(sdiodev, 4, &addr); 776 if (err) 777 return err; 778 779 if (pktq->qlen == 1 || !sdiodev->sg_support) 780 skb_queue_walk(pktq, skb) { 781 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, true, 782 addr, skb); 783 if (err) 784 break; 785 } 786 else 787 err = brcmf_sdiod_sglist_rw(sdiodev, SDIO_FUNC_2, true, addr, 788 pktq); 789 790 return err; 791 } 792 793 int 794 brcmf_sdiod_ramrw(struct brcmf_sdio_dev *sdiodev, bool write, u32 address, 795 u8 *data, uint size) 796 { 797 int bcmerror = 0; 798 struct sk_buff *pkt; 799 u32 sdaddr; 800 uint dsize; 801 802 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size); 803 pkt = dev_alloc_skb(dsize); 804 if (!pkt) { 805 brcmf_err("dev_alloc_skb failed: len %d\n", dsize); 806 return -EIO; 807 } 808 pkt->priority = 0; 809 810 /* Determine initial transfer parameters */ 811 sdaddr = address & SBSDIO_SB_OFT_ADDR_MASK; 812 if ((sdaddr + size) & SBSDIO_SBWINDOW_MASK) 813 dsize = (SBSDIO_SB_OFT_ADDR_LIMIT - sdaddr); 814 else 815 dsize = size; 816 817 sdio_claim_host(sdiodev->func[1]); 818 819 /* Do the transfer(s) */ 820 while (size) { 821 /* Set the backplane window to include the start address */ 822 bcmerror = brcmf_sdiod_set_sbaddr_window(sdiodev, address); 823 if (bcmerror) 824 break; 825 826 brcmf_dbg(SDIO, "%s %d bytes at offset 0x%08x in window 0x%08x\n", 827 write ? "write" : "read", dsize, 828 sdaddr, address & SBSDIO_SBWINDOW_MASK); 829 830 sdaddr &= SBSDIO_SB_OFT_ADDR_MASK; 831 sdaddr |= SBSDIO_SB_ACCESS_2_4B_FLAG; 832 833 skb_put(pkt, dsize); 834 if (write) 835 memcpy(pkt->data, data, dsize); 836 bcmerror = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_1, write, 837 sdaddr, pkt); 838 if (bcmerror) { 839 brcmf_err("membytes transfer failed\n"); 840 break; 841 } 842 if (!write) 843 memcpy(data, pkt->data, dsize); 844 skb_trim(pkt, 0); 845 846 /* Adjust for next transfer (if any) */ 847 size -= dsize; 848 if (size) { 849 data += dsize; 850 address += dsize; 851 sdaddr = 0; 852 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size); 853 } 854 } 855 856 dev_kfree_skb(pkt); 857 858 /* Return the window to backplane enumeration space for core access */ 859 if (brcmf_sdiod_set_sbaddr_window(sdiodev, sdiodev->sbwad)) 860 brcmf_err("FAILED to set window back to 0x%x\n", 861 sdiodev->sbwad); 862 863 sdio_release_host(sdiodev->func[1]); 864 865 return bcmerror; 866 } 867 868 int brcmf_sdiod_abort(struct brcmf_sdio_dev *sdiodev, uint fn) 869 { 870 char t_func = (char)fn; 871 brcmf_dbg(SDIO, "Enter\n"); 872 873 /* issue abort cmd52 command through F0 */ 874 brcmf_sdiod_request_data(sdiodev, SDIO_FUNC_0, SDIO_CCCR_ABORT, 875 sizeof(t_func), &t_func, true); 876 877 brcmf_dbg(SDIO, "Exit\n"); 878 return 0; 879 } 880 881 void brcmf_sdiod_sgtable_alloc(struct brcmf_sdio_dev *sdiodev) 882 { 883 struct sdio_func *func; 884 struct mmc_host *host; 885 uint max_blocks; 886 uint nents; 887 int err; 888 889 func = sdiodev->func[2]; 890 host = func->card->host; 891 sdiodev->sg_support = host->max_segs > 1; 892 max_blocks = min_t(uint, host->max_blk_count, 511u); 893 sdiodev->max_request_size = min_t(uint, host->max_req_size, 894 max_blocks * func->cur_blksize); 895 sdiodev->max_segment_count = min_t(uint, host->max_segs, 896 SG_MAX_SINGLE_ALLOC); 897 sdiodev->max_segment_size = host->max_seg_size; 898 899 if (!sdiodev->sg_support) 900 return; 901 902 nents = max_t(uint, BRCMF_DEFAULT_RXGLOM_SIZE, 903 sdiodev->settings->bus.sdio.txglomsz); 904 nents += (nents >> 4) + 1; 905 906 WARN_ON(nents > sdiodev->max_segment_count); 907 908 brcmf_dbg(TRACE, "nents=%d\n", nents); 909 err = sg_alloc_table(&sdiodev->sgtable, nents, GFP_KERNEL); 910 if (err < 0) { 911 brcmf_err("allocation failed: disable scatter-gather"); 912 sdiodev->sg_support = false; 913 } 914 915 sdiodev->txglomsz = sdiodev->settings->bus.sdio.txglomsz; 916 } 917 918 #ifdef CONFIG_PM_SLEEP 919 static int brcmf_sdiod_freezer_attach(struct brcmf_sdio_dev *sdiodev) 920 { 921 sdiodev->freezer = kzalloc(sizeof(*sdiodev->freezer), GFP_KERNEL); 922 if (!sdiodev->freezer) 923 return -ENOMEM; 924 atomic_set(&sdiodev->freezer->thread_count, 0); 925 atomic_set(&sdiodev->freezer->freezing, 0); 926 init_waitqueue_head(&sdiodev->freezer->thread_freeze); 927 init_completion(&sdiodev->freezer->resumed); 928 return 0; 929 } 930 931 static void brcmf_sdiod_freezer_detach(struct brcmf_sdio_dev *sdiodev) 932 { 933 if (sdiodev->freezer) { 934 WARN_ON(atomic_read(&sdiodev->freezer->freezing)); 935 kfree(sdiodev->freezer); 936 } 937 } 938 939 static int brcmf_sdiod_freezer_on(struct brcmf_sdio_dev *sdiodev) 940 { 941 atomic_t *expect = &sdiodev->freezer->thread_count; 942 int res = 0; 943 944 sdiodev->freezer->frozen_count = 0; 945 reinit_completion(&sdiodev->freezer->resumed); 946 atomic_set(&sdiodev->freezer->freezing, 1); 947 brcmf_sdio_trigger_dpc(sdiodev->bus); 948 wait_event(sdiodev->freezer->thread_freeze, 949 atomic_read(expect) == sdiodev->freezer->frozen_count); 950 sdio_claim_host(sdiodev->func[1]); 951 res = brcmf_sdio_sleep(sdiodev->bus, true); 952 sdio_release_host(sdiodev->func[1]); 953 return res; 954 } 955 956 static void brcmf_sdiod_freezer_off(struct brcmf_sdio_dev *sdiodev) 957 { 958 sdio_claim_host(sdiodev->func[1]); 959 brcmf_sdio_sleep(sdiodev->bus, false); 960 sdio_release_host(sdiodev->func[1]); 961 atomic_set(&sdiodev->freezer->freezing, 0); 962 complete_all(&sdiodev->freezer->resumed); 963 } 964 965 bool brcmf_sdiod_freezing(struct brcmf_sdio_dev *sdiodev) 966 { 967 return atomic_read(&sdiodev->freezer->freezing); 968 } 969 970 void brcmf_sdiod_try_freeze(struct brcmf_sdio_dev *sdiodev) 971 { 972 if (!brcmf_sdiod_freezing(sdiodev)) 973 return; 974 sdiodev->freezer->frozen_count++; 975 wake_up(&sdiodev->freezer->thread_freeze); 976 wait_for_completion(&sdiodev->freezer->resumed); 977 } 978 979 void brcmf_sdiod_freezer_count(struct brcmf_sdio_dev *sdiodev) 980 { 981 atomic_inc(&sdiodev->freezer->thread_count); 982 } 983 984 void brcmf_sdiod_freezer_uncount(struct brcmf_sdio_dev *sdiodev) 985 { 986 atomic_dec(&sdiodev->freezer->thread_count); 987 } 988 #else 989 static int brcmf_sdiod_freezer_attach(struct brcmf_sdio_dev *sdiodev) 990 { 991 return 0; 992 } 993 994 static void brcmf_sdiod_freezer_detach(struct brcmf_sdio_dev *sdiodev) 995 { 996 } 997 #endif /* CONFIG_PM_SLEEP */ 998 999 static int brcmf_sdiod_remove(struct brcmf_sdio_dev *sdiodev) 1000 { 1001 sdiodev->state = BRCMF_SDIOD_DOWN; 1002 if (sdiodev->bus) { 1003 brcmf_sdio_remove(sdiodev->bus); 1004 sdiodev->bus = NULL; 1005 } 1006 1007 brcmf_sdiod_freezer_detach(sdiodev); 1008 1009 /* Disable Function 2 */ 1010 sdio_claim_host(sdiodev->func[2]); 1011 sdio_disable_func(sdiodev->func[2]); 1012 sdio_release_host(sdiodev->func[2]); 1013 1014 /* Disable Function 1 */ 1015 sdio_claim_host(sdiodev->func[1]); 1016 sdio_disable_func(sdiodev->func[1]); 1017 sdio_release_host(sdiodev->func[1]); 1018 1019 sg_free_table(&sdiodev->sgtable); 1020 sdiodev->sbwad = 0; 1021 1022 pm_runtime_allow(sdiodev->func[1]->card->host->parent); 1023 return 0; 1024 } 1025 1026 static void brcmf_sdiod_host_fixup(struct mmc_host *host) 1027 { 1028 /* runtime-pm powers off the device */ 1029 pm_runtime_forbid(host->parent); 1030 /* avoid removal detection upon resume */ 1031 host->caps |= MMC_CAP_NONREMOVABLE; 1032 } 1033 1034 static int brcmf_sdiod_probe(struct brcmf_sdio_dev *sdiodev) 1035 { 1036 int ret = 0; 1037 1038 sdiodev->num_funcs = 2; 1039 1040 sdio_claim_host(sdiodev->func[1]); 1041 1042 ret = sdio_set_block_size(sdiodev->func[1], SDIO_FUNC1_BLOCKSIZE); 1043 if (ret) { 1044 brcmf_err("Failed to set F1 blocksize\n"); 1045 sdio_release_host(sdiodev->func[1]); 1046 goto out; 1047 } 1048 ret = sdio_set_block_size(sdiodev->func[2], SDIO_FUNC2_BLOCKSIZE); 1049 if (ret) { 1050 brcmf_err("Failed to set F2 blocksize\n"); 1051 sdio_release_host(sdiodev->func[1]); 1052 goto out; 1053 } 1054 1055 /* increase F2 timeout */ 1056 sdiodev->func[2]->enable_timeout = SDIO_WAIT_F2RDY; 1057 1058 /* Enable Function 1 */ 1059 ret = sdio_enable_func(sdiodev->func[1]); 1060 sdio_release_host(sdiodev->func[1]); 1061 if (ret) { 1062 brcmf_err("Failed to enable F1: err=%d\n", ret); 1063 goto out; 1064 } 1065 1066 ret = brcmf_sdiod_freezer_attach(sdiodev); 1067 if (ret) 1068 goto out; 1069 1070 /* try to attach to the target device */ 1071 sdiodev->bus = brcmf_sdio_probe(sdiodev); 1072 if (!sdiodev->bus) { 1073 ret = -ENODEV; 1074 goto out; 1075 } 1076 brcmf_sdiod_host_fixup(sdiodev->func[2]->card->host); 1077 out: 1078 if (ret) 1079 brcmf_sdiod_remove(sdiodev); 1080 1081 return ret; 1082 } 1083 1084 #define BRCMF_SDIO_DEVICE(dev_id) \ 1085 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, dev_id)} 1086 1087 /* devices we support, null terminated */ 1088 static const struct sdio_device_id brcmf_sdmmc_ids[] = { 1089 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43143), 1090 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43241), 1091 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4329), 1092 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4330), 1093 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4334), 1094 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43340), 1095 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43341), 1096 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43362), 1097 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4335_4339), 1098 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43430), 1099 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4345), 1100 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4354), 1101 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4356), 1102 { /* end: all zeroes */ } 1103 }; 1104 MODULE_DEVICE_TABLE(sdio, brcmf_sdmmc_ids); 1105 1106 1107 static void brcmf_sdiod_acpi_set_power_manageable(struct device *dev, 1108 int val) 1109 { 1110 #if IS_ENABLED(CONFIG_ACPI) 1111 struct acpi_device *adev; 1112 1113 adev = ACPI_COMPANION(dev); 1114 if (adev) 1115 adev->flags.power_manageable = 0; 1116 #endif 1117 } 1118 1119 static int brcmf_ops_sdio_probe(struct sdio_func *func, 1120 const struct sdio_device_id *id) 1121 { 1122 int err; 1123 struct brcmf_sdio_dev *sdiodev; 1124 struct brcmf_bus *bus_if; 1125 struct device *dev; 1126 1127 brcmf_dbg(SDIO, "Enter\n"); 1128 brcmf_dbg(SDIO, "Class=%x\n", func->class); 1129 brcmf_dbg(SDIO, "sdio vendor ID: 0x%04x\n", func->vendor); 1130 brcmf_dbg(SDIO, "sdio device ID: 0x%04x\n", func->device); 1131 brcmf_dbg(SDIO, "Function#: %d\n", func->num); 1132 1133 dev = &func->dev; 1134 /* prohibit ACPI power management for this device */ 1135 brcmf_sdiod_acpi_set_power_manageable(dev, 0); 1136 1137 /* Consume func num 1 but dont do anything with it. */ 1138 if (func->num == 1) 1139 return 0; 1140 1141 /* Ignore anything but func 2 */ 1142 if (func->num != 2) 1143 return -ENODEV; 1144 1145 bus_if = kzalloc(sizeof(struct brcmf_bus), GFP_KERNEL); 1146 if (!bus_if) 1147 return -ENOMEM; 1148 sdiodev = kzalloc(sizeof(struct brcmf_sdio_dev), GFP_KERNEL); 1149 if (!sdiodev) { 1150 kfree(bus_if); 1151 return -ENOMEM; 1152 } 1153 1154 /* store refs to functions used. mmc_card does 1155 * not hold the F0 function pointer. 1156 */ 1157 sdiodev->func[0] = kmemdup(func, sizeof(*func), GFP_KERNEL); 1158 sdiodev->func[0]->num = 0; 1159 sdiodev->func[1] = func->card->sdio_func[0]; 1160 sdiodev->func[2] = func; 1161 1162 sdiodev->bus_if = bus_if; 1163 bus_if->bus_priv.sdio = sdiodev; 1164 bus_if->proto_type = BRCMF_PROTO_BCDC; 1165 dev_set_drvdata(&func->dev, bus_if); 1166 dev_set_drvdata(&sdiodev->func[1]->dev, bus_if); 1167 sdiodev->dev = &sdiodev->func[1]->dev; 1168 1169 brcmf_sdiod_change_state(sdiodev, BRCMF_SDIOD_DOWN); 1170 1171 brcmf_dbg(SDIO, "F2 found, calling brcmf_sdiod_probe...\n"); 1172 err = brcmf_sdiod_probe(sdiodev); 1173 if (err) { 1174 brcmf_err("F2 error, probe failed %d...\n", err); 1175 goto fail; 1176 } 1177 1178 brcmf_dbg(SDIO, "F2 init completed...\n"); 1179 return 0; 1180 1181 fail: 1182 dev_set_drvdata(&func->dev, NULL); 1183 dev_set_drvdata(&sdiodev->func[1]->dev, NULL); 1184 kfree(sdiodev->func[0]); 1185 kfree(sdiodev); 1186 kfree(bus_if); 1187 return err; 1188 } 1189 1190 static void brcmf_ops_sdio_remove(struct sdio_func *func) 1191 { 1192 struct brcmf_bus *bus_if; 1193 struct brcmf_sdio_dev *sdiodev; 1194 1195 brcmf_dbg(SDIO, "Enter\n"); 1196 brcmf_dbg(SDIO, "sdio vendor ID: 0x%04x\n", func->vendor); 1197 brcmf_dbg(SDIO, "sdio device ID: 0x%04x\n", func->device); 1198 brcmf_dbg(SDIO, "Function: %d\n", func->num); 1199 1200 if (func->num != 1) 1201 return; 1202 1203 bus_if = dev_get_drvdata(&func->dev); 1204 if (bus_if) { 1205 sdiodev = bus_if->bus_priv.sdio; 1206 brcmf_sdiod_remove(sdiodev); 1207 1208 dev_set_drvdata(&sdiodev->func[1]->dev, NULL); 1209 dev_set_drvdata(&sdiodev->func[2]->dev, NULL); 1210 1211 kfree(bus_if); 1212 kfree(sdiodev->func[0]); 1213 kfree(sdiodev); 1214 } 1215 1216 brcmf_dbg(SDIO, "Exit\n"); 1217 } 1218 1219 void brcmf_sdio_wowl_config(struct device *dev, bool enabled) 1220 { 1221 struct brcmf_bus *bus_if = dev_get_drvdata(dev); 1222 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio; 1223 1224 brcmf_dbg(SDIO, "Configuring WOWL, enabled=%d\n", enabled); 1225 sdiodev->wowl_enabled = enabled; 1226 } 1227 1228 #ifdef CONFIG_PM_SLEEP 1229 static int brcmf_ops_sdio_suspend(struct device *dev) 1230 { 1231 struct sdio_func *func; 1232 struct brcmf_bus *bus_if; 1233 struct brcmf_sdio_dev *sdiodev; 1234 mmc_pm_flag_t sdio_flags; 1235 1236 func = container_of(dev, struct sdio_func, dev); 1237 brcmf_dbg(SDIO, "Enter: F%d\n", func->num); 1238 if (func->num != SDIO_FUNC_1) 1239 return 0; 1240 1241 1242 bus_if = dev_get_drvdata(dev); 1243 sdiodev = bus_if->bus_priv.sdio; 1244 1245 brcmf_sdiod_freezer_on(sdiodev); 1246 brcmf_sdio_wd_timer(sdiodev->bus, 0); 1247 1248 sdio_flags = MMC_PM_KEEP_POWER; 1249 if (sdiodev->wowl_enabled) { 1250 if (sdiodev->settings->bus.sdio.oob_irq_supported) 1251 enable_irq_wake(sdiodev->settings->bus.sdio.oob_irq_nr); 1252 else 1253 sdio_flags |= MMC_PM_WAKE_SDIO_IRQ; 1254 } 1255 if (sdio_set_host_pm_flags(sdiodev->func[1], sdio_flags)) 1256 brcmf_err("Failed to set pm_flags %x\n", sdio_flags); 1257 return 0; 1258 } 1259 1260 static int brcmf_ops_sdio_resume(struct device *dev) 1261 { 1262 struct brcmf_bus *bus_if = dev_get_drvdata(dev); 1263 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio; 1264 struct sdio_func *func = container_of(dev, struct sdio_func, dev); 1265 1266 brcmf_dbg(SDIO, "Enter: F%d\n", func->num); 1267 if (func->num != SDIO_FUNC_2) 1268 return 0; 1269 1270 brcmf_sdiod_freezer_off(sdiodev); 1271 return 0; 1272 } 1273 1274 static const struct dev_pm_ops brcmf_sdio_pm_ops = { 1275 .suspend = brcmf_ops_sdio_suspend, 1276 .resume = brcmf_ops_sdio_resume, 1277 }; 1278 #endif /* CONFIG_PM_SLEEP */ 1279 1280 static struct sdio_driver brcmf_sdmmc_driver = { 1281 .probe = brcmf_ops_sdio_probe, 1282 .remove = brcmf_ops_sdio_remove, 1283 .name = KBUILD_MODNAME, 1284 .id_table = brcmf_sdmmc_ids, 1285 .drv = { 1286 .owner = THIS_MODULE, 1287 #ifdef CONFIG_PM_SLEEP 1288 .pm = &brcmf_sdio_pm_ops, 1289 #endif /* CONFIG_PM_SLEEP */ 1290 }, 1291 }; 1292 1293 void brcmf_sdio_register(void) 1294 { 1295 int ret; 1296 1297 ret = sdio_register_driver(&brcmf_sdmmc_driver); 1298 if (ret) 1299 brcmf_err("sdio_register_driver failed: %d\n", ret); 1300 } 1301 1302 void brcmf_sdio_exit(void) 1303 { 1304 brcmf_dbg(SDIO, "Enter\n"); 1305 1306 sdio_unregister_driver(&brcmf_sdmmc_driver); 1307 } 1308 1309