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 static int mmc_submit_one(struct mmc_data *md, struct mmc_request *mr, 346 struct mmc_command *mc, int sg_cnt, int req_sz, 347 int func_blk_sz, u32 *addr, 348 struct brcmf_sdio_dev *sdiodev, 349 struct sdio_func *func, int write) 350 { 351 int ret; 352 353 md->sg_len = sg_cnt; 354 md->blocks = req_sz / func_blk_sz; 355 mc->arg |= (*addr & 0x1FFFF) << 9; /* address */ 356 mc->arg |= md->blocks & 0x1FF; /* block count */ 357 /* incrementing addr for function 1 */ 358 if (func->num == 1) 359 *addr += req_sz; 360 361 mmc_set_data_timeout(md, func->card); 362 mmc_wait_for_req(func->card->host, mr); 363 364 ret = mc->error ? mc->error : md->error; 365 if (ret == -ENOMEDIUM) { 366 brcmf_sdiod_change_state(sdiodev, BRCMF_SDIOD_NOMEDIUM); 367 } else if (ret != 0) { 368 brcmf_err("CMD53 sg block %s failed %d\n", 369 write ? "write" : "read", ret); 370 ret = -EIO; 371 } 372 373 return ret; 374 } 375 376 /** 377 * brcmf_sdiod_sglist_rw - SDIO interface function for block data access 378 * @sdiodev: brcmfmac sdio device 379 * @func: SDIO function 380 * @write: direction flag 381 * @addr: dongle memory address as source/destination 382 * @pkt: skb pointer 383 * 384 * This function takes the respbonsibility as the interface function to MMC 385 * stack for block data access. It assumes that the skb passed down by the 386 * caller has already been padded and aligned. 387 */ 388 static int brcmf_sdiod_sglist_rw(struct brcmf_sdio_dev *sdiodev, 389 struct sdio_func *func, 390 bool write, u32 addr, 391 struct sk_buff_head *pktlist) 392 { 393 unsigned int req_sz, func_blk_sz, sg_cnt, sg_data_sz, pkt_offset; 394 unsigned int max_req_sz, src_offset, dst_offset; 395 unsigned char *pkt_data, *orig_data, *dst_data; 396 struct sk_buff_head local_list, *target_list; 397 struct sk_buff *pkt_next = NULL, *src; 398 unsigned short max_seg_cnt; 399 struct mmc_request mmc_req; 400 struct mmc_command mmc_cmd; 401 struct mmc_data mmc_dat; 402 struct scatterlist *sgl; 403 int ret = 0; 404 405 if (!pktlist->qlen) 406 return -EINVAL; 407 408 target_list = pktlist; 409 /* for host with broken sg support, prepare a page aligned list */ 410 __skb_queue_head_init(&local_list); 411 if (!write && sdiodev->settings->bus.sdio.broken_sg_support) { 412 req_sz = 0; 413 skb_queue_walk(pktlist, pkt_next) 414 req_sz += pkt_next->len; 415 req_sz = ALIGN(req_sz, func->cur_blksize); 416 while (req_sz > PAGE_SIZE) { 417 pkt_next = brcmu_pkt_buf_get_skb(PAGE_SIZE); 418 if (pkt_next == NULL) { 419 ret = -ENOMEM; 420 goto exit; 421 } 422 __skb_queue_tail(&local_list, pkt_next); 423 req_sz -= PAGE_SIZE; 424 } 425 pkt_next = brcmu_pkt_buf_get_skb(req_sz); 426 if (pkt_next == NULL) { 427 ret = -ENOMEM; 428 goto exit; 429 } 430 __skb_queue_tail(&local_list, pkt_next); 431 target_list = &local_list; 432 } 433 434 func_blk_sz = func->cur_blksize; 435 max_req_sz = sdiodev->max_request_size; 436 max_seg_cnt = min_t(unsigned short, sdiodev->max_segment_count, 437 target_list->qlen); 438 439 memset(&mmc_req, 0, sizeof(struct mmc_request)); 440 memset(&mmc_cmd, 0, sizeof(struct mmc_command)); 441 memset(&mmc_dat, 0, sizeof(struct mmc_data)); 442 443 mmc_dat.sg = sdiodev->sgtable.sgl; 444 mmc_dat.blksz = func_blk_sz; 445 mmc_dat.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ; 446 mmc_cmd.opcode = SD_IO_RW_EXTENDED; 447 mmc_cmd.arg = write ? 1<<31 : 0; /* write flag */ 448 mmc_cmd.arg |= (func->num & 0x7) << 28; /* SDIO func num */ 449 mmc_cmd.arg |= 1 << 27; /* block mode */ 450 /* for function 1 the addr will be incremented */ 451 mmc_cmd.arg |= (func->num == 1) ? 1 << 26 : 0; 452 mmc_cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC; 453 mmc_req.cmd = &mmc_cmd; 454 mmc_req.data = &mmc_dat; 455 456 req_sz = 0; 457 sg_cnt = 0; 458 sgl = sdiodev->sgtable.sgl; 459 skb_queue_walk(target_list, pkt_next) { 460 pkt_offset = 0; 461 while (pkt_offset < pkt_next->len) { 462 pkt_data = pkt_next->data + pkt_offset; 463 sg_data_sz = pkt_next->len - pkt_offset; 464 if (sg_data_sz > sdiodev->max_segment_size) 465 sg_data_sz = sdiodev->max_segment_size; 466 if (sg_data_sz > max_req_sz - req_sz) 467 sg_data_sz = max_req_sz - req_sz; 468 469 sg_set_buf(sgl, pkt_data, sg_data_sz); 470 sg_cnt++; 471 472 sgl = sg_next(sgl); 473 req_sz += sg_data_sz; 474 pkt_offset += sg_data_sz; 475 if (req_sz >= max_req_sz || sg_cnt >= max_seg_cnt) { 476 ret = mmc_submit_one(&mmc_dat, &mmc_req, &mmc_cmd, 477 sg_cnt, req_sz, func_blk_sz, 478 &addr, sdiodev, func, write); 479 if (ret) 480 goto exit_queue_walk; 481 req_sz = 0; 482 sg_cnt = 0; 483 sgl = sdiodev->sgtable.sgl; 484 } 485 } 486 } 487 if (sg_cnt) 488 ret = mmc_submit_one(&mmc_dat, &mmc_req, &mmc_cmd, 489 sg_cnt, req_sz, func_blk_sz, 490 &addr, sdiodev, func, write); 491 exit_queue_walk: 492 if (!write && sdiodev->settings->bus.sdio.broken_sg_support) { 493 src = __skb_peek(&local_list); 494 src_offset = 0; 495 skb_queue_walk(pktlist, pkt_next) { 496 dst_offset = 0; 497 498 /* This is safe because we must have enough SKB data 499 * in the local list to cover everything in pktlist. 500 */ 501 while (1) { 502 req_sz = pkt_next->len - dst_offset; 503 if (req_sz > src->len - src_offset) 504 req_sz = src->len - src_offset; 505 506 orig_data = src->data + src_offset; 507 dst_data = pkt_next->data + dst_offset; 508 memcpy(dst_data, orig_data, req_sz); 509 510 src_offset += req_sz; 511 if (src_offset == src->len) { 512 src_offset = 0; 513 src = skb_peek_next(src, &local_list); 514 } 515 dst_offset += req_sz; 516 if (dst_offset == pkt_next->len) 517 break; 518 } 519 } 520 } 521 522 exit: 523 sg_init_table(sdiodev->sgtable.sgl, sdiodev->sgtable.orig_nents); 524 while ((pkt_next = __skb_dequeue(&local_list)) != NULL) 525 brcmu_pkt_buf_free_skb(pkt_next); 526 527 return ret; 528 } 529 530 int brcmf_sdiod_recv_buf(struct brcmf_sdio_dev *sdiodev, u8 *buf, uint nbytes) 531 { 532 struct sk_buff *mypkt; 533 int err; 534 535 mypkt = brcmu_pkt_buf_get_skb(nbytes); 536 if (!mypkt) { 537 brcmf_err("brcmu_pkt_buf_get_skb failed: len %d\n", 538 nbytes); 539 return -EIO; 540 } 541 542 err = brcmf_sdiod_recv_pkt(sdiodev, mypkt); 543 if (!err) 544 memcpy(buf, mypkt->data, nbytes); 545 546 brcmu_pkt_buf_free_skb(mypkt); 547 return err; 548 } 549 550 int brcmf_sdiod_recv_pkt(struct brcmf_sdio_dev *sdiodev, struct sk_buff *pkt) 551 { 552 u32 addr = sdiodev->cc_core->base; 553 int err = 0; 554 555 brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n", addr, pkt->len); 556 557 err = brcmf_sdiod_set_backplane_window(sdiodev, addr); 558 if (err) 559 goto done; 560 561 addr &= SBSDIO_SB_OFT_ADDR_MASK; 562 addr |= SBSDIO_SB_ACCESS_2_4B_FLAG; 563 564 err = brcmf_sdiod_skbuff_read(sdiodev, sdiodev->func2, addr, pkt); 565 566 done: 567 return err; 568 } 569 570 int brcmf_sdiod_recv_chain(struct brcmf_sdio_dev *sdiodev, 571 struct sk_buff_head *pktq, uint totlen) 572 { 573 struct sk_buff *glom_skb = NULL; 574 struct sk_buff *skb; 575 u32 addr = sdiodev->cc_core->base; 576 int err = 0; 577 578 brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n", 579 addr, pktq->qlen); 580 581 err = brcmf_sdiod_set_backplane_window(sdiodev, addr); 582 if (err) 583 goto done; 584 585 addr &= SBSDIO_SB_OFT_ADDR_MASK; 586 addr |= SBSDIO_SB_ACCESS_2_4B_FLAG; 587 588 if (pktq->qlen == 1) 589 err = brcmf_sdiod_skbuff_read(sdiodev, sdiodev->func2, addr, 590 __skb_peek(pktq)); 591 else if (!sdiodev->sg_support) { 592 glom_skb = brcmu_pkt_buf_get_skb(totlen); 593 if (!glom_skb) 594 return -ENOMEM; 595 err = brcmf_sdiod_skbuff_read(sdiodev, sdiodev->func2, addr, 596 glom_skb); 597 if (err) 598 goto done; 599 600 skb_queue_walk(pktq, skb) { 601 memcpy(skb->data, glom_skb->data, skb->len); 602 skb_pull(glom_skb, skb->len); 603 } 604 } else 605 err = brcmf_sdiod_sglist_rw(sdiodev, sdiodev->func2, false, 606 addr, pktq); 607 608 done: 609 brcmu_pkt_buf_free_skb(glom_skb); 610 return err; 611 } 612 613 int brcmf_sdiod_send_buf(struct brcmf_sdio_dev *sdiodev, u8 *buf, uint nbytes) 614 { 615 struct sk_buff *mypkt; 616 u32 addr = sdiodev->cc_core->base; 617 int err; 618 619 mypkt = brcmu_pkt_buf_get_skb(nbytes); 620 621 if (!mypkt) { 622 brcmf_err("brcmu_pkt_buf_get_skb failed: len %d\n", 623 nbytes); 624 return -EIO; 625 } 626 627 memcpy(mypkt->data, buf, nbytes); 628 629 err = brcmf_sdiod_set_backplane_window(sdiodev, addr); 630 if (err) 631 goto out; 632 633 addr &= SBSDIO_SB_OFT_ADDR_MASK; 634 addr |= SBSDIO_SB_ACCESS_2_4B_FLAG; 635 636 err = brcmf_sdiod_skbuff_write(sdiodev, sdiodev->func2, addr, mypkt); 637 out: 638 brcmu_pkt_buf_free_skb(mypkt); 639 640 return err; 641 } 642 643 int brcmf_sdiod_send_pkt(struct brcmf_sdio_dev *sdiodev, 644 struct sk_buff_head *pktq) 645 { 646 struct sk_buff *skb; 647 u32 addr = sdiodev->cc_core->base; 648 int err; 649 650 brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n", addr, pktq->qlen); 651 652 err = brcmf_sdiod_set_backplane_window(sdiodev, addr); 653 if (err) 654 return err; 655 656 addr &= SBSDIO_SB_OFT_ADDR_MASK; 657 addr |= SBSDIO_SB_ACCESS_2_4B_FLAG; 658 659 if (pktq->qlen == 1 || !sdiodev->sg_support) { 660 skb_queue_walk(pktq, skb) { 661 err = brcmf_sdiod_skbuff_write(sdiodev, sdiodev->func2, 662 addr, skb); 663 if (err) 664 break; 665 } 666 } else { 667 err = brcmf_sdiod_sglist_rw(sdiodev, sdiodev->func2, true, 668 addr, pktq); 669 } 670 671 return err; 672 } 673 674 int 675 brcmf_sdiod_ramrw(struct brcmf_sdio_dev *sdiodev, bool write, u32 address, 676 u8 *data, uint size) 677 { 678 int err = 0; 679 struct sk_buff *pkt; 680 u32 sdaddr; 681 uint dsize; 682 683 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size); 684 pkt = dev_alloc_skb(dsize); 685 if (!pkt) { 686 brcmf_err("dev_alloc_skb failed: len %d\n", dsize); 687 return -EIO; 688 } 689 pkt->priority = 0; 690 691 /* Determine initial transfer parameters */ 692 sdaddr = address & SBSDIO_SB_OFT_ADDR_MASK; 693 if ((sdaddr + size) & SBSDIO_SBWINDOW_MASK) 694 dsize = (SBSDIO_SB_OFT_ADDR_LIMIT - sdaddr); 695 else 696 dsize = size; 697 698 sdio_claim_host(sdiodev->func1); 699 700 /* Do the transfer(s) */ 701 while (size) { 702 /* Set the backplane window to include the start address */ 703 err = brcmf_sdiod_set_backplane_window(sdiodev, address); 704 if (err) 705 break; 706 707 brcmf_dbg(SDIO, "%s %d bytes at offset 0x%08x in window 0x%08x\n", 708 write ? "write" : "read", dsize, 709 sdaddr, address & SBSDIO_SBWINDOW_MASK); 710 711 sdaddr &= SBSDIO_SB_OFT_ADDR_MASK; 712 sdaddr |= SBSDIO_SB_ACCESS_2_4B_FLAG; 713 714 skb_put(pkt, dsize); 715 716 if (write) { 717 memcpy(pkt->data, data, dsize); 718 err = brcmf_sdiod_skbuff_write(sdiodev, sdiodev->func1, 719 sdaddr, pkt); 720 } else { 721 err = brcmf_sdiod_skbuff_read(sdiodev, sdiodev->func1, 722 sdaddr, pkt); 723 } 724 725 if (err) { 726 brcmf_err("membytes transfer failed\n"); 727 break; 728 } 729 if (!write) 730 memcpy(data, pkt->data, dsize); 731 skb_trim(pkt, 0); 732 733 /* Adjust for next transfer (if any) */ 734 size -= dsize; 735 if (size) { 736 data += dsize; 737 address += dsize; 738 sdaddr = 0; 739 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size); 740 } 741 } 742 743 dev_kfree_skb(pkt); 744 745 sdio_release_host(sdiodev->func1); 746 747 return err; 748 } 749 750 int brcmf_sdiod_abort(struct brcmf_sdio_dev *sdiodev, struct sdio_func *func) 751 { 752 brcmf_dbg(SDIO, "Enter\n"); 753 754 /* Issue abort cmd52 command through F0 */ 755 brcmf_sdiod_func0_wb(sdiodev, SDIO_CCCR_ABORT, func->num, NULL); 756 757 brcmf_dbg(SDIO, "Exit\n"); 758 return 0; 759 } 760 761 void brcmf_sdiod_sgtable_alloc(struct brcmf_sdio_dev *sdiodev) 762 { 763 struct sdio_func *func; 764 struct mmc_host *host; 765 uint max_blocks; 766 uint nents; 767 int err; 768 769 func = sdiodev->func2; 770 host = func->card->host; 771 sdiodev->sg_support = host->max_segs > 1; 772 max_blocks = min_t(uint, host->max_blk_count, 511u); 773 sdiodev->max_request_size = min_t(uint, host->max_req_size, 774 max_blocks * func->cur_blksize); 775 sdiodev->max_segment_count = min_t(uint, host->max_segs, 776 SG_MAX_SINGLE_ALLOC); 777 sdiodev->max_segment_size = host->max_seg_size; 778 779 if (!sdiodev->sg_support) 780 return; 781 782 nents = max_t(uint, BRCMF_DEFAULT_RXGLOM_SIZE, 783 sdiodev->settings->bus.sdio.txglomsz); 784 nents += (nents >> 4) + 1; 785 786 WARN_ON(nents > sdiodev->max_segment_count); 787 788 brcmf_dbg(TRACE, "nents=%d\n", nents); 789 err = sg_alloc_table(&sdiodev->sgtable, nents, GFP_KERNEL); 790 if (err < 0) { 791 brcmf_err("allocation failed: disable scatter-gather"); 792 sdiodev->sg_support = false; 793 } 794 795 sdiodev->txglomsz = sdiodev->settings->bus.sdio.txglomsz; 796 } 797 798 #ifdef CONFIG_PM_SLEEP 799 static int brcmf_sdiod_freezer_attach(struct brcmf_sdio_dev *sdiodev) 800 { 801 sdiodev->freezer = kzalloc(sizeof(*sdiodev->freezer), GFP_KERNEL); 802 if (!sdiodev->freezer) 803 return -ENOMEM; 804 atomic_set(&sdiodev->freezer->thread_count, 0); 805 atomic_set(&sdiodev->freezer->freezing, 0); 806 init_waitqueue_head(&sdiodev->freezer->thread_freeze); 807 init_completion(&sdiodev->freezer->resumed); 808 return 0; 809 } 810 811 static void brcmf_sdiod_freezer_detach(struct brcmf_sdio_dev *sdiodev) 812 { 813 if (sdiodev->freezer) { 814 WARN_ON(atomic_read(&sdiodev->freezer->freezing)); 815 kfree(sdiodev->freezer); 816 } 817 } 818 819 static int brcmf_sdiod_freezer_on(struct brcmf_sdio_dev *sdiodev) 820 { 821 atomic_t *expect = &sdiodev->freezer->thread_count; 822 int res = 0; 823 824 sdiodev->freezer->frozen_count = 0; 825 reinit_completion(&sdiodev->freezer->resumed); 826 atomic_set(&sdiodev->freezer->freezing, 1); 827 brcmf_sdio_trigger_dpc(sdiodev->bus); 828 wait_event(sdiodev->freezer->thread_freeze, 829 atomic_read(expect) == sdiodev->freezer->frozen_count); 830 sdio_claim_host(sdiodev->func1); 831 res = brcmf_sdio_sleep(sdiodev->bus, true); 832 sdio_release_host(sdiodev->func1); 833 return res; 834 } 835 836 static void brcmf_sdiod_freezer_off(struct brcmf_sdio_dev *sdiodev) 837 { 838 sdio_claim_host(sdiodev->func1); 839 brcmf_sdio_sleep(sdiodev->bus, false); 840 sdio_release_host(sdiodev->func1); 841 atomic_set(&sdiodev->freezer->freezing, 0); 842 complete_all(&sdiodev->freezer->resumed); 843 } 844 845 bool brcmf_sdiod_freezing(struct brcmf_sdio_dev *sdiodev) 846 { 847 return atomic_read(&sdiodev->freezer->freezing); 848 } 849 850 void brcmf_sdiod_try_freeze(struct brcmf_sdio_dev *sdiodev) 851 { 852 if (!brcmf_sdiod_freezing(sdiodev)) 853 return; 854 sdiodev->freezer->frozen_count++; 855 wake_up(&sdiodev->freezer->thread_freeze); 856 wait_for_completion(&sdiodev->freezer->resumed); 857 } 858 859 void brcmf_sdiod_freezer_count(struct brcmf_sdio_dev *sdiodev) 860 { 861 atomic_inc(&sdiodev->freezer->thread_count); 862 } 863 864 void brcmf_sdiod_freezer_uncount(struct brcmf_sdio_dev *sdiodev) 865 { 866 atomic_dec(&sdiodev->freezer->thread_count); 867 } 868 #else 869 static int brcmf_sdiod_freezer_attach(struct brcmf_sdio_dev *sdiodev) 870 { 871 return 0; 872 } 873 874 static void brcmf_sdiod_freezer_detach(struct brcmf_sdio_dev *sdiodev) 875 { 876 } 877 #endif /* CONFIG_PM_SLEEP */ 878 879 static int brcmf_sdiod_remove(struct brcmf_sdio_dev *sdiodev) 880 { 881 sdiodev->state = BRCMF_SDIOD_DOWN; 882 if (sdiodev->bus) { 883 brcmf_sdio_remove(sdiodev->bus); 884 sdiodev->bus = NULL; 885 } 886 887 brcmf_sdiod_freezer_detach(sdiodev); 888 889 /* Disable Function 2 */ 890 sdio_claim_host(sdiodev->func2); 891 sdio_disable_func(sdiodev->func2); 892 sdio_release_host(sdiodev->func2); 893 894 /* Disable Function 1 */ 895 sdio_claim_host(sdiodev->func1); 896 sdio_disable_func(sdiodev->func1); 897 sdio_release_host(sdiodev->func1); 898 899 sg_free_table(&sdiodev->sgtable); 900 sdiodev->sbwad = 0; 901 902 pm_runtime_allow(sdiodev->func1->card->host->parent); 903 return 0; 904 } 905 906 static void brcmf_sdiod_host_fixup(struct mmc_host *host) 907 { 908 /* runtime-pm powers off the device */ 909 pm_runtime_forbid(host->parent); 910 /* avoid removal detection upon resume */ 911 host->caps |= MMC_CAP_NONREMOVABLE; 912 } 913 914 static int brcmf_sdiod_probe(struct brcmf_sdio_dev *sdiodev) 915 { 916 int ret = 0; 917 918 sdio_claim_host(sdiodev->func1); 919 920 ret = sdio_set_block_size(sdiodev->func1, SDIO_FUNC1_BLOCKSIZE); 921 if (ret) { 922 brcmf_err("Failed to set F1 blocksize\n"); 923 sdio_release_host(sdiodev->func1); 924 goto out; 925 } 926 ret = sdio_set_block_size(sdiodev->func2, SDIO_FUNC2_BLOCKSIZE); 927 if (ret) { 928 brcmf_err("Failed to set F2 blocksize\n"); 929 sdio_release_host(sdiodev->func1); 930 goto out; 931 } 932 933 /* increase F2 timeout */ 934 sdiodev->func2->enable_timeout = SDIO_WAIT_F2RDY; 935 936 /* Enable Function 1 */ 937 ret = sdio_enable_func(sdiodev->func1); 938 sdio_release_host(sdiodev->func1); 939 if (ret) { 940 brcmf_err("Failed to enable F1: err=%d\n", ret); 941 goto out; 942 } 943 944 ret = brcmf_sdiod_freezer_attach(sdiodev); 945 if (ret) 946 goto out; 947 948 /* try to attach to the target device */ 949 sdiodev->bus = brcmf_sdio_probe(sdiodev); 950 if (!sdiodev->bus) { 951 ret = -ENODEV; 952 goto out; 953 } 954 brcmf_sdiod_host_fixup(sdiodev->func2->card->host); 955 out: 956 if (ret) 957 brcmf_sdiod_remove(sdiodev); 958 959 return ret; 960 } 961 962 #define BRCMF_SDIO_DEVICE(dev_id) \ 963 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, dev_id)} 964 965 /* devices we support, null terminated */ 966 static const struct sdio_device_id brcmf_sdmmc_ids[] = { 967 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43143), 968 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43241), 969 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4329), 970 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4330), 971 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4334), 972 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43340), 973 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43341), 974 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43362), 975 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43364), 976 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4335_4339), 977 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4339), 978 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43430), 979 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4345), 980 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43455), 981 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4354), 982 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4356), 983 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_CYPRESS_4373), 984 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_CYPRESS_43012), 985 { /* end: all zeroes */ } 986 }; 987 MODULE_DEVICE_TABLE(sdio, brcmf_sdmmc_ids); 988 989 990 static void brcmf_sdiod_acpi_set_power_manageable(struct device *dev, 991 int val) 992 { 993 #if IS_ENABLED(CONFIG_ACPI) 994 struct acpi_device *adev; 995 996 adev = ACPI_COMPANION(dev); 997 if (adev) 998 adev->flags.power_manageable = 0; 999 #endif 1000 } 1001 1002 static int brcmf_ops_sdio_probe(struct sdio_func *func, 1003 const struct sdio_device_id *id) 1004 { 1005 int err; 1006 struct brcmf_sdio_dev *sdiodev; 1007 struct brcmf_bus *bus_if; 1008 struct device *dev; 1009 1010 brcmf_dbg(SDIO, "Enter\n"); 1011 brcmf_dbg(SDIO, "Class=%x\n", func->class); 1012 brcmf_dbg(SDIO, "sdio vendor ID: 0x%04x\n", func->vendor); 1013 brcmf_dbg(SDIO, "sdio device ID: 0x%04x\n", func->device); 1014 brcmf_dbg(SDIO, "Function#: %d\n", func->num); 1015 1016 dev = &func->dev; 1017 1018 /* Set MMC_QUIRK_LENIENT_FN0 for this card */ 1019 func->card->quirks |= MMC_QUIRK_LENIENT_FN0; 1020 1021 /* prohibit ACPI power management for this device */ 1022 brcmf_sdiod_acpi_set_power_manageable(dev, 0); 1023 1024 /* Consume func num 1 but dont do anything with it. */ 1025 if (func->num == 1) 1026 return 0; 1027 1028 /* Ignore anything but func 2 */ 1029 if (func->num != 2) 1030 return -ENODEV; 1031 1032 bus_if = kzalloc(sizeof(struct brcmf_bus), GFP_KERNEL); 1033 if (!bus_if) 1034 return -ENOMEM; 1035 sdiodev = kzalloc(sizeof(struct brcmf_sdio_dev), GFP_KERNEL); 1036 if (!sdiodev) { 1037 kfree(bus_if); 1038 return -ENOMEM; 1039 } 1040 1041 /* store refs to functions used. mmc_card does 1042 * not hold the F0 function pointer. 1043 */ 1044 sdiodev->func1 = func->card->sdio_func[0]; 1045 sdiodev->func2 = func; 1046 1047 sdiodev->bus_if = bus_if; 1048 bus_if->bus_priv.sdio = sdiodev; 1049 bus_if->proto_type = BRCMF_PROTO_BCDC; 1050 dev_set_drvdata(&func->dev, bus_if); 1051 dev_set_drvdata(&sdiodev->func1->dev, bus_if); 1052 sdiodev->dev = &sdiodev->func1->dev; 1053 1054 brcmf_sdiod_change_state(sdiodev, BRCMF_SDIOD_DOWN); 1055 1056 brcmf_dbg(SDIO, "F2 found, calling brcmf_sdiod_probe...\n"); 1057 err = brcmf_sdiod_probe(sdiodev); 1058 if (err) { 1059 brcmf_err("F2 error, probe failed %d...\n", err); 1060 goto fail; 1061 } 1062 1063 brcmf_dbg(SDIO, "F2 init completed...\n"); 1064 return 0; 1065 1066 fail: 1067 dev_set_drvdata(&func->dev, NULL); 1068 dev_set_drvdata(&sdiodev->func1->dev, NULL); 1069 kfree(sdiodev); 1070 kfree(bus_if); 1071 return err; 1072 } 1073 1074 static void brcmf_ops_sdio_remove(struct sdio_func *func) 1075 { 1076 struct brcmf_bus *bus_if; 1077 struct brcmf_sdio_dev *sdiodev; 1078 1079 brcmf_dbg(SDIO, "Enter\n"); 1080 brcmf_dbg(SDIO, "sdio vendor ID: 0x%04x\n", func->vendor); 1081 brcmf_dbg(SDIO, "sdio device ID: 0x%04x\n", func->device); 1082 brcmf_dbg(SDIO, "Function: %d\n", func->num); 1083 1084 bus_if = dev_get_drvdata(&func->dev); 1085 if (bus_if) { 1086 sdiodev = bus_if->bus_priv.sdio; 1087 1088 /* start by unregistering irqs */ 1089 brcmf_sdiod_intr_unregister(sdiodev); 1090 1091 if (func->num != 1) 1092 return; 1093 1094 /* only proceed with rest of cleanup if func 1 */ 1095 brcmf_sdiod_remove(sdiodev); 1096 1097 dev_set_drvdata(&sdiodev->func1->dev, NULL); 1098 dev_set_drvdata(&sdiodev->func2->dev, NULL); 1099 1100 kfree(bus_if); 1101 kfree(sdiodev); 1102 } 1103 1104 brcmf_dbg(SDIO, "Exit\n"); 1105 } 1106 1107 void brcmf_sdio_wowl_config(struct device *dev, bool enabled) 1108 { 1109 struct brcmf_bus *bus_if = dev_get_drvdata(dev); 1110 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio; 1111 1112 brcmf_dbg(SDIO, "Configuring WOWL, enabled=%d\n", enabled); 1113 sdiodev->wowl_enabled = enabled; 1114 } 1115 1116 #ifdef CONFIG_PM_SLEEP 1117 static int brcmf_ops_sdio_suspend(struct device *dev) 1118 { 1119 struct sdio_func *func; 1120 struct brcmf_bus *bus_if; 1121 struct brcmf_sdio_dev *sdiodev; 1122 mmc_pm_flag_t sdio_flags; 1123 1124 func = container_of(dev, struct sdio_func, dev); 1125 brcmf_dbg(SDIO, "Enter: F%d\n", func->num); 1126 if (func->num != 1) 1127 return 0; 1128 1129 1130 bus_if = dev_get_drvdata(dev); 1131 sdiodev = bus_if->bus_priv.sdio; 1132 1133 brcmf_sdiod_freezer_on(sdiodev); 1134 brcmf_sdio_wd_timer(sdiodev->bus, 0); 1135 1136 sdio_flags = MMC_PM_KEEP_POWER; 1137 if (sdiodev->wowl_enabled) { 1138 if (sdiodev->settings->bus.sdio.oob_irq_supported) 1139 enable_irq_wake(sdiodev->settings->bus.sdio.oob_irq_nr); 1140 else 1141 sdio_flags |= MMC_PM_WAKE_SDIO_IRQ; 1142 } 1143 if (sdio_set_host_pm_flags(sdiodev->func1, sdio_flags)) 1144 brcmf_err("Failed to set pm_flags %x\n", sdio_flags); 1145 return 0; 1146 } 1147 1148 static int brcmf_ops_sdio_resume(struct device *dev) 1149 { 1150 struct brcmf_bus *bus_if = dev_get_drvdata(dev); 1151 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio; 1152 struct sdio_func *func = container_of(dev, struct sdio_func, dev); 1153 1154 brcmf_dbg(SDIO, "Enter: F%d\n", func->num); 1155 if (func->num != 2) 1156 return 0; 1157 1158 brcmf_sdiod_freezer_off(sdiodev); 1159 return 0; 1160 } 1161 1162 static const struct dev_pm_ops brcmf_sdio_pm_ops = { 1163 .suspend = brcmf_ops_sdio_suspend, 1164 .resume = brcmf_ops_sdio_resume, 1165 }; 1166 #endif /* CONFIG_PM_SLEEP */ 1167 1168 static struct sdio_driver brcmf_sdmmc_driver = { 1169 .probe = brcmf_ops_sdio_probe, 1170 .remove = brcmf_ops_sdio_remove, 1171 .name = KBUILD_MODNAME, 1172 .id_table = brcmf_sdmmc_ids, 1173 .drv = { 1174 .owner = THIS_MODULE, 1175 #ifdef CONFIG_PM_SLEEP 1176 .pm = &brcmf_sdio_pm_ops, 1177 #endif /* CONFIG_PM_SLEEP */ 1178 .coredump = brcmf_dev_coredump, 1179 }, 1180 }; 1181 1182 void brcmf_sdio_register(void) 1183 { 1184 int ret; 1185 1186 ret = sdio_register_driver(&brcmf_sdmmc_driver); 1187 if (ret) 1188 brcmf_err("sdio_register_driver failed: %d\n", ret); 1189 } 1190 1191 void brcmf_sdio_exit(void) 1192 { 1193 brcmf_dbg(SDIO, "Enter\n"); 1194 1195 sdio_unregister_driver(&brcmf_sdmmc_driver); 1196 } 1197 1198