1 /* 2 * linux/drivers/mmc/core/sdio_io.c 3 * 4 * Copyright 2007-2008 Pierre Ossman 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or (at 9 * your option) any later version. 10 */ 11 12 #include <linux/export.h> 13 #include <linux/kernel.h> 14 #include <linux/mmc/host.h> 15 #include <linux/mmc/card.h> 16 #include <linux/mmc/sdio.h> 17 #include <linux/mmc/sdio_func.h> 18 19 #include "sdio_ops.h" 20 #include "core.h" 21 #include "card.h" 22 23 /** 24 * sdio_claim_host - exclusively claim a bus for a certain SDIO function 25 * @func: SDIO function that will be accessed 26 * 27 * Claim a bus for a set of operations. The SDIO function given 28 * is used to figure out which bus is relevant. 29 */ 30 void sdio_claim_host(struct sdio_func *func) 31 { 32 if (WARN_ON(!func)) 33 return; 34 35 mmc_claim_host(func->card->host); 36 } 37 EXPORT_SYMBOL_GPL(sdio_claim_host); 38 39 /** 40 * sdio_release_host - release a bus for a certain SDIO function 41 * @func: SDIO function that was accessed 42 * 43 * Release a bus, allowing others to claim the bus for their 44 * operations. 45 */ 46 void sdio_release_host(struct sdio_func *func) 47 { 48 if (WARN_ON(!func)) 49 return; 50 51 mmc_release_host(func->card->host); 52 } 53 EXPORT_SYMBOL_GPL(sdio_release_host); 54 55 /** 56 * sdio_enable_func - enables a SDIO function for usage 57 * @func: SDIO function to enable 58 * 59 * Powers up and activates a SDIO function so that register 60 * access is possible. 61 */ 62 int sdio_enable_func(struct sdio_func *func) 63 { 64 int ret; 65 unsigned char reg; 66 unsigned long timeout; 67 68 if (!func) 69 return -EINVAL; 70 71 pr_debug("SDIO: Enabling device %s...\n", sdio_func_id(func)); 72 73 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, ®); 74 if (ret) 75 goto err; 76 77 reg |= 1 << func->num; 78 79 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL); 80 if (ret) 81 goto err; 82 83 timeout = jiffies + msecs_to_jiffies(func->enable_timeout); 84 85 while (1) { 86 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IORx, 0, ®); 87 if (ret) 88 goto err; 89 if (reg & (1 << func->num)) 90 break; 91 ret = -ETIME; 92 if (time_after(jiffies, timeout)) 93 goto err; 94 } 95 96 pr_debug("SDIO: Enabled device %s\n", sdio_func_id(func)); 97 98 return 0; 99 100 err: 101 pr_debug("SDIO: Failed to enable device %s\n", sdio_func_id(func)); 102 return ret; 103 } 104 EXPORT_SYMBOL_GPL(sdio_enable_func); 105 106 /** 107 * sdio_disable_func - disable a SDIO function 108 * @func: SDIO function to disable 109 * 110 * Powers down and deactivates a SDIO function. Register access 111 * to this function will fail until the function is reenabled. 112 */ 113 int sdio_disable_func(struct sdio_func *func) 114 { 115 int ret; 116 unsigned char reg; 117 118 if (!func) 119 return -EINVAL; 120 121 pr_debug("SDIO: Disabling device %s...\n", sdio_func_id(func)); 122 123 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, ®); 124 if (ret) 125 goto err; 126 127 reg &= ~(1 << func->num); 128 129 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL); 130 if (ret) 131 goto err; 132 133 pr_debug("SDIO: Disabled device %s\n", sdio_func_id(func)); 134 135 return 0; 136 137 err: 138 pr_debug("SDIO: Failed to disable device %s\n", sdio_func_id(func)); 139 return -EIO; 140 } 141 EXPORT_SYMBOL_GPL(sdio_disable_func); 142 143 /** 144 * sdio_set_block_size - set the block size of an SDIO function 145 * @func: SDIO function to change 146 * @blksz: new block size or 0 to use the default. 147 * 148 * The default block size is the largest supported by both the function 149 * and the host, with a maximum of 512 to ensure that arbitrarily sized 150 * data transfer use the optimal (least) number of commands. 151 * 152 * A driver may call this to override the default block size set by the 153 * core. This can be used to set a block size greater than the maximum 154 * that reported by the card; it is the driver's responsibility to ensure 155 * it uses a value that the card supports. 156 * 157 * Returns 0 on success, -EINVAL if the host does not support the 158 * requested block size, or -EIO (etc.) if one of the resultant FBR block 159 * size register writes failed. 160 * 161 */ 162 int sdio_set_block_size(struct sdio_func *func, unsigned blksz) 163 { 164 int ret; 165 166 if (blksz > func->card->host->max_blk_size) 167 return -EINVAL; 168 169 if (blksz == 0) { 170 blksz = min(func->max_blksize, func->card->host->max_blk_size); 171 blksz = min(blksz, 512u); 172 } 173 174 ret = mmc_io_rw_direct(func->card, 1, 0, 175 SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE, 176 blksz & 0xff, NULL); 177 if (ret) 178 return ret; 179 ret = mmc_io_rw_direct(func->card, 1, 0, 180 SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE + 1, 181 (blksz >> 8) & 0xff, NULL); 182 if (ret) 183 return ret; 184 func->cur_blksize = blksz; 185 return 0; 186 } 187 EXPORT_SYMBOL_GPL(sdio_set_block_size); 188 189 /* 190 * Calculate the maximum byte mode transfer size 191 */ 192 static inline unsigned int sdio_max_byte_size(struct sdio_func *func) 193 { 194 unsigned mval = func->card->host->max_blk_size; 195 196 if (mmc_blksz_for_byte_mode(func->card)) 197 mval = min(mval, func->cur_blksize); 198 else 199 mval = min(mval, func->max_blksize); 200 201 if (mmc_card_broken_byte_mode_512(func->card)) 202 return min(mval, 511u); 203 204 return min(mval, 512u); /* maximum size for byte mode */ 205 } 206 207 /* 208 * This is legacy code, which needs to be re-worked some day. Basically we need 209 * to take into account the properties of the host, as to enable the SDIO func 210 * driver layer to allocate optimal buffers. 211 */ 212 static inline unsigned int _sdio_align_size(unsigned int sz) 213 { 214 /* 215 * FIXME: We don't have a system for the controller to tell 216 * the core about its problems yet, so for now we just 32-bit 217 * align the size. 218 */ 219 return ALIGN(sz, 4); 220 } 221 222 /** 223 * sdio_align_size - pads a transfer size to a more optimal value 224 * @func: SDIO function 225 * @sz: original transfer size 226 * 227 * Pads the original data size with a number of extra bytes in 228 * order to avoid controller bugs and/or performance hits 229 * (e.g. some controllers revert to PIO for certain sizes). 230 * 231 * If possible, it will also adjust the size so that it can be 232 * handled in just a single request. 233 * 234 * Returns the improved size, which might be unmodified. 235 */ 236 unsigned int sdio_align_size(struct sdio_func *func, unsigned int sz) 237 { 238 unsigned int orig_sz; 239 unsigned int blk_sz, byte_sz; 240 unsigned chunk_sz; 241 242 orig_sz = sz; 243 244 /* 245 * Do a first check with the controller, in case it 246 * wants to increase the size up to a point where it 247 * might need more than one block. 248 */ 249 sz = _sdio_align_size(sz); 250 251 /* 252 * If we can still do this with just a byte transfer, then 253 * we're done. 254 */ 255 if (sz <= sdio_max_byte_size(func)) 256 return sz; 257 258 if (func->card->cccr.multi_block) { 259 /* 260 * Check if the transfer is already block aligned 261 */ 262 if ((sz % func->cur_blksize) == 0) 263 return sz; 264 265 /* 266 * Realign it so that it can be done with one request, 267 * and recheck if the controller still likes it. 268 */ 269 blk_sz = ((sz + func->cur_blksize - 1) / 270 func->cur_blksize) * func->cur_blksize; 271 blk_sz = _sdio_align_size(blk_sz); 272 273 /* 274 * This value is only good if it is still just 275 * one request. 276 */ 277 if ((blk_sz % func->cur_blksize) == 0) 278 return blk_sz; 279 280 /* 281 * We failed to do one request, but at least try to 282 * pad the remainder properly. 283 */ 284 byte_sz = _sdio_align_size(sz % func->cur_blksize); 285 if (byte_sz <= sdio_max_byte_size(func)) { 286 blk_sz = sz / func->cur_blksize; 287 return blk_sz * func->cur_blksize + byte_sz; 288 } 289 } else { 290 /* 291 * We need multiple requests, so first check that the 292 * controller can handle the chunk size; 293 */ 294 chunk_sz = _sdio_align_size(sdio_max_byte_size(func)); 295 if (chunk_sz == sdio_max_byte_size(func)) { 296 /* 297 * Fix up the size of the remainder (if any) 298 */ 299 byte_sz = orig_sz % chunk_sz; 300 if (byte_sz) { 301 byte_sz = _sdio_align_size(byte_sz); 302 } 303 304 return (orig_sz / chunk_sz) * chunk_sz + byte_sz; 305 } 306 } 307 308 /* 309 * The controller is simply incapable of transferring the size 310 * we want in decent manner, so just return the original size. 311 */ 312 return orig_sz; 313 } 314 EXPORT_SYMBOL_GPL(sdio_align_size); 315 316 /* Split an arbitrarily sized data transfer into several 317 * IO_RW_EXTENDED commands. */ 318 static int sdio_io_rw_ext_helper(struct sdio_func *func, int write, 319 unsigned addr, int incr_addr, u8 *buf, unsigned size) 320 { 321 unsigned remainder = size; 322 unsigned max_blocks; 323 int ret; 324 325 if (!func || (func->num > 7)) 326 return -EINVAL; 327 328 /* Do the bulk of the transfer using block mode (if supported). */ 329 if (func->card->cccr.multi_block && (size > sdio_max_byte_size(func))) { 330 /* Blocks per command is limited by host count, host transfer 331 * size and the maximum for IO_RW_EXTENDED of 511 blocks. */ 332 max_blocks = min(func->card->host->max_blk_count, 511u); 333 334 while (remainder >= func->cur_blksize) { 335 unsigned blocks; 336 337 blocks = remainder / func->cur_blksize; 338 if (blocks > max_blocks) 339 blocks = max_blocks; 340 size = blocks * func->cur_blksize; 341 342 ret = mmc_io_rw_extended(func->card, write, 343 func->num, addr, incr_addr, buf, 344 blocks, func->cur_blksize); 345 if (ret) 346 return ret; 347 348 remainder -= size; 349 buf += size; 350 if (incr_addr) 351 addr += size; 352 } 353 } 354 355 /* Write the remainder using byte mode. */ 356 while (remainder > 0) { 357 size = min(remainder, sdio_max_byte_size(func)); 358 359 /* Indicate byte mode by setting "blocks" = 0 */ 360 ret = mmc_io_rw_extended(func->card, write, func->num, addr, 361 incr_addr, buf, 0, size); 362 if (ret) 363 return ret; 364 365 remainder -= size; 366 buf += size; 367 if (incr_addr) 368 addr += size; 369 } 370 return 0; 371 } 372 373 /** 374 * sdio_readb - read a single byte from a SDIO function 375 * @func: SDIO function to access 376 * @addr: address to read 377 * @err_ret: optional status value from transfer 378 * 379 * Reads a single byte from the address space of a given SDIO 380 * function. If there is a problem reading the address, 0xff 381 * is returned and @err_ret will contain the error code. 382 */ 383 u8 sdio_readb(struct sdio_func *func, unsigned int addr, int *err_ret) 384 { 385 int ret; 386 u8 val; 387 388 if (!func) { 389 if (err_ret) 390 *err_ret = -EINVAL; 391 return 0xFF; 392 } 393 394 ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val); 395 if (err_ret) 396 *err_ret = ret; 397 if (ret) 398 return 0xFF; 399 400 return val; 401 } 402 EXPORT_SYMBOL_GPL(sdio_readb); 403 404 /** 405 * sdio_writeb - write a single byte to a SDIO function 406 * @func: SDIO function to access 407 * @b: byte to write 408 * @addr: address to write to 409 * @err_ret: optional status value from transfer 410 * 411 * Writes a single byte to the address space of a given SDIO 412 * function. @err_ret will contain the status of the actual 413 * transfer. 414 */ 415 void sdio_writeb(struct sdio_func *func, u8 b, unsigned int addr, int *err_ret) 416 { 417 int ret; 418 419 if (!func) { 420 if (err_ret) 421 *err_ret = -EINVAL; 422 return; 423 } 424 425 ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL); 426 if (err_ret) 427 *err_ret = ret; 428 } 429 EXPORT_SYMBOL_GPL(sdio_writeb); 430 431 /** 432 * sdio_writeb_readb - write and read a byte from SDIO function 433 * @func: SDIO function to access 434 * @write_byte: byte to write 435 * @addr: address to write to 436 * @err_ret: optional status value from transfer 437 * 438 * Performs a RAW (Read after Write) operation as defined by SDIO spec - 439 * single byte is written to address space of a given SDIO function and 440 * response is read back from the same address, both using single request. 441 * If there is a problem with the operation, 0xff is returned and 442 * @err_ret will contain the error code. 443 */ 444 u8 sdio_writeb_readb(struct sdio_func *func, u8 write_byte, 445 unsigned int addr, int *err_ret) 446 { 447 int ret; 448 u8 val; 449 450 ret = mmc_io_rw_direct(func->card, 1, func->num, addr, 451 write_byte, &val); 452 if (err_ret) 453 *err_ret = ret; 454 if (ret) 455 return 0xff; 456 457 return val; 458 } 459 EXPORT_SYMBOL_GPL(sdio_writeb_readb); 460 461 /** 462 * sdio_memcpy_fromio - read a chunk of memory from a SDIO function 463 * @func: SDIO function to access 464 * @dst: buffer to store the data 465 * @addr: address to begin reading from 466 * @count: number of bytes to read 467 * 468 * Reads from the address space of a given SDIO function. Return 469 * value indicates if the transfer succeeded or not. 470 */ 471 int sdio_memcpy_fromio(struct sdio_func *func, void *dst, 472 unsigned int addr, int count) 473 { 474 return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count); 475 } 476 EXPORT_SYMBOL_GPL(sdio_memcpy_fromio); 477 478 /** 479 * sdio_memcpy_toio - write a chunk of memory to a SDIO function 480 * @func: SDIO function to access 481 * @addr: address to start writing to 482 * @src: buffer that contains the data to write 483 * @count: number of bytes to write 484 * 485 * Writes to the address space of a given SDIO function. Return 486 * value indicates if the transfer succeeded or not. 487 */ 488 int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr, 489 void *src, int count) 490 { 491 return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count); 492 } 493 EXPORT_SYMBOL_GPL(sdio_memcpy_toio); 494 495 /** 496 * sdio_readsb - read from a FIFO on a SDIO function 497 * @func: SDIO function to access 498 * @dst: buffer to store the data 499 * @addr: address of (single byte) FIFO 500 * @count: number of bytes to read 501 * 502 * Reads from the specified FIFO of a given SDIO function. Return 503 * value indicates if the transfer succeeded or not. 504 */ 505 int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr, 506 int count) 507 { 508 return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count); 509 } 510 EXPORT_SYMBOL_GPL(sdio_readsb); 511 512 /** 513 * sdio_writesb - write to a FIFO of a SDIO function 514 * @func: SDIO function to access 515 * @addr: address of (single byte) FIFO 516 * @src: buffer that contains the data to write 517 * @count: number of bytes to write 518 * 519 * Writes to the specified FIFO of a given SDIO function. Return 520 * value indicates if the transfer succeeded or not. 521 */ 522 int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src, 523 int count) 524 { 525 return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count); 526 } 527 EXPORT_SYMBOL_GPL(sdio_writesb); 528 529 /** 530 * sdio_readw - read a 16 bit integer from a SDIO function 531 * @func: SDIO function to access 532 * @addr: address to read 533 * @err_ret: optional status value from transfer 534 * 535 * Reads a 16 bit integer from the address space of a given SDIO 536 * function. If there is a problem reading the address, 0xffff 537 * is returned and @err_ret will contain the error code. 538 */ 539 u16 sdio_readw(struct sdio_func *func, unsigned int addr, int *err_ret) 540 { 541 int ret; 542 543 ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2); 544 if (err_ret) 545 *err_ret = ret; 546 if (ret) 547 return 0xFFFF; 548 549 return le16_to_cpup((__le16 *)func->tmpbuf); 550 } 551 EXPORT_SYMBOL_GPL(sdio_readw); 552 553 /** 554 * sdio_writew - write a 16 bit integer to a SDIO function 555 * @func: SDIO function to access 556 * @b: integer to write 557 * @addr: address to write to 558 * @err_ret: optional status value from transfer 559 * 560 * Writes a 16 bit integer to the address space of a given SDIO 561 * function. @err_ret will contain the status of the actual 562 * transfer. 563 */ 564 void sdio_writew(struct sdio_func *func, u16 b, unsigned int addr, int *err_ret) 565 { 566 int ret; 567 568 *(__le16 *)func->tmpbuf = cpu_to_le16(b); 569 570 ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2); 571 if (err_ret) 572 *err_ret = ret; 573 } 574 EXPORT_SYMBOL_GPL(sdio_writew); 575 576 /** 577 * sdio_readl - read a 32 bit integer from a SDIO function 578 * @func: SDIO function to access 579 * @addr: address to read 580 * @err_ret: optional status value from transfer 581 * 582 * Reads a 32 bit integer from the address space of a given SDIO 583 * function. If there is a problem reading the address, 584 * 0xffffffff is returned and @err_ret will contain the error 585 * code. 586 */ 587 u32 sdio_readl(struct sdio_func *func, unsigned int addr, int *err_ret) 588 { 589 int ret; 590 591 ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4); 592 if (err_ret) 593 *err_ret = ret; 594 if (ret) 595 return 0xFFFFFFFF; 596 597 return le32_to_cpup((__le32 *)func->tmpbuf); 598 } 599 EXPORT_SYMBOL_GPL(sdio_readl); 600 601 /** 602 * sdio_writel - write a 32 bit integer to a SDIO function 603 * @func: SDIO function to access 604 * @b: integer to write 605 * @addr: address to write to 606 * @err_ret: optional status value from transfer 607 * 608 * Writes a 32 bit integer to the address space of a given SDIO 609 * function. @err_ret will contain the status of the actual 610 * transfer. 611 */ 612 void sdio_writel(struct sdio_func *func, u32 b, unsigned int addr, int *err_ret) 613 { 614 int ret; 615 616 *(__le32 *)func->tmpbuf = cpu_to_le32(b); 617 618 ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4); 619 if (err_ret) 620 *err_ret = ret; 621 } 622 EXPORT_SYMBOL_GPL(sdio_writel); 623 624 /** 625 * sdio_f0_readb - read a single byte from SDIO function 0 626 * @func: an SDIO function of the card 627 * @addr: address to read 628 * @err_ret: optional status value from transfer 629 * 630 * Reads a single byte from the address space of SDIO function 0. 631 * If there is a problem reading the address, 0xff is returned 632 * and @err_ret will contain the error code. 633 */ 634 unsigned char sdio_f0_readb(struct sdio_func *func, unsigned int addr, 635 int *err_ret) 636 { 637 int ret; 638 unsigned char val; 639 640 if (!func) { 641 if (err_ret) 642 *err_ret = -EINVAL; 643 return 0xFF; 644 } 645 646 ret = mmc_io_rw_direct(func->card, 0, 0, addr, 0, &val); 647 if (err_ret) 648 *err_ret = ret; 649 if (ret) 650 return 0xFF; 651 652 return val; 653 } 654 EXPORT_SYMBOL_GPL(sdio_f0_readb); 655 656 /** 657 * sdio_f0_writeb - write a single byte to SDIO function 0 658 * @func: an SDIO function of the card 659 * @b: byte to write 660 * @addr: address to write to 661 * @err_ret: optional status value from transfer 662 * 663 * Writes a single byte to the address space of SDIO function 0. 664 * @err_ret will contain the status of the actual transfer. 665 * 666 * Only writes to the vendor specific CCCR registers (0xF0 - 667 * 0xFF) are permiited; @err_ret will be set to -EINVAL for * 668 * writes outside this range. 669 */ 670 void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr, 671 int *err_ret) 672 { 673 int ret; 674 675 if (!func) { 676 if (err_ret) 677 *err_ret = -EINVAL; 678 return; 679 } 680 681 if ((addr < 0xF0 || addr > 0xFF) && (!mmc_card_lenient_fn0(func->card))) { 682 if (err_ret) 683 *err_ret = -EINVAL; 684 return; 685 } 686 687 ret = mmc_io_rw_direct(func->card, 1, 0, addr, b, NULL); 688 if (err_ret) 689 *err_ret = ret; 690 } 691 EXPORT_SYMBOL_GPL(sdio_f0_writeb); 692 693 /** 694 * sdio_get_host_pm_caps - get host power management capabilities 695 * @func: SDIO function attached to host 696 * 697 * Returns a capability bitmask corresponding to power management 698 * features supported by the host controller that the card function 699 * might rely upon during a system suspend. The host doesn't need 700 * to be claimed, nor the function active, for this information to be 701 * obtained. 702 */ 703 mmc_pm_flag_t sdio_get_host_pm_caps(struct sdio_func *func) 704 { 705 if (!func) 706 return 0; 707 708 return func->card->host->pm_caps; 709 } 710 EXPORT_SYMBOL_GPL(sdio_get_host_pm_caps); 711 712 /** 713 * sdio_set_host_pm_flags - set wanted host power management capabilities 714 * @func: SDIO function attached to host 715 * 716 * Set a capability bitmask corresponding to wanted host controller 717 * power management features for the upcoming suspend state. 718 * This must be called, if needed, each time the suspend method of 719 * the function driver is called, and must contain only bits that 720 * were returned by sdio_get_host_pm_caps(). 721 * The host doesn't need to be claimed, nor the function active, 722 * for this information to be set. 723 */ 724 int sdio_set_host_pm_flags(struct sdio_func *func, mmc_pm_flag_t flags) 725 { 726 struct mmc_host *host; 727 728 if (!func) 729 return -EINVAL; 730 731 host = func->card->host; 732 733 if (flags & ~host->pm_caps) 734 return -EINVAL; 735 736 /* function suspend methods are serialized, hence no lock needed */ 737 host->pm_flags |= flags; 738 return 0; 739 } 740 EXPORT_SYMBOL_GPL(sdio_set_host_pm_flags); 741