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