1 /* 2 * linux/drivers/net/wireless/libertas/if_sdio.c 3 * 4 * Copyright 2007-2008 Pierre Ossman 5 * 6 * Inspired by if_cs.c, Copyright 2007 Holger Schurig 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or (at 11 * your option) any later version. 12 * 13 * This hardware has more or less no CMD53 support, so all registers 14 * must be accessed using sdio_readb()/sdio_writeb(). 15 * 16 * Transfers must be in one transaction or the firmware goes bonkers. 17 * This means that the transfer must either be small enough to do a 18 * byte based transfer or it must be padded to a multiple of the 19 * current block size. 20 * 21 * As SDIO is still new to the kernel, it is unfortunately common with 22 * bugs in the host controllers related to that. One such bug is that 23 * controllers cannot do transfers that aren't a multiple of 4 bytes. 24 * If you don't have time to fix the host controller driver, you can 25 * work around the problem by modifying if_sdio_host_to_card() and 26 * if_sdio_card_to_host() to pad the data. 27 */ 28 29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 30 31 #include <linux/kernel.h> 32 #include <linux/module.h> 33 #include <linux/slab.h> 34 #include <linux/firmware.h> 35 #include <linux/netdevice.h> 36 #include <linux/delay.h> 37 #include <linux/mmc/card.h> 38 #include <linux/mmc/sdio_func.h> 39 #include <linux/mmc/sdio_ids.h> 40 #include <linux/mmc/sdio.h> 41 #include <linux/mmc/host.h> 42 #include <linux/pm_runtime.h> 43 44 #include "host.h" 45 #include "decl.h" 46 #include "defs.h" 47 #include "dev.h" 48 #include "cmd.h" 49 #include "if_sdio.h" 50 51 static void if_sdio_interrupt(struct sdio_func *func); 52 53 /* The if_sdio_remove() callback function is called when 54 * user removes this module from kernel space or ejects 55 * the card from the slot. The driver handles these 2 cases 56 * differently for SD8688 combo chip. 57 * If the user is removing the module, the FUNC_SHUTDOWN 58 * command for SD8688 is sent to the firmware. 59 * If the card is removed, there is no need to send this command. 60 * 61 * The variable 'user_rmmod' is used to distinguish these two 62 * scenarios. This flag is initialized as FALSE in case the card 63 * is removed, and will be set to TRUE for module removal when 64 * module_exit function is called. 65 */ 66 static u8 user_rmmod; 67 68 static const struct sdio_device_id if_sdio_ids[] = { 69 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 70 SDIO_DEVICE_ID_MARVELL_LIBERTAS) }, 71 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 72 SDIO_DEVICE_ID_MARVELL_8688WLAN) }, 73 { /* end: all zeroes */ }, 74 }; 75 76 MODULE_DEVICE_TABLE(sdio, if_sdio_ids); 77 78 #define MODEL_8385 0x04 79 #define MODEL_8686 0x0b 80 #define MODEL_8688 0x10 81 82 static const struct lbs_fw_table fw_table[] = { 83 { MODEL_8385, "libertas/sd8385_helper.bin", "libertas/sd8385.bin" }, 84 { MODEL_8385, "sd8385_helper.bin", "sd8385.bin" }, 85 { MODEL_8686, "libertas/sd8686_v9_helper.bin", "libertas/sd8686_v9.bin" }, 86 { MODEL_8686, "libertas/sd8686_v8_helper.bin", "libertas/sd8686_v8.bin" }, 87 { MODEL_8686, "sd8686_helper.bin", "sd8686.bin" }, 88 { MODEL_8688, "libertas/sd8688_helper.bin", "libertas/sd8688.bin" }, 89 { MODEL_8688, "sd8688_helper.bin", "sd8688.bin" }, 90 { 0, NULL, NULL } 91 }; 92 MODULE_FIRMWARE("libertas/sd8385_helper.bin"); 93 MODULE_FIRMWARE("libertas/sd8385.bin"); 94 MODULE_FIRMWARE("sd8385_helper.bin"); 95 MODULE_FIRMWARE("sd8385.bin"); 96 MODULE_FIRMWARE("libertas/sd8686_v9_helper.bin"); 97 MODULE_FIRMWARE("libertas/sd8686_v9.bin"); 98 MODULE_FIRMWARE("libertas/sd8686_v8_helper.bin"); 99 MODULE_FIRMWARE("libertas/sd8686_v8.bin"); 100 MODULE_FIRMWARE("sd8686_helper.bin"); 101 MODULE_FIRMWARE("sd8686.bin"); 102 MODULE_FIRMWARE("libertas/sd8688_helper.bin"); 103 MODULE_FIRMWARE("libertas/sd8688.bin"); 104 MODULE_FIRMWARE("sd8688_helper.bin"); 105 MODULE_FIRMWARE("sd8688.bin"); 106 107 struct if_sdio_packet { 108 struct if_sdio_packet *next; 109 u16 nb; 110 u8 buffer[0] __attribute__((aligned(4))); 111 }; 112 113 struct if_sdio_card { 114 struct sdio_func *func; 115 struct lbs_private *priv; 116 117 int model; 118 unsigned long ioport; 119 unsigned int scratch_reg; 120 bool started; 121 wait_queue_head_t pwron_waitq; 122 123 u8 buffer[65536] __attribute__((aligned(4))); 124 125 spinlock_t lock; 126 struct if_sdio_packet *packets; 127 128 struct workqueue_struct *workqueue; 129 struct work_struct packet_worker; 130 131 u8 rx_unit; 132 }; 133 134 static void if_sdio_finish_power_on(struct if_sdio_card *card); 135 static int if_sdio_power_off(struct if_sdio_card *card); 136 137 /********************************************************************/ 138 /* I/O */ 139 /********************************************************************/ 140 141 /* 142 * For SD8385/SD8686, this function reads firmware status after 143 * the image is downloaded, or reads RX packet length when 144 * interrupt (with IF_SDIO_H_INT_UPLD bit set) is received. 145 * For SD8688, this function reads firmware status only. 146 */ 147 static u16 if_sdio_read_scratch(struct if_sdio_card *card, int *err) 148 { 149 int ret; 150 u16 scratch; 151 152 scratch = sdio_readb(card->func, card->scratch_reg, &ret); 153 if (!ret) 154 scratch |= sdio_readb(card->func, card->scratch_reg + 1, 155 &ret) << 8; 156 157 if (err) 158 *err = ret; 159 160 if (ret) 161 return 0xffff; 162 163 return scratch; 164 } 165 166 static u8 if_sdio_read_rx_unit(struct if_sdio_card *card) 167 { 168 int ret; 169 u8 rx_unit; 170 171 rx_unit = sdio_readb(card->func, IF_SDIO_RX_UNIT, &ret); 172 173 if (ret) 174 rx_unit = 0; 175 176 return rx_unit; 177 } 178 179 static u16 if_sdio_read_rx_len(struct if_sdio_card *card, int *err) 180 { 181 int ret; 182 u16 rx_len; 183 184 switch (card->model) { 185 case MODEL_8385: 186 case MODEL_8686: 187 rx_len = if_sdio_read_scratch(card, &ret); 188 break; 189 case MODEL_8688: 190 default: /* for newer chipsets */ 191 rx_len = sdio_readb(card->func, IF_SDIO_RX_LEN, &ret); 192 if (!ret) 193 rx_len <<= card->rx_unit; 194 else 195 rx_len = 0xffff; /* invalid length */ 196 197 break; 198 } 199 200 if (err) 201 *err = ret; 202 203 return rx_len; 204 } 205 206 static int if_sdio_handle_cmd(struct if_sdio_card *card, 207 u8 *buffer, unsigned size) 208 { 209 struct lbs_private *priv = card->priv; 210 int ret; 211 unsigned long flags; 212 u8 i; 213 214 if (size > LBS_CMD_BUFFER_SIZE) { 215 lbs_deb_sdio("response packet too large (%d bytes)\n", 216 (int)size); 217 ret = -E2BIG; 218 goto out; 219 } 220 221 spin_lock_irqsave(&priv->driver_lock, flags); 222 223 i = (priv->resp_idx == 0) ? 1 : 0; 224 BUG_ON(priv->resp_len[i]); 225 priv->resp_len[i] = size; 226 memcpy(priv->resp_buf[i], buffer, size); 227 lbs_notify_command_response(priv, i); 228 229 spin_unlock_irqrestore(&priv->driver_lock, flags); 230 231 ret = 0; 232 233 out: 234 return ret; 235 } 236 237 static int if_sdio_handle_data(struct if_sdio_card *card, 238 u8 *buffer, unsigned size) 239 { 240 int ret; 241 struct sk_buff *skb; 242 243 if (size > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) { 244 lbs_deb_sdio("response packet too large (%d bytes)\n", 245 (int)size); 246 ret = -E2BIG; 247 goto out; 248 } 249 250 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + NET_IP_ALIGN); 251 if (!skb) { 252 ret = -ENOMEM; 253 goto out; 254 } 255 256 skb_reserve(skb, NET_IP_ALIGN); 257 258 skb_put_data(skb, buffer, size); 259 260 lbs_process_rxed_packet(card->priv, skb); 261 262 ret = 0; 263 264 out: 265 return ret; 266 } 267 268 static int if_sdio_handle_event(struct if_sdio_card *card, 269 u8 *buffer, unsigned size) 270 { 271 int ret; 272 u32 event; 273 274 if (card->model == MODEL_8385) { 275 event = sdio_readb(card->func, IF_SDIO_EVENT, &ret); 276 if (ret) 277 goto out; 278 279 /* right shift 3 bits to get the event id */ 280 event >>= 3; 281 } else { 282 if (size < 4) { 283 lbs_deb_sdio("event packet too small (%d bytes)\n", 284 (int)size); 285 ret = -EINVAL; 286 goto out; 287 } 288 event = buffer[3] << 24; 289 event |= buffer[2] << 16; 290 event |= buffer[1] << 8; 291 event |= buffer[0] << 0; 292 } 293 294 lbs_queue_event(card->priv, event & 0xFF); 295 ret = 0; 296 297 out: 298 return ret; 299 } 300 301 static int if_sdio_wait_status(struct if_sdio_card *card, const u8 condition) 302 { 303 u8 status; 304 unsigned long timeout; 305 int ret = 0; 306 307 timeout = jiffies + HZ; 308 while (1) { 309 status = sdio_readb(card->func, IF_SDIO_STATUS, &ret); 310 if (ret) 311 return ret; 312 if ((status & condition) == condition) 313 break; 314 if (time_after(jiffies, timeout)) 315 return -ETIMEDOUT; 316 mdelay(1); 317 } 318 return ret; 319 } 320 321 static int if_sdio_card_to_host(struct if_sdio_card *card) 322 { 323 int ret; 324 u16 size, type, chunk; 325 326 size = if_sdio_read_rx_len(card, &ret); 327 if (ret) 328 goto out; 329 330 if (size < 4) { 331 lbs_deb_sdio("invalid packet size (%d bytes) from firmware\n", 332 (int)size); 333 ret = -EINVAL; 334 goto out; 335 } 336 337 ret = if_sdio_wait_status(card, IF_SDIO_IO_RDY); 338 if (ret) 339 goto out; 340 341 /* 342 * The transfer must be in one transaction or the firmware 343 * goes suicidal. There's no way to guarantee that for all 344 * controllers, but we can at least try. 345 */ 346 chunk = sdio_align_size(card->func, size); 347 348 ret = sdio_readsb(card->func, card->buffer, card->ioport, chunk); 349 if (ret) 350 goto out; 351 352 chunk = card->buffer[0] | (card->buffer[1] << 8); 353 type = card->buffer[2] | (card->buffer[3] << 8); 354 355 lbs_deb_sdio("packet of type %d and size %d bytes\n", 356 (int)type, (int)chunk); 357 358 if (chunk > size) { 359 lbs_deb_sdio("packet fragment (%d > %d)\n", 360 (int)chunk, (int)size); 361 ret = -EINVAL; 362 goto out; 363 } 364 365 if (chunk < size) { 366 lbs_deb_sdio("packet fragment (%d < %d)\n", 367 (int)chunk, (int)size); 368 } 369 370 switch (type) { 371 case MVMS_CMD: 372 ret = if_sdio_handle_cmd(card, card->buffer + 4, chunk - 4); 373 if (ret) 374 goto out; 375 break; 376 case MVMS_DAT: 377 ret = if_sdio_handle_data(card, card->buffer + 4, chunk - 4); 378 if (ret) 379 goto out; 380 break; 381 case MVMS_EVENT: 382 ret = if_sdio_handle_event(card, card->buffer + 4, chunk - 4); 383 if (ret) 384 goto out; 385 break; 386 default: 387 lbs_deb_sdio("invalid type (%d) from firmware\n", 388 (int)type); 389 ret = -EINVAL; 390 goto out; 391 } 392 393 out: 394 if (ret) 395 pr_err("problem fetching packet from firmware\n"); 396 397 return ret; 398 } 399 400 static void if_sdio_host_to_card_worker(struct work_struct *work) 401 { 402 struct if_sdio_card *card; 403 struct if_sdio_packet *packet; 404 int ret; 405 unsigned long flags; 406 407 card = container_of(work, struct if_sdio_card, packet_worker); 408 409 while (1) { 410 spin_lock_irqsave(&card->lock, flags); 411 packet = card->packets; 412 if (packet) 413 card->packets = packet->next; 414 spin_unlock_irqrestore(&card->lock, flags); 415 416 if (!packet) 417 break; 418 419 sdio_claim_host(card->func); 420 421 ret = if_sdio_wait_status(card, IF_SDIO_IO_RDY); 422 if (ret == 0) { 423 ret = sdio_writesb(card->func, card->ioport, 424 packet->buffer, packet->nb); 425 } 426 427 if (ret) 428 pr_err("error %d sending packet to firmware\n", ret); 429 430 sdio_release_host(card->func); 431 432 kfree(packet); 433 } 434 } 435 436 /********************************************************************/ 437 /* Firmware */ 438 /********************************************************************/ 439 440 #define FW_DL_READY_STATUS (IF_SDIO_IO_RDY | IF_SDIO_DL_RDY) 441 442 static int if_sdio_prog_helper(struct if_sdio_card *card, 443 const struct firmware *fw) 444 { 445 int ret; 446 unsigned long timeout; 447 u8 *chunk_buffer; 448 u32 chunk_size; 449 const u8 *firmware; 450 size_t size; 451 452 chunk_buffer = kzalloc(64, GFP_KERNEL); 453 if (!chunk_buffer) { 454 ret = -ENOMEM; 455 goto out; 456 } 457 458 sdio_claim_host(card->func); 459 460 ret = sdio_set_block_size(card->func, 32); 461 if (ret) 462 goto release; 463 464 firmware = fw->data; 465 size = fw->size; 466 467 while (size) { 468 ret = if_sdio_wait_status(card, FW_DL_READY_STATUS); 469 if (ret) 470 goto release; 471 472 /* On some platforms (like Davinci) the chip needs more time 473 * between helper blocks. 474 */ 475 mdelay(2); 476 477 chunk_size = min_t(size_t, size, 60); 478 479 *((__le32*)chunk_buffer) = cpu_to_le32(chunk_size); 480 memcpy(chunk_buffer + 4, firmware, chunk_size); 481 /* 482 lbs_deb_sdio("sending %d bytes chunk\n", chunk_size); 483 */ 484 ret = sdio_writesb(card->func, card->ioport, 485 chunk_buffer, 64); 486 if (ret) 487 goto release; 488 489 firmware += chunk_size; 490 size -= chunk_size; 491 } 492 493 /* an empty block marks the end of the transfer */ 494 memset(chunk_buffer, 0, 4); 495 ret = sdio_writesb(card->func, card->ioport, chunk_buffer, 64); 496 if (ret) 497 goto release; 498 499 lbs_deb_sdio("waiting for helper to boot...\n"); 500 501 /* wait for the helper to boot by looking at the size register */ 502 timeout = jiffies + HZ; 503 while (1) { 504 u16 req_size; 505 506 req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret); 507 if (ret) 508 goto release; 509 510 req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8; 511 if (ret) 512 goto release; 513 514 if (req_size != 0) 515 break; 516 517 if (time_after(jiffies, timeout)) { 518 ret = -ETIMEDOUT; 519 goto release; 520 } 521 522 msleep(10); 523 } 524 525 ret = 0; 526 527 release: 528 sdio_release_host(card->func); 529 kfree(chunk_buffer); 530 531 out: 532 if (ret) 533 pr_err("failed to load helper firmware\n"); 534 535 return ret; 536 } 537 538 static int if_sdio_prog_real(struct if_sdio_card *card, 539 const struct firmware *fw) 540 { 541 int ret; 542 unsigned long timeout; 543 u8 *chunk_buffer; 544 u32 chunk_size; 545 const u8 *firmware; 546 size_t size, req_size; 547 548 chunk_buffer = kzalloc(512, GFP_KERNEL); 549 if (!chunk_buffer) { 550 ret = -ENOMEM; 551 goto out; 552 } 553 554 sdio_claim_host(card->func); 555 556 ret = sdio_set_block_size(card->func, 32); 557 if (ret) 558 goto release; 559 560 firmware = fw->data; 561 size = fw->size; 562 563 while (size) { 564 timeout = jiffies + HZ; 565 while (1) { 566 ret = if_sdio_wait_status(card, FW_DL_READY_STATUS); 567 if (ret) 568 goto release; 569 570 req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, 571 &ret); 572 if (ret) 573 goto release; 574 575 req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, 576 &ret) << 8; 577 if (ret) 578 goto release; 579 580 /* 581 * For SD8688 wait until the length is not 0, 1 or 2 582 * before downloading the first FW block, 583 * since BOOT code writes the register to indicate the 584 * helper/FW download winner, 585 * the value could be 1 or 2 (Func1 or Func2). 586 */ 587 if ((size != fw->size) || (req_size > 2)) 588 break; 589 if (time_after(jiffies, timeout)) { 590 ret = -ETIMEDOUT; 591 goto release; 592 } 593 mdelay(1); 594 } 595 596 /* 597 lbs_deb_sdio("firmware wants %d bytes\n", (int)req_size); 598 */ 599 if (req_size == 0) { 600 lbs_deb_sdio("firmware helper gave up early\n"); 601 ret = -EIO; 602 goto release; 603 } 604 605 if (req_size & 0x01) { 606 lbs_deb_sdio("firmware helper signalled error\n"); 607 ret = -EIO; 608 goto release; 609 } 610 611 if (req_size > size) 612 req_size = size; 613 614 while (req_size) { 615 chunk_size = min_t(size_t, req_size, 512); 616 617 memcpy(chunk_buffer, firmware, chunk_size); 618 /* 619 lbs_deb_sdio("sending %d bytes (%d bytes) chunk\n", 620 chunk_size, (chunk_size + 31) / 32 * 32); 621 */ 622 ret = sdio_writesb(card->func, card->ioport, 623 chunk_buffer, roundup(chunk_size, 32)); 624 if (ret) 625 goto release; 626 627 firmware += chunk_size; 628 size -= chunk_size; 629 req_size -= chunk_size; 630 } 631 } 632 633 ret = 0; 634 635 lbs_deb_sdio("waiting for firmware to boot...\n"); 636 637 /* wait for the firmware to boot */ 638 timeout = jiffies + HZ; 639 while (1) { 640 u16 scratch; 641 642 scratch = if_sdio_read_scratch(card, &ret); 643 if (ret) 644 goto release; 645 646 if (scratch == IF_SDIO_FIRMWARE_OK) 647 break; 648 649 if (time_after(jiffies, timeout)) { 650 ret = -ETIMEDOUT; 651 goto release; 652 } 653 654 msleep(10); 655 } 656 657 ret = 0; 658 659 release: 660 sdio_release_host(card->func); 661 kfree(chunk_buffer); 662 663 out: 664 if (ret) 665 pr_err("failed to load firmware\n"); 666 667 return ret; 668 } 669 670 static void if_sdio_do_prog_firmware(struct lbs_private *priv, int ret, 671 const struct firmware *helper, 672 const struct firmware *mainfw) 673 { 674 struct if_sdio_card *card = priv->card; 675 676 if (ret) { 677 pr_err("failed to find firmware (%d)\n", ret); 678 return; 679 } 680 681 ret = if_sdio_prog_helper(card, helper); 682 if (ret) 683 return; 684 685 lbs_deb_sdio("Helper firmware loaded\n"); 686 687 ret = if_sdio_prog_real(card, mainfw); 688 if (ret) 689 return; 690 691 lbs_deb_sdio("Firmware loaded\n"); 692 if_sdio_finish_power_on(card); 693 } 694 695 static int if_sdio_prog_firmware(struct if_sdio_card *card) 696 { 697 int ret; 698 u16 scratch; 699 700 /* 701 * Disable interrupts 702 */ 703 sdio_claim_host(card->func); 704 sdio_writeb(card->func, 0x00, IF_SDIO_H_INT_MASK, &ret); 705 sdio_release_host(card->func); 706 707 sdio_claim_host(card->func); 708 scratch = if_sdio_read_scratch(card, &ret); 709 sdio_release_host(card->func); 710 711 lbs_deb_sdio("firmware status = %#x\n", scratch); 712 lbs_deb_sdio("scratch ret = %d\n", ret); 713 714 if (ret) 715 goto out; 716 717 718 /* 719 * The manual clearly describes that FEDC is the right code to use 720 * to detect firmware presence, but for SD8686 it is not that simple. 721 * Scratch is also used to store the RX packet length, so we lose 722 * the FEDC value early on. So we use a non-zero check in order 723 * to validate firmware presence. 724 * Additionally, the SD8686 in the Gumstix always has the high scratch 725 * bit set, even when the firmware is not loaded. So we have to 726 * exclude that from the test. 727 */ 728 if (scratch == IF_SDIO_FIRMWARE_OK) { 729 lbs_deb_sdio("firmware already loaded\n"); 730 if_sdio_finish_power_on(card); 731 return 0; 732 } else if ((card->model == MODEL_8686) && (scratch & 0x7fff)) { 733 lbs_deb_sdio("firmware may be running\n"); 734 if_sdio_finish_power_on(card); 735 return 0; 736 } 737 738 ret = lbs_get_firmware_async(card->priv, &card->func->dev, card->model, 739 fw_table, if_sdio_do_prog_firmware); 740 741 out: 742 return ret; 743 } 744 745 /********************************************************************/ 746 /* Power management */ 747 /********************************************************************/ 748 749 /* Finish power on sequence (after firmware is loaded) */ 750 static void if_sdio_finish_power_on(struct if_sdio_card *card) 751 { 752 struct sdio_func *func = card->func; 753 struct lbs_private *priv = card->priv; 754 int ret; 755 756 sdio_claim_host(func); 757 sdio_set_block_size(card->func, IF_SDIO_BLOCK_SIZE); 758 759 /* 760 * Get rx_unit if the chip is SD8688 or newer. 761 * SD8385 & SD8686 do not have rx_unit. 762 */ 763 if ((card->model != MODEL_8385) 764 && (card->model != MODEL_8686)) 765 card->rx_unit = if_sdio_read_rx_unit(card); 766 else 767 card->rx_unit = 0; 768 769 /* 770 * Set up the interrupt handler late. 771 * 772 * If we set it up earlier, the (buggy) hardware generates a spurious 773 * interrupt, even before the interrupt has been enabled, with 774 * CCCR_INTx = 0. 775 * 776 * We register the interrupt handler late so that we can handle any 777 * spurious interrupts, and also to avoid generation of that known 778 * spurious interrupt in the first place. 779 */ 780 ret = sdio_claim_irq(func, if_sdio_interrupt); 781 if (ret) 782 goto release; 783 784 /* 785 * Enable interrupts now that everything is set up 786 */ 787 sdio_writeb(func, 0x0f, IF_SDIO_H_INT_MASK, &ret); 788 if (ret) 789 goto release_irq; 790 791 sdio_release_host(func); 792 793 /* Set fw_ready before queuing any commands so that 794 * lbs_thread won't block from sending them to firmware. 795 */ 796 priv->fw_ready = 1; 797 798 /* 799 * FUNC_INIT is required for SD8688 WLAN/BT multiple functions 800 */ 801 if (card->model == MODEL_8688) { 802 struct cmd_header cmd; 803 804 memset(&cmd, 0, sizeof(cmd)); 805 806 lbs_deb_sdio("send function INIT command\n"); 807 if (__lbs_cmd(priv, CMD_FUNC_INIT, &cmd, sizeof(cmd), 808 lbs_cmd_copyback, (unsigned long) &cmd)) 809 netdev_alert(priv->dev, "CMD_FUNC_INIT cmd failed\n"); 810 } 811 812 wake_up(&card->pwron_waitq); 813 814 if (!card->started) { 815 ret = lbs_start_card(priv); 816 if_sdio_power_off(card); 817 if (ret == 0) { 818 card->started = true; 819 /* Tell PM core that we don't need the card to be 820 * powered now */ 821 pm_runtime_put(&func->dev); 822 } 823 } 824 825 return; 826 827 release_irq: 828 sdio_release_irq(func); 829 release: 830 sdio_release_host(func); 831 } 832 833 static int if_sdio_power_on(struct if_sdio_card *card) 834 { 835 struct sdio_func *func = card->func; 836 struct mmc_host *host = func->card->host; 837 int ret; 838 839 sdio_claim_host(func); 840 841 ret = sdio_enable_func(func); 842 if (ret) 843 goto release; 844 845 /* For 1-bit transfers to the 8686 model, we need to enable the 846 * interrupt flag in the CCCR register. Set the MMC_QUIRK_LENIENT_FN0 847 * bit to allow access to non-vendor registers. */ 848 if ((card->model == MODEL_8686) && 849 (host->caps & MMC_CAP_SDIO_IRQ) && 850 (host->ios.bus_width == MMC_BUS_WIDTH_1)) { 851 u8 reg; 852 853 func->card->quirks |= MMC_QUIRK_LENIENT_FN0; 854 reg = sdio_f0_readb(func, SDIO_CCCR_IF, &ret); 855 if (ret) 856 goto disable; 857 858 reg |= SDIO_BUS_ECSI; 859 sdio_f0_writeb(func, reg, SDIO_CCCR_IF, &ret); 860 if (ret) 861 goto disable; 862 } 863 864 card->ioport = sdio_readb(func, IF_SDIO_IOPORT, &ret); 865 if (ret) 866 goto disable; 867 868 card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 1, &ret) << 8; 869 if (ret) 870 goto disable; 871 872 card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 2, &ret) << 16; 873 if (ret) 874 goto disable; 875 876 sdio_release_host(func); 877 ret = if_sdio_prog_firmware(card); 878 if (ret) { 879 sdio_claim_host(func); 880 goto disable; 881 } 882 883 return 0; 884 885 disable: 886 sdio_disable_func(func); 887 release: 888 sdio_release_host(func); 889 return ret; 890 } 891 892 static int if_sdio_power_off(struct if_sdio_card *card) 893 { 894 struct sdio_func *func = card->func; 895 struct lbs_private *priv = card->priv; 896 897 priv->fw_ready = 0; 898 899 sdio_claim_host(func); 900 sdio_release_irq(func); 901 sdio_disable_func(func); 902 sdio_release_host(func); 903 return 0; 904 } 905 906 907 /*******************************************************************/ 908 /* Libertas callbacks */ 909 /*******************************************************************/ 910 911 static int if_sdio_host_to_card(struct lbs_private *priv, 912 u8 type, u8 *buf, u16 nb) 913 { 914 int ret; 915 struct if_sdio_card *card; 916 struct if_sdio_packet *packet, *cur; 917 u16 size; 918 unsigned long flags; 919 920 card = priv->card; 921 922 if (nb > (65536 - sizeof(struct if_sdio_packet) - 4)) { 923 ret = -EINVAL; 924 goto out; 925 } 926 927 /* 928 * The transfer must be in one transaction or the firmware 929 * goes suicidal. There's no way to guarantee that for all 930 * controllers, but we can at least try. 931 */ 932 size = sdio_align_size(card->func, nb + 4); 933 934 packet = kzalloc(sizeof(struct if_sdio_packet) + size, 935 GFP_ATOMIC); 936 if (!packet) { 937 ret = -ENOMEM; 938 goto out; 939 } 940 941 packet->next = NULL; 942 packet->nb = size; 943 944 /* 945 * SDIO specific header. 946 */ 947 packet->buffer[0] = (nb + 4) & 0xff; 948 packet->buffer[1] = ((nb + 4) >> 8) & 0xff; 949 packet->buffer[2] = type; 950 packet->buffer[3] = 0; 951 952 memcpy(packet->buffer + 4, buf, nb); 953 954 spin_lock_irqsave(&card->lock, flags); 955 956 if (!card->packets) 957 card->packets = packet; 958 else { 959 cur = card->packets; 960 while (cur->next) 961 cur = cur->next; 962 cur->next = packet; 963 } 964 965 switch (type) { 966 case MVMS_CMD: 967 priv->dnld_sent = DNLD_CMD_SENT; 968 break; 969 case MVMS_DAT: 970 priv->dnld_sent = DNLD_DATA_SENT; 971 break; 972 default: 973 lbs_deb_sdio("unknown packet type %d\n", (int)type); 974 } 975 976 spin_unlock_irqrestore(&card->lock, flags); 977 978 queue_work(card->workqueue, &card->packet_worker); 979 980 ret = 0; 981 982 out: 983 return ret; 984 } 985 986 static int if_sdio_enter_deep_sleep(struct lbs_private *priv) 987 { 988 int ret = -1; 989 struct cmd_header cmd; 990 991 memset(&cmd, 0, sizeof(cmd)); 992 993 lbs_deb_sdio("send DEEP_SLEEP command\n"); 994 ret = __lbs_cmd(priv, CMD_802_11_DEEP_SLEEP, &cmd, sizeof(cmd), 995 lbs_cmd_copyback, (unsigned long) &cmd); 996 if (ret) 997 netdev_err(priv->dev, "DEEP_SLEEP cmd failed\n"); 998 999 mdelay(200); 1000 return ret; 1001 } 1002 1003 static int if_sdio_exit_deep_sleep(struct lbs_private *priv) 1004 { 1005 struct if_sdio_card *card = priv->card; 1006 int ret = -1; 1007 1008 sdio_claim_host(card->func); 1009 1010 sdio_writeb(card->func, HOST_POWER_UP, CONFIGURATION_REG, &ret); 1011 if (ret) 1012 netdev_err(priv->dev, "sdio_writeb failed!\n"); 1013 1014 sdio_release_host(card->func); 1015 1016 return ret; 1017 } 1018 1019 static int if_sdio_reset_deep_sleep_wakeup(struct lbs_private *priv) 1020 { 1021 struct if_sdio_card *card = priv->card; 1022 int ret = -1; 1023 1024 sdio_claim_host(card->func); 1025 1026 sdio_writeb(card->func, 0, CONFIGURATION_REG, &ret); 1027 if (ret) 1028 netdev_err(priv->dev, "sdio_writeb failed!\n"); 1029 1030 sdio_release_host(card->func); 1031 1032 return ret; 1033 1034 } 1035 1036 static struct mmc_host *reset_host; 1037 1038 static void if_sdio_reset_card_worker(struct work_struct *work) 1039 { 1040 /* 1041 * The actual reset operation must be run outside of lbs_thread. This 1042 * is because mmc_remove_host() will cause the device to be instantly 1043 * destroyed, and the libertas driver then needs to end lbs_thread, 1044 * leading to a deadlock. 1045 * 1046 * We run it in a workqueue totally independent from the if_sdio_card 1047 * instance for that reason. 1048 */ 1049 1050 pr_info("Resetting card..."); 1051 mmc_remove_host(reset_host); 1052 mmc_add_host(reset_host); 1053 } 1054 static DECLARE_WORK(card_reset_work, if_sdio_reset_card_worker); 1055 1056 static void if_sdio_reset_card(struct lbs_private *priv) 1057 { 1058 struct if_sdio_card *card = priv->card; 1059 1060 if (work_pending(&card_reset_work)) 1061 return; 1062 1063 reset_host = card->func->card->host; 1064 schedule_work(&card_reset_work); 1065 } 1066 1067 static int if_sdio_power_save(struct lbs_private *priv) 1068 { 1069 struct if_sdio_card *card = priv->card; 1070 int ret; 1071 1072 flush_workqueue(card->workqueue); 1073 1074 ret = if_sdio_power_off(card); 1075 1076 /* Let runtime PM know the card is powered off */ 1077 pm_runtime_put_sync(&card->func->dev); 1078 1079 return ret; 1080 } 1081 1082 static int if_sdio_power_restore(struct lbs_private *priv) 1083 { 1084 struct if_sdio_card *card = priv->card; 1085 int r; 1086 1087 /* Make sure the card will not be powered off by runtime PM */ 1088 pm_runtime_get_sync(&card->func->dev); 1089 1090 r = if_sdio_power_on(card); 1091 if (r) 1092 return r; 1093 1094 wait_event(card->pwron_waitq, priv->fw_ready); 1095 return 0; 1096 } 1097 1098 1099 /*******************************************************************/ 1100 /* SDIO callbacks */ 1101 /*******************************************************************/ 1102 1103 static void if_sdio_interrupt(struct sdio_func *func) 1104 { 1105 int ret; 1106 struct if_sdio_card *card; 1107 u8 cause; 1108 1109 card = sdio_get_drvdata(func); 1110 1111 cause = sdio_readb(card->func, IF_SDIO_H_INT_STATUS, &ret); 1112 if (ret || !cause) 1113 return; 1114 1115 lbs_deb_sdio("interrupt: 0x%X\n", (unsigned)cause); 1116 1117 sdio_writeb(card->func, ~cause, IF_SDIO_H_INT_STATUS, &ret); 1118 if (ret) 1119 return; 1120 1121 /* 1122 * Ignore the define name, this really means the card has 1123 * successfully received the command. 1124 */ 1125 card->priv->is_activity_detected = 1; 1126 if (cause & IF_SDIO_H_INT_DNLD) 1127 lbs_host_to_card_done(card->priv); 1128 1129 1130 if (cause & IF_SDIO_H_INT_UPLD) { 1131 ret = if_sdio_card_to_host(card); 1132 if (ret) 1133 return; 1134 } 1135 } 1136 1137 static int if_sdio_probe(struct sdio_func *func, 1138 const struct sdio_device_id *id) 1139 { 1140 struct if_sdio_card *card; 1141 struct lbs_private *priv; 1142 int ret, i; 1143 unsigned int model; 1144 struct if_sdio_packet *packet; 1145 1146 for (i = 0;i < func->card->num_info;i++) { 1147 if (sscanf(func->card->info[i], 1148 "802.11 SDIO ID: %x", &model) == 1) 1149 break; 1150 if (sscanf(func->card->info[i], 1151 "ID: %x", &model) == 1) 1152 break; 1153 if (!strcmp(func->card->info[i], "IBIS Wireless SDIO Card")) { 1154 model = MODEL_8385; 1155 break; 1156 } 1157 } 1158 1159 if (i == func->card->num_info) { 1160 pr_err("unable to identify card model\n"); 1161 return -ENODEV; 1162 } 1163 1164 card = kzalloc(sizeof(struct if_sdio_card), GFP_KERNEL); 1165 if (!card) 1166 return -ENOMEM; 1167 1168 card->func = func; 1169 card->model = model; 1170 1171 switch (card->model) { 1172 case MODEL_8385: 1173 card->scratch_reg = IF_SDIO_SCRATCH_OLD; 1174 break; 1175 case MODEL_8686: 1176 card->scratch_reg = IF_SDIO_SCRATCH; 1177 break; 1178 case MODEL_8688: 1179 default: /* for newer chipsets */ 1180 card->scratch_reg = IF_SDIO_FW_STATUS; 1181 break; 1182 } 1183 1184 spin_lock_init(&card->lock); 1185 card->workqueue = alloc_workqueue("libertas_sdio", WQ_MEM_RECLAIM, 0); 1186 INIT_WORK(&card->packet_worker, if_sdio_host_to_card_worker); 1187 init_waitqueue_head(&card->pwron_waitq); 1188 1189 /* Check if we support this card */ 1190 for (i = 0; i < ARRAY_SIZE(fw_table); i++) { 1191 if (card->model == fw_table[i].model) 1192 break; 1193 } 1194 if (i == ARRAY_SIZE(fw_table)) { 1195 pr_err("unknown card model 0x%x\n", card->model); 1196 ret = -ENODEV; 1197 goto free; 1198 } 1199 1200 sdio_set_drvdata(func, card); 1201 1202 lbs_deb_sdio("class = 0x%X, vendor = 0x%X, " 1203 "device = 0x%X, model = 0x%X, ioport = 0x%X\n", 1204 func->class, func->vendor, func->device, 1205 model, (unsigned)card->ioport); 1206 1207 1208 priv = lbs_add_card(card, &func->dev); 1209 if (!priv) { 1210 ret = -ENOMEM; 1211 goto free; 1212 } 1213 1214 card->priv = priv; 1215 1216 priv->card = card; 1217 priv->hw_host_to_card = if_sdio_host_to_card; 1218 priv->enter_deep_sleep = if_sdio_enter_deep_sleep; 1219 priv->exit_deep_sleep = if_sdio_exit_deep_sleep; 1220 priv->reset_deep_sleep_wakeup = if_sdio_reset_deep_sleep_wakeup; 1221 priv->reset_card = if_sdio_reset_card; 1222 priv->power_save = if_sdio_power_save; 1223 priv->power_restore = if_sdio_power_restore; 1224 priv->is_polling = !(func->card->host->caps & MMC_CAP_SDIO_IRQ); 1225 ret = if_sdio_power_on(card); 1226 if (ret) 1227 goto err_activate_card; 1228 1229 out: 1230 return ret; 1231 1232 err_activate_card: 1233 flush_workqueue(card->workqueue); 1234 lbs_remove_card(priv); 1235 free: 1236 destroy_workqueue(card->workqueue); 1237 while (card->packets) { 1238 packet = card->packets; 1239 card->packets = card->packets->next; 1240 kfree(packet); 1241 } 1242 1243 kfree(card); 1244 1245 goto out; 1246 } 1247 1248 static void if_sdio_remove(struct sdio_func *func) 1249 { 1250 struct if_sdio_card *card; 1251 struct if_sdio_packet *packet; 1252 1253 card = sdio_get_drvdata(func); 1254 1255 /* Undo decrement done above in if_sdio_probe */ 1256 pm_runtime_get_noresume(&func->dev); 1257 1258 if (user_rmmod && (card->model == MODEL_8688)) { 1259 /* 1260 * FUNC_SHUTDOWN is required for SD8688 WLAN/BT 1261 * multiple functions 1262 */ 1263 struct cmd_header cmd; 1264 1265 memset(&cmd, 0, sizeof(cmd)); 1266 1267 lbs_deb_sdio("send function SHUTDOWN command\n"); 1268 if (__lbs_cmd(card->priv, CMD_FUNC_SHUTDOWN, 1269 &cmd, sizeof(cmd), lbs_cmd_copyback, 1270 (unsigned long) &cmd)) 1271 pr_alert("CMD_FUNC_SHUTDOWN cmd failed\n"); 1272 } 1273 1274 1275 lbs_deb_sdio("call remove card\n"); 1276 lbs_stop_card(card->priv); 1277 lbs_remove_card(card->priv); 1278 1279 destroy_workqueue(card->workqueue); 1280 1281 while (card->packets) { 1282 packet = card->packets; 1283 card->packets = card->packets->next; 1284 kfree(packet); 1285 } 1286 1287 kfree(card); 1288 } 1289 1290 static int if_sdio_suspend(struct device *dev) 1291 { 1292 struct sdio_func *func = dev_to_sdio_func(dev); 1293 int ret; 1294 struct if_sdio_card *card = sdio_get_drvdata(func); 1295 1296 mmc_pm_flag_t flags = sdio_get_host_pm_caps(func); 1297 1298 /* If we're powered off anyway, just let the mmc layer remove the 1299 * card. */ 1300 if (!lbs_iface_active(card->priv)) 1301 return -ENOSYS; 1302 1303 dev_info(dev, "%s: suspend: PM flags = 0x%x\n", 1304 sdio_func_id(func), flags); 1305 1306 /* If we aren't being asked to wake on anything, we should bail out 1307 * and let the SD stack power down the card. 1308 */ 1309 if (card->priv->wol_criteria == EHS_REMOVE_WAKEUP) { 1310 dev_info(dev, "Suspend without wake params -- powering down card\n"); 1311 return -ENOSYS; 1312 } 1313 1314 if (!(flags & MMC_PM_KEEP_POWER)) { 1315 dev_err(dev, "%s: cannot remain alive while host is suspended\n", 1316 sdio_func_id(func)); 1317 return -ENOSYS; 1318 } 1319 1320 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER); 1321 if (ret) 1322 return ret; 1323 1324 ret = lbs_suspend(card->priv); 1325 if (ret) 1326 return ret; 1327 1328 return sdio_set_host_pm_flags(func, MMC_PM_WAKE_SDIO_IRQ); 1329 } 1330 1331 static int if_sdio_resume(struct device *dev) 1332 { 1333 struct sdio_func *func = dev_to_sdio_func(dev); 1334 struct if_sdio_card *card = sdio_get_drvdata(func); 1335 int ret; 1336 1337 dev_info(dev, "%s: resume: we're back\n", sdio_func_id(func)); 1338 1339 ret = lbs_resume(card->priv); 1340 1341 return ret; 1342 } 1343 1344 static const struct dev_pm_ops if_sdio_pm_ops = { 1345 .suspend = if_sdio_suspend, 1346 .resume = if_sdio_resume, 1347 }; 1348 1349 static struct sdio_driver if_sdio_driver = { 1350 .name = "libertas_sdio", 1351 .id_table = if_sdio_ids, 1352 .probe = if_sdio_probe, 1353 .remove = if_sdio_remove, 1354 .drv = { 1355 .pm = &if_sdio_pm_ops, 1356 }, 1357 }; 1358 1359 /*******************************************************************/ 1360 /* Module functions */ 1361 /*******************************************************************/ 1362 1363 static int __init if_sdio_init_module(void) 1364 { 1365 int ret = 0; 1366 1367 printk(KERN_INFO "libertas_sdio: Libertas SDIO driver\n"); 1368 printk(KERN_INFO "libertas_sdio: Copyright Pierre Ossman\n"); 1369 1370 ret = sdio_register_driver(&if_sdio_driver); 1371 1372 /* Clear the flag in case user removes the card. */ 1373 user_rmmod = 0; 1374 1375 return ret; 1376 } 1377 1378 static void __exit if_sdio_exit_module(void) 1379 { 1380 /* Set the flag as user is removing this module. */ 1381 user_rmmod = 1; 1382 1383 cancel_work_sync(&card_reset_work); 1384 1385 sdio_unregister_driver(&if_sdio_driver); 1386 } 1387 1388 module_init(if_sdio_init_module); 1389 module_exit(if_sdio_exit_module); 1390 1391 MODULE_DESCRIPTION("Libertas SDIO WLAN Driver"); 1392 MODULE_AUTHOR("Pierre Ossman"); 1393 MODULE_LICENSE("GPL"); 1394