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 return err; 632 633 addr &= SBSDIO_SB_OFT_ADDR_MASK; 634 addr |= SBSDIO_SB_ACCESS_2_4B_FLAG; 635 636 if (!err) 637 err = brcmf_sdiod_skbuff_write(sdiodev, sdiodev->func2, addr, 638 mypkt); 639 640 brcmu_pkt_buf_free_skb(mypkt); 641 642 return err; 643 } 644 645 int brcmf_sdiod_send_pkt(struct brcmf_sdio_dev *sdiodev, 646 struct sk_buff_head *pktq) 647 { 648 struct sk_buff *skb; 649 u32 addr = sdiodev->cc_core->base; 650 int err; 651 652 brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n", addr, pktq->qlen); 653 654 err = brcmf_sdiod_set_backplane_window(sdiodev, addr); 655 if (err) 656 return err; 657 658 addr &= SBSDIO_SB_OFT_ADDR_MASK; 659 addr |= SBSDIO_SB_ACCESS_2_4B_FLAG; 660 661 if (pktq->qlen == 1 || !sdiodev->sg_support) { 662 skb_queue_walk(pktq, skb) { 663 err = brcmf_sdiod_skbuff_write(sdiodev, sdiodev->func2, 664 addr, skb); 665 if (err) 666 break; 667 } 668 } else { 669 err = brcmf_sdiod_sglist_rw(sdiodev, sdiodev->func2, true, 670 addr, pktq); 671 } 672 673 return err; 674 } 675 676 int 677 brcmf_sdiod_ramrw(struct brcmf_sdio_dev *sdiodev, bool write, u32 address, 678 u8 *data, uint size) 679 { 680 int err = 0; 681 struct sk_buff *pkt; 682 u32 sdaddr; 683 uint dsize; 684 685 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size); 686 pkt = dev_alloc_skb(dsize); 687 if (!pkt) { 688 brcmf_err("dev_alloc_skb failed: len %d\n", dsize); 689 return -EIO; 690 } 691 pkt->priority = 0; 692 693 /* Determine initial transfer parameters */ 694 sdaddr = address & SBSDIO_SB_OFT_ADDR_MASK; 695 if ((sdaddr + size) & SBSDIO_SBWINDOW_MASK) 696 dsize = (SBSDIO_SB_OFT_ADDR_LIMIT - sdaddr); 697 else 698 dsize = size; 699 700 sdio_claim_host(sdiodev->func1); 701 702 /* Do the transfer(s) */ 703 while (size) { 704 /* Set the backplane window to include the start address */ 705 err = brcmf_sdiod_set_backplane_window(sdiodev, address); 706 if (err) 707 break; 708 709 brcmf_dbg(SDIO, "%s %d bytes at offset 0x%08x in window 0x%08x\n", 710 write ? "write" : "read", dsize, 711 sdaddr, address & SBSDIO_SBWINDOW_MASK); 712 713 sdaddr &= SBSDIO_SB_OFT_ADDR_MASK; 714 sdaddr |= SBSDIO_SB_ACCESS_2_4B_FLAG; 715 716 skb_put(pkt, dsize); 717 718 if (write) { 719 memcpy(pkt->data, data, dsize); 720 err = brcmf_sdiod_skbuff_write(sdiodev, sdiodev->func1, 721 sdaddr, pkt); 722 } else { 723 err = brcmf_sdiod_skbuff_read(sdiodev, sdiodev->func1, 724 sdaddr, pkt); 725 } 726 727 if (err) { 728 brcmf_err("membytes transfer failed\n"); 729 break; 730 } 731 if (!write) 732 memcpy(data, pkt->data, dsize); 733 skb_trim(pkt, 0); 734 735 /* Adjust for next transfer (if any) */ 736 size -= dsize; 737 if (size) { 738 data += dsize; 739 address += dsize; 740 sdaddr = 0; 741 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size); 742 } 743 } 744 745 dev_kfree_skb(pkt); 746 747 sdio_release_host(sdiodev->func1); 748 749 return err; 750 } 751 752 int brcmf_sdiod_abort(struct brcmf_sdio_dev *sdiodev, struct sdio_func *func) 753 { 754 brcmf_dbg(SDIO, "Enter\n"); 755 756 /* Issue abort cmd52 command through F0 */ 757 brcmf_sdiod_func0_wb(sdiodev, SDIO_CCCR_ABORT, func->num, NULL); 758 759 brcmf_dbg(SDIO, "Exit\n"); 760 return 0; 761 } 762 763 void brcmf_sdiod_sgtable_alloc(struct brcmf_sdio_dev *sdiodev) 764 { 765 struct sdio_func *func; 766 struct mmc_host *host; 767 uint max_blocks; 768 uint nents; 769 int err; 770 771 func = sdiodev->func2; 772 host = func->card->host; 773 sdiodev->sg_support = host->max_segs > 1; 774 max_blocks = min_t(uint, host->max_blk_count, 511u); 775 sdiodev->max_request_size = min_t(uint, host->max_req_size, 776 max_blocks * func->cur_blksize); 777 sdiodev->max_segment_count = min_t(uint, host->max_segs, 778 SG_MAX_SINGLE_ALLOC); 779 sdiodev->max_segment_size = host->max_seg_size; 780 781 if (!sdiodev->sg_support) 782 return; 783 784 nents = max_t(uint, BRCMF_DEFAULT_RXGLOM_SIZE, 785 sdiodev->settings->bus.sdio.txglomsz); 786 nents += (nents >> 4) + 1; 787 788 WARN_ON(nents > sdiodev->max_segment_count); 789 790 brcmf_dbg(TRACE, "nents=%d\n", nents); 791 err = sg_alloc_table(&sdiodev->sgtable, nents, GFP_KERNEL); 792 if (err < 0) { 793 brcmf_err("allocation failed: disable scatter-gather"); 794 sdiodev->sg_support = false; 795 } 796 797 sdiodev->txglomsz = sdiodev->settings->bus.sdio.txglomsz; 798 } 799 800 #ifdef CONFIG_PM_SLEEP 801 static int brcmf_sdiod_freezer_attach(struct brcmf_sdio_dev *sdiodev) 802 { 803 sdiodev->freezer = kzalloc(sizeof(*sdiodev->freezer), GFP_KERNEL); 804 if (!sdiodev->freezer) 805 return -ENOMEM; 806 atomic_set(&sdiodev->freezer->thread_count, 0); 807 atomic_set(&sdiodev->freezer->freezing, 0); 808 init_waitqueue_head(&sdiodev->freezer->thread_freeze); 809 init_completion(&sdiodev->freezer->resumed); 810 return 0; 811 } 812 813 static void brcmf_sdiod_freezer_detach(struct brcmf_sdio_dev *sdiodev) 814 { 815 if (sdiodev->freezer) { 816 WARN_ON(atomic_read(&sdiodev->freezer->freezing)); 817 kfree(sdiodev->freezer); 818 } 819 } 820 821 static int brcmf_sdiod_freezer_on(struct brcmf_sdio_dev *sdiodev) 822 { 823 atomic_t *expect = &sdiodev->freezer->thread_count; 824 int res = 0; 825 826 sdiodev->freezer->frozen_count = 0; 827 reinit_completion(&sdiodev->freezer->resumed); 828 atomic_set(&sdiodev->freezer->freezing, 1); 829 brcmf_sdio_trigger_dpc(sdiodev->bus); 830 wait_event(sdiodev->freezer->thread_freeze, 831 atomic_read(expect) == sdiodev->freezer->frozen_count); 832 sdio_claim_host(sdiodev->func1); 833 res = brcmf_sdio_sleep(sdiodev->bus, true); 834 sdio_release_host(sdiodev->func1); 835 return res; 836 } 837 838 static void brcmf_sdiod_freezer_off(struct brcmf_sdio_dev *sdiodev) 839 { 840 sdio_claim_host(sdiodev->func1); 841 brcmf_sdio_sleep(sdiodev->bus, false); 842 sdio_release_host(sdiodev->func1); 843 atomic_set(&sdiodev->freezer->freezing, 0); 844 complete_all(&sdiodev->freezer->resumed); 845 } 846 847 bool brcmf_sdiod_freezing(struct brcmf_sdio_dev *sdiodev) 848 { 849 return atomic_read(&sdiodev->freezer->freezing); 850 } 851 852 void brcmf_sdiod_try_freeze(struct brcmf_sdio_dev *sdiodev) 853 { 854 if (!brcmf_sdiod_freezing(sdiodev)) 855 return; 856 sdiodev->freezer->frozen_count++; 857 wake_up(&sdiodev->freezer->thread_freeze); 858 wait_for_completion(&sdiodev->freezer->resumed); 859 } 860 861 void brcmf_sdiod_freezer_count(struct brcmf_sdio_dev *sdiodev) 862 { 863 atomic_inc(&sdiodev->freezer->thread_count); 864 } 865 866 void brcmf_sdiod_freezer_uncount(struct brcmf_sdio_dev *sdiodev) 867 { 868 atomic_dec(&sdiodev->freezer->thread_count); 869 } 870 #else 871 static int brcmf_sdiod_freezer_attach(struct brcmf_sdio_dev *sdiodev) 872 { 873 return 0; 874 } 875 876 static void brcmf_sdiod_freezer_detach(struct brcmf_sdio_dev *sdiodev) 877 { 878 } 879 #endif /* CONFIG_PM_SLEEP */ 880 881 static int brcmf_sdiod_remove(struct brcmf_sdio_dev *sdiodev) 882 { 883 sdiodev->state = BRCMF_SDIOD_DOWN; 884 if (sdiodev->bus) { 885 brcmf_sdio_remove(sdiodev->bus); 886 sdiodev->bus = NULL; 887 } 888 889 brcmf_sdiod_freezer_detach(sdiodev); 890 891 /* Disable Function 2 */ 892 sdio_claim_host(sdiodev->func2); 893 sdio_disable_func(sdiodev->func2); 894 sdio_release_host(sdiodev->func2); 895 896 /* Disable Function 1 */ 897 sdio_claim_host(sdiodev->func1); 898 sdio_disable_func(sdiodev->func1); 899 sdio_release_host(sdiodev->func1); 900 901 sg_free_table(&sdiodev->sgtable); 902 sdiodev->sbwad = 0; 903 904 pm_runtime_allow(sdiodev->func1->card->host->parent); 905 return 0; 906 } 907 908 static void brcmf_sdiod_host_fixup(struct mmc_host *host) 909 { 910 /* runtime-pm powers off the device */ 911 pm_runtime_forbid(host->parent); 912 /* avoid removal detection upon resume */ 913 host->caps |= MMC_CAP_NONREMOVABLE; 914 } 915 916 static int brcmf_sdiod_probe(struct brcmf_sdio_dev *sdiodev) 917 { 918 int ret = 0; 919 920 sdio_claim_host(sdiodev->func1); 921 922 ret = sdio_set_block_size(sdiodev->func1, SDIO_FUNC1_BLOCKSIZE); 923 if (ret) { 924 brcmf_err("Failed to set F1 blocksize\n"); 925 sdio_release_host(sdiodev->func1); 926 goto out; 927 } 928 ret = sdio_set_block_size(sdiodev->func2, SDIO_FUNC2_BLOCKSIZE); 929 if (ret) { 930 brcmf_err("Failed to set F2 blocksize\n"); 931 sdio_release_host(sdiodev->func1); 932 goto out; 933 } 934 935 /* increase F2 timeout */ 936 sdiodev->func2->enable_timeout = SDIO_WAIT_F2RDY; 937 938 /* Enable Function 1 */ 939 ret = sdio_enable_func(sdiodev->func1); 940 sdio_release_host(sdiodev->func1); 941 if (ret) { 942 brcmf_err("Failed to enable F1: err=%d\n", ret); 943 goto out; 944 } 945 946 ret = brcmf_sdiod_freezer_attach(sdiodev); 947 if (ret) 948 goto out; 949 950 /* try to attach to the target device */ 951 sdiodev->bus = brcmf_sdio_probe(sdiodev); 952 if (!sdiodev->bus) { 953 ret = -ENODEV; 954 goto out; 955 } 956 brcmf_sdiod_host_fixup(sdiodev->func2->card->host); 957 out: 958 if (ret) 959 brcmf_sdiod_remove(sdiodev); 960 961 return ret; 962 } 963 964 #define BRCMF_SDIO_DEVICE(dev_id) \ 965 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, dev_id)} 966 967 /* devices we support, null terminated */ 968 static const struct sdio_device_id brcmf_sdmmc_ids[] = { 969 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43143), 970 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43241), 971 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4329), 972 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4330), 973 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4334), 974 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43340), 975 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43341), 976 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43362), 977 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43364), 978 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4335_4339), 979 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4339), 980 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43430), 981 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4345), 982 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_43455), 983 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4354), 984 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_BROADCOM_4356), 985 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_CYPRESS_4373), 986 BRCMF_SDIO_DEVICE(SDIO_DEVICE_ID_CYPRESS_43012), 987 { /* end: all zeroes */ } 988 }; 989 MODULE_DEVICE_TABLE(sdio, brcmf_sdmmc_ids); 990 991 992 static void brcmf_sdiod_acpi_set_power_manageable(struct device *dev, 993 int val) 994 { 995 #if IS_ENABLED(CONFIG_ACPI) 996 struct acpi_device *adev; 997 998 adev = ACPI_COMPANION(dev); 999 if (adev) 1000 adev->flags.power_manageable = 0; 1001 #endif 1002 } 1003 1004 static int brcmf_ops_sdio_probe(struct sdio_func *func, 1005 const struct sdio_device_id *id) 1006 { 1007 int err; 1008 struct brcmf_sdio_dev *sdiodev; 1009 struct brcmf_bus *bus_if; 1010 struct device *dev; 1011 1012 brcmf_dbg(SDIO, "Enter\n"); 1013 brcmf_dbg(SDIO, "Class=%x\n", func->class); 1014 brcmf_dbg(SDIO, "sdio vendor ID: 0x%04x\n", func->vendor); 1015 brcmf_dbg(SDIO, "sdio device ID: 0x%04x\n", func->device); 1016 brcmf_dbg(SDIO, "Function#: %d\n", func->num); 1017 1018 dev = &func->dev; 1019 1020 /* Set MMC_QUIRK_LENIENT_FN0 for this card */ 1021 func->card->quirks |= MMC_QUIRK_LENIENT_FN0; 1022 1023 /* prohibit ACPI power management for this device */ 1024 brcmf_sdiod_acpi_set_power_manageable(dev, 0); 1025 1026 /* Consume func num 1 but dont do anything with it. */ 1027 if (func->num == 1) 1028 return 0; 1029 1030 /* Ignore anything but func 2 */ 1031 if (func->num != 2) 1032 return -ENODEV; 1033 1034 bus_if = kzalloc(sizeof(struct brcmf_bus), GFP_KERNEL); 1035 if (!bus_if) 1036 return -ENOMEM; 1037 sdiodev = kzalloc(sizeof(struct brcmf_sdio_dev), GFP_KERNEL); 1038 if (!sdiodev) { 1039 kfree(bus_if); 1040 return -ENOMEM; 1041 } 1042 1043 /* store refs to functions used. mmc_card does 1044 * not hold the F0 function pointer. 1045 */ 1046 sdiodev->func1 = func->card->sdio_func[0]; 1047 sdiodev->func2 = func; 1048 1049 sdiodev->bus_if = bus_if; 1050 bus_if->bus_priv.sdio = sdiodev; 1051 bus_if->proto_type = BRCMF_PROTO_BCDC; 1052 dev_set_drvdata(&func->dev, bus_if); 1053 dev_set_drvdata(&sdiodev->func1->dev, bus_if); 1054 sdiodev->dev = &sdiodev->func1->dev; 1055 1056 brcmf_sdiod_change_state(sdiodev, BRCMF_SDIOD_DOWN); 1057 1058 brcmf_dbg(SDIO, "F2 found, calling brcmf_sdiod_probe...\n"); 1059 err = brcmf_sdiod_probe(sdiodev); 1060 if (err) { 1061 brcmf_err("F2 error, probe failed %d...\n", err); 1062 goto fail; 1063 } 1064 1065 brcmf_dbg(SDIO, "F2 init completed...\n"); 1066 return 0; 1067 1068 fail: 1069 dev_set_drvdata(&func->dev, NULL); 1070 dev_set_drvdata(&sdiodev->func1->dev, NULL); 1071 kfree(sdiodev); 1072 kfree(bus_if); 1073 return err; 1074 } 1075 1076 static void brcmf_ops_sdio_remove(struct sdio_func *func) 1077 { 1078 struct brcmf_bus *bus_if; 1079 struct brcmf_sdio_dev *sdiodev; 1080 1081 brcmf_dbg(SDIO, "Enter\n"); 1082 brcmf_dbg(SDIO, "sdio vendor ID: 0x%04x\n", func->vendor); 1083 brcmf_dbg(SDIO, "sdio device ID: 0x%04x\n", func->device); 1084 brcmf_dbg(SDIO, "Function: %d\n", func->num); 1085 1086 bus_if = dev_get_drvdata(&func->dev); 1087 if (bus_if) { 1088 sdiodev = bus_if->bus_priv.sdio; 1089 1090 /* start by unregistering irqs */ 1091 brcmf_sdiod_intr_unregister(sdiodev); 1092 1093 if (func->num != 1) 1094 return; 1095 1096 /* only proceed with rest of cleanup if func 1 */ 1097 brcmf_sdiod_remove(sdiodev); 1098 1099 dev_set_drvdata(&sdiodev->func1->dev, NULL); 1100 dev_set_drvdata(&sdiodev->func2->dev, NULL); 1101 1102 kfree(bus_if); 1103 kfree(sdiodev); 1104 } 1105 1106 brcmf_dbg(SDIO, "Exit\n"); 1107 } 1108 1109 void brcmf_sdio_wowl_config(struct device *dev, bool enabled) 1110 { 1111 struct brcmf_bus *bus_if = dev_get_drvdata(dev); 1112 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio; 1113 1114 brcmf_dbg(SDIO, "Configuring WOWL, enabled=%d\n", enabled); 1115 sdiodev->wowl_enabled = enabled; 1116 } 1117 1118 #ifdef CONFIG_PM_SLEEP 1119 static int brcmf_ops_sdio_suspend(struct device *dev) 1120 { 1121 struct sdio_func *func; 1122 struct brcmf_bus *bus_if; 1123 struct brcmf_sdio_dev *sdiodev; 1124 mmc_pm_flag_t sdio_flags; 1125 1126 func = container_of(dev, struct sdio_func, dev); 1127 brcmf_dbg(SDIO, "Enter: F%d\n", func->num); 1128 if (func->num != 1) 1129 return 0; 1130 1131 1132 bus_if = dev_get_drvdata(dev); 1133 sdiodev = bus_if->bus_priv.sdio; 1134 1135 brcmf_sdiod_freezer_on(sdiodev); 1136 brcmf_sdio_wd_timer(sdiodev->bus, 0); 1137 1138 sdio_flags = MMC_PM_KEEP_POWER; 1139 if (sdiodev->wowl_enabled) { 1140 if (sdiodev->settings->bus.sdio.oob_irq_supported) 1141 enable_irq_wake(sdiodev->settings->bus.sdio.oob_irq_nr); 1142 else 1143 sdio_flags |= MMC_PM_WAKE_SDIO_IRQ; 1144 } 1145 if (sdio_set_host_pm_flags(sdiodev->func1, sdio_flags)) 1146 brcmf_err("Failed to set pm_flags %x\n", sdio_flags); 1147 return 0; 1148 } 1149 1150 static int brcmf_ops_sdio_resume(struct device *dev) 1151 { 1152 struct brcmf_bus *bus_if = dev_get_drvdata(dev); 1153 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio; 1154 struct sdio_func *func = container_of(dev, struct sdio_func, dev); 1155 1156 brcmf_dbg(SDIO, "Enter: F%d\n", func->num); 1157 if (func->num != 2) 1158 return 0; 1159 1160 brcmf_sdiod_freezer_off(sdiodev); 1161 return 0; 1162 } 1163 1164 static const struct dev_pm_ops brcmf_sdio_pm_ops = { 1165 .suspend = brcmf_ops_sdio_suspend, 1166 .resume = brcmf_ops_sdio_resume, 1167 }; 1168 #endif /* CONFIG_PM_SLEEP */ 1169 1170 static struct sdio_driver brcmf_sdmmc_driver = { 1171 .probe = brcmf_ops_sdio_probe, 1172 .remove = brcmf_ops_sdio_remove, 1173 .name = KBUILD_MODNAME, 1174 .id_table = brcmf_sdmmc_ids, 1175 .drv = { 1176 .owner = THIS_MODULE, 1177 #ifdef CONFIG_PM_SLEEP 1178 .pm = &brcmf_sdio_pm_ops, 1179 #endif /* CONFIG_PM_SLEEP */ 1180 .coredump = brcmf_dev_coredump, 1181 }, 1182 }; 1183 1184 void brcmf_sdio_register(void) 1185 { 1186 int ret; 1187 1188 ret = sdio_register_driver(&brcmf_sdmmc_driver); 1189 if (ret) 1190 brcmf_err("sdio_register_driver failed: %d\n", ret); 1191 } 1192 1193 void brcmf_sdio_exit(void) 1194 { 1195 brcmf_dbg(SDIO, "Enter\n"); 1196 1197 sdio_unregister_driver(&brcmf_sdmmc_driver); 1198 } 1199 1200