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