Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  linux/drivers/mmc/core/sdio_io.c
0004  *
0005  *  Copyright 2007-2008 Pierre Ossman
0006  */
0007 
0008 #include <linux/export.h>
0009 #include <linux/kernel.h>
0010 #include <linux/mmc/host.h>
0011 #include <linux/mmc/card.h>
0012 #include <linux/mmc/sdio.h>
0013 #include <linux/mmc/sdio_func.h>
0014 
0015 #include "sdio_ops.h"
0016 #include "core.h"
0017 #include "card.h"
0018 #include "host.h"
0019 
0020 /**
0021  *  sdio_claim_host - exclusively claim a bus for a certain SDIO function
0022  *  @func: SDIO function that will be accessed
0023  *
0024  *  Claim a bus for a set of operations. The SDIO function given
0025  *  is used to figure out which bus is relevant.
0026  */
0027 void sdio_claim_host(struct sdio_func *func)
0028 {
0029     if (WARN_ON(!func))
0030         return;
0031 
0032     mmc_claim_host(func->card->host);
0033 }
0034 EXPORT_SYMBOL_GPL(sdio_claim_host);
0035 
0036 /**
0037  *  sdio_release_host - release a bus for a certain SDIO function
0038  *  @func: SDIO function that was accessed
0039  *
0040  *  Release a bus, allowing others to claim the bus for their
0041  *  operations.
0042  */
0043 void sdio_release_host(struct sdio_func *func)
0044 {
0045     if (WARN_ON(!func))
0046         return;
0047 
0048     mmc_release_host(func->card->host);
0049 }
0050 EXPORT_SYMBOL_GPL(sdio_release_host);
0051 
0052 /**
0053  *  sdio_enable_func - enables a SDIO function for usage
0054  *  @func: SDIO function to enable
0055  *
0056  *  Powers up and activates a SDIO function so that register
0057  *  access is possible.
0058  */
0059 int sdio_enable_func(struct sdio_func *func)
0060 {
0061     int ret;
0062     unsigned char reg;
0063     unsigned long timeout;
0064 
0065     if (!func)
0066         return -EINVAL;
0067 
0068     pr_debug("SDIO: Enabling device %s...\n", sdio_func_id(func));
0069 
0070     ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, &reg);
0071     if (ret)
0072         goto err;
0073 
0074     reg |= 1 << func->num;
0075 
0076     ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);
0077     if (ret)
0078         goto err;
0079 
0080     timeout = jiffies + msecs_to_jiffies(func->enable_timeout);
0081 
0082     while (1) {
0083         ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IORx, 0, &reg);
0084         if (ret)
0085             goto err;
0086         if (reg & (1 << func->num))
0087             break;
0088         ret = -ETIME;
0089         if (time_after(jiffies, timeout))
0090             goto err;
0091     }
0092 
0093     pr_debug("SDIO: Enabled device %s\n", sdio_func_id(func));
0094 
0095     return 0;
0096 
0097 err:
0098     pr_debug("SDIO: Failed to enable device %s\n", sdio_func_id(func));
0099     return ret;
0100 }
0101 EXPORT_SYMBOL_GPL(sdio_enable_func);
0102 
0103 /**
0104  *  sdio_disable_func - disable a SDIO function
0105  *  @func: SDIO function to disable
0106  *
0107  *  Powers down and deactivates a SDIO function. Register access
0108  *  to this function will fail until the function is reenabled.
0109  */
0110 int sdio_disable_func(struct sdio_func *func)
0111 {
0112     int ret;
0113     unsigned char reg;
0114 
0115     if (!func)
0116         return -EINVAL;
0117 
0118     pr_debug("SDIO: Disabling device %s...\n", sdio_func_id(func));
0119 
0120     ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, &reg);
0121     if (ret)
0122         goto err;
0123 
0124     reg &= ~(1 << func->num);
0125 
0126     ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);
0127     if (ret)
0128         goto err;
0129 
0130     pr_debug("SDIO: Disabled device %s\n", sdio_func_id(func));
0131 
0132     return 0;
0133 
0134 err:
0135     pr_debug("SDIO: Failed to disable device %s\n", sdio_func_id(func));
0136     return ret;
0137 }
0138 EXPORT_SYMBOL_GPL(sdio_disable_func);
0139 
0140 /**
0141  *  sdio_set_block_size - set the block size of an SDIO function
0142  *  @func: SDIO function to change
0143  *  @blksz: new block size or 0 to use the default.
0144  *
0145  *  The default block size is the largest supported by both the function
0146  *  and the host, with a maximum of 512 to ensure that arbitrarily sized
0147  *  data transfer use the optimal (least) number of commands.
0148  *
0149  *  A driver may call this to override the default block size set by the
0150  *  core. This can be used to set a block size greater than the maximum
0151  *  that reported by the card; it is the driver's responsibility to ensure
0152  *  it uses a value that the card supports.
0153  *
0154  *  Returns 0 on success, -EINVAL if the host does not support the
0155  *  requested block size, or -EIO (etc.) if one of the resultant FBR block
0156  *  size register writes failed.
0157  *
0158  */
0159 int sdio_set_block_size(struct sdio_func *func, unsigned blksz)
0160 {
0161     int ret;
0162 
0163     if (blksz > func->card->host->max_blk_size)
0164         return -EINVAL;
0165 
0166     if (blksz == 0) {
0167         blksz = min(func->max_blksize, func->card->host->max_blk_size);
0168         blksz = min(blksz, 512u);
0169     }
0170 
0171     ret = mmc_io_rw_direct(func->card, 1, 0,
0172         SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE,
0173         blksz & 0xff, NULL);
0174     if (ret)
0175         return ret;
0176     ret = mmc_io_rw_direct(func->card, 1, 0,
0177         SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE + 1,
0178         (blksz >> 8) & 0xff, NULL);
0179     if (ret)
0180         return ret;
0181     func->cur_blksize = blksz;
0182     return 0;
0183 }
0184 EXPORT_SYMBOL_GPL(sdio_set_block_size);
0185 
0186 /*
0187  * Calculate the maximum byte mode transfer size
0188  */
0189 static inline unsigned int sdio_max_byte_size(struct sdio_func *func)
0190 {
0191     unsigned mval = func->card->host->max_blk_size;
0192 
0193     if (mmc_blksz_for_byte_mode(func->card))
0194         mval = min(mval, func->cur_blksize);
0195     else
0196         mval = min(mval, func->max_blksize);
0197 
0198     if (mmc_card_broken_byte_mode_512(func->card))
0199         return min(mval, 511u);
0200 
0201     return min(mval, 512u); /* maximum size for byte mode */
0202 }
0203 
0204 /*
0205  * This is legacy code, which needs to be re-worked some day. Basically we need
0206  * to take into account the properties of the host, as to enable the SDIO func
0207  * driver layer to allocate optimal buffers.
0208  */
0209 static inline unsigned int _sdio_align_size(unsigned int sz)
0210 {
0211     /*
0212      * FIXME: We don't have a system for the controller to tell
0213      * the core about its problems yet, so for now we just 32-bit
0214      * align the size.
0215      */
0216     return ALIGN(sz, 4);
0217 }
0218 
0219 /**
0220  *  sdio_align_size - pads a transfer size to a more optimal value
0221  *  @func: SDIO function
0222  *  @sz: original transfer size
0223  *
0224  *  Pads the original data size with a number of extra bytes in
0225  *  order to avoid controller bugs and/or performance hits
0226  *  (e.g. some controllers revert to PIO for certain sizes).
0227  *
0228  *  If possible, it will also adjust the size so that it can be
0229  *  handled in just a single request.
0230  *
0231  *  Returns the improved size, which might be unmodified.
0232  */
0233 unsigned int sdio_align_size(struct sdio_func *func, unsigned int sz)
0234 {
0235     unsigned int orig_sz;
0236     unsigned int blk_sz, byte_sz;
0237     unsigned chunk_sz;
0238 
0239     orig_sz = sz;
0240 
0241     /*
0242      * Do a first check with the controller, in case it
0243      * wants to increase the size up to a point where it
0244      * might need more than one block.
0245      */
0246     sz = _sdio_align_size(sz);
0247 
0248     /*
0249      * If we can still do this with just a byte transfer, then
0250      * we're done.
0251      */
0252     if (sz <= sdio_max_byte_size(func))
0253         return sz;
0254 
0255     if (func->card->cccr.multi_block) {
0256         /*
0257          * Check if the transfer is already block aligned
0258          */
0259         if ((sz % func->cur_blksize) == 0)
0260             return sz;
0261 
0262         /*
0263          * Realign it so that it can be done with one request,
0264          * and recheck if the controller still likes it.
0265          */
0266         blk_sz = ((sz + func->cur_blksize - 1) /
0267             func->cur_blksize) * func->cur_blksize;
0268         blk_sz = _sdio_align_size(blk_sz);
0269 
0270         /*
0271          * This value is only good if it is still just
0272          * one request.
0273          */
0274         if ((blk_sz % func->cur_blksize) == 0)
0275             return blk_sz;
0276 
0277         /*
0278          * We failed to do one request, but at least try to
0279          * pad the remainder properly.
0280          */
0281         byte_sz = _sdio_align_size(sz % func->cur_blksize);
0282         if (byte_sz <= sdio_max_byte_size(func)) {
0283             blk_sz = sz / func->cur_blksize;
0284             return blk_sz * func->cur_blksize + byte_sz;
0285         }
0286     } else {
0287         /*
0288          * We need multiple requests, so first check that the
0289          * controller can handle the chunk size;
0290          */
0291         chunk_sz = _sdio_align_size(sdio_max_byte_size(func));
0292         if (chunk_sz == sdio_max_byte_size(func)) {
0293             /*
0294              * Fix up the size of the remainder (if any)
0295              */
0296             byte_sz = orig_sz % chunk_sz;
0297             if (byte_sz) {
0298                 byte_sz = _sdio_align_size(byte_sz);
0299             }
0300 
0301             return (orig_sz / chunk_sz) * chunk_sz + byte_sz;
0302         }
0303     }
0304 
0305     /*
0306      * The controller is simply incapable of transferring the size
0307      * we want in decent manner, so just return the original size.
0308      */
0309     return orig_sz;
0310 }
0311 EXPORT_SYMBOL_GPL(sdio_align_size);
0312 
0313 /* Split an arbitrarily sized data transfer into several
0314  * IO_RW_EXTENDED commands. */
0315 static int sdio_io_rw_ext_helper(struct sdio_func *func, int write,
0316     unsigned addr, int incr_addr, u8 *buf, unsigned size)
0317 {
0318     unsigned remainder = size;
0319     unsigned max_blocks;
0320     int ret;
0321 
0322     if (!func || (func->num > 7))
0323         return -EINVAL;
0324 
0325     /* Do the bulk of the transfer using block mode (if supported). */
0326     if (func->card->cccr.multi_block && (size > sdio_max_byte_size(func))) {
0327         /* Blocks per command is limited by host count, host transfer
0328          * size and the maximum for IO_RW_EXTENDED of 511 blocks. */
0329         max_blocks = min(func->card->host->max_blk_count, 511u);
0330 
0331         while (remainder >= func->cur_blksize) {
0332             unsigned blocks;
0333 
0334             blocks = remainder / func->cur_blksize;
0335             if (blocks > max_blocks)
0336                 blocks = max_blocks;
0337             size = blocks * func->cur_blksize;
0338 
0339             ret = mmc_io_rw_extended(func->card, write,
0340                 func->num, addr, incr_addr, buf,
0341                 blocks, func->cur_blksize);
0342             if (ret)
0343                 return ret;
0344 
0345             remainder -= size;
0346             buf += size;
0347             if (incr_addr)
0348                 addr += size;
0349         }
0350     }
0351 
0352     /* Write the remainder using byte mode. */
0353     while (remainder > 0) {
0354         size = min(remainder, sdio_max_byte_size(func));
0355 
0356         /* Indicate byte mode by setting "blocks" = 0 */
0357         ret = mmc_io_rw_extended(func->card, write, func->num, addr,
0358              incr_addr, buf, 0, size);
0359         if (ret)
0360             return ret;
0361 
0362         remainder -= size;
0363         buf += size;
0364         if (incr_addr)
0365             addr += size;
0366     }
0367     return 0;
0368 }
0369 
0370 /**
0371  *  sdio_readb - read a single byte from a SDIO function
0372  *  @func: SDIO function to access
0373  *  @addr: address to read
0374  *  @err_ret: optional status value from transfer
0375  *
0376  *  Reads a single byte from the address space of a given SDIO
0377  *  function. If there is a problem reading the address, 0xff
0378  *  is returned and @err_ret will contain the error code.
0379  */
0380 u8 sdio_readb(struct sdio_func *func, unsigned int addr, int *err_ret)
0381 {
0382     int ret;
0383     u8 val;
0384 
0385     if (!func) {
0386         if (err_ret)
0387             *err_ret = -EINVAL;
0388         return 0xFF;
0389     }
0390 
0391     ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val);
0392     if (err_ret)
0393         *err_ret = ret;
0394     if (ret)
0395         return 0xFF;
0396 
0397     return val;
0398 }
0399 EXPORT_SYMBOL_GPL(sdio_readb);
0400 
0401 /**
0402  *  sdio_writeb - write a single byte to a SDIO function
0403  *  @func: SDIO function to access
0404  *  @b: byte to write
0405  *  @addr: address to write to
0406  *  @err_ret: optional status value from transfer
0407  *
0408  *  Writes a single byte to the address space of a given SDIO
0409  *  function. @err_ret will contain the status of the actual
0410  *  transfer.
0411  */
0412 void sdio_writeb(struct sdio_func *func, u8 b, unsigned int addr, int *err_ret)
0413 {
0414     int ret;
0415 
0416     if (!func) {
0417         if (err_ret)
0418             *err_ret = -EINVAL;
0419         return;
0420     }
0421 
0422     ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL);
0423     if (err_ret)
0424         *err_ret = ret;
0425 }
0426 EXPORT_SYMBOL_GPL(sdio_writeb);
0427 
0428 /**
0429  *  sdio_writeb_readb - write and read a byte from SDIO function
0430  *  @func: SDIO function to access
0431  *  @write_byte: byte to write
0432  *  @addr: address to write to
0433  *  @err_ret: optional status value from transfer
0434  *
0435  *  Performs a RAW (Read after Write) operation as defined by SDIO spec -
0436  *  single byte is written to address space of a given SDIO function and
0437  *  response is read back from the same address, both using single request.
0438  *  If there is a problem with the operation, 0xff is returned and
0439  *  @err_ret will contain the error code.
0440  */
0441 u8 sdio_writeb_readb(struct sdio_func *func, u8 write_byte,
0442     unsigned int addr, int *err_ret)
0443 {
0444     int ret;
0445     u8 val;
0446 
0447     ret = mmc_io_rw_direct(func->card, 1, func->num, addr,
0448             write_byte, &val);
0449     if (err_ret)
0450         *err_ret = ret;
0451     if (ret)
0452         return 0xff;
0453 
0454     return val;
0455 }
0456 EXPORT_SYMBOL_GPL(sdio_writeb_readb);
0457 
0458 /**
0459  *  sdio_memcpy_fromio - read a chunk of memory from a SDIO function
0460  *  @func: SDIO function to access
0461  *  @dst: buffer to store the data
0462  *  @addr: address to begin reading from
0463  *  @count: number of bytes to read
0464  *
0465  *  Reads from the address space of a given SDIO function. Return
0466  *  value indicates if the transfer succeeded or not.
0467  */
0468 int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
0469     unsigned int addr, int count)
0470 {
0471     return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count);
0472 }
0473 EXPORT_SYMBOL_GPL(sdio_memcpy_fromio);
0474 
0475 /**
0476  *  sdio_memcpy_toio - write a chunk of memory to a SDIO function
0477  *  @func: SDIO function to access
0478  *  @addr: address to start writing to
0479  *  @src: buffer that contains the data to write
0480  *  @count: number of bytes to write
0481  *
0482  *  Writes to the address space of a given SDIO function. Return
0483  *  value indicates if the transfer succeeded or not.
0484  */
0485 int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
0486     void *src, int count)
0487 {
0488     return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count);
0489 }
0490 EXPORT_SYMBOL_GPL(sdio_memcpy_toio);
0491 
0492 /**
0493  *  sdio_readsb - read from a FIFO on a SDIO function
0494  *  @func: SDIO function to access
0495  *  @dst: buffer to store the data
0496  *  @addr: address of (single byte) FIFO
0497  *  @count: number of bytes to read
0498  *
0499  *  Reads from the specified FIFO of a given SDIO function. Return
0500  *  value indicates if the transfer succeeded or not.
0501  */
0502 int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
0503     int count)
0504 {
0505     return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count);
0506 }
0507 EXPORT_SYMBOL_GPL(sdio_readsb);
0508 
0509 /**
0510  *  sdio_writesb - write to a FIFO of a SDIO function
0511  *  @func: SDIO function to access
0512  *  @addr: address of (single byte) FIFO
0513  *  @src: buffer that contains the data to write
0514  *  @count: number of bytes to write
0515  *
0516  *  Writes to the specified FIFO of a given SDIO function. Return
0517  *  value indicates if the transfer succeeded or not.
0518  */
0519 int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,
0520     int count)
0521 {
0522     return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count);
0523 }
0524 EXPORT_SYMBOL_GPL(sdio_writesb);
0525 
0526 /**
0527  *  sdio_readw - read a 16 bit integer from a SDIO function
0528  *  @func: SDIO function to access
0529  *  @addr: address to read
0530  *  @err_ret: optional status value from transfer
0531  *
0532  *  Reads a 16 bit integer from the address space of a given SDIO
0533  *  function. If there is a problem reading the address, 0xffff
0534  *  is returned and @err_ret will contain the error code.
0535  */
0536 u16 sdio_readw(struct sdio_func *func, unsigned int addr, int *err_ret)
0537 {
0538     int ret;
0539 
0540     ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);
0541     if (err_ret)
0542         *err_ret = ret;
0543     if (ret)
0544         return 0xFFFF;
0545 
0546     return le16_to_cpup((__le16 *)func->tmpbuf);
0547 }
0548 EXPORT_SYMBOL_GPL(sdio_readw);
0549 
0550 /**
0551  *  sdio_writew - write a 16 bit integer to a SDIO function
0552  *  @func: SDIO function to access
0553  *  @b: integer to write
0554  *  @addr: address to write to
0555  *  @err_ret: optional status value from transfer
0556  *
0557  *  Writes a 16 bit integer to the address space of a given SDIO
0558  *  function. @err_ret will contain the status of the actual
0559  *  transfer.
0560  */
0561 void sdio_writew(struct sdio_func *func, u16 b, unsigned int addr, int *err_ret)
0562 {
0563     int ret;
0564 
0565     *(__le16 *)func->tmpbuf = cpu_to_le16(b);
0566 
0567     ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
0568     if (err_ret)
0569         *err_ret = ret;
0570 }
0571 EXPORT_SYMBOL_GPL(sdio_writew);
0572 
0573 /**
0574  *  sdio_readl - read a 32 bit integer from a SDIO function
0575  *  @func: SDIO function to access
0576  *  @addr: address to read
0577  *  @err_ret: optional status value from transfer
0578  *
0579  *  Reads a 32 bit integer from the address space of a given SDIO
0580  *  function. If there is a problem reading the address,
0581  *  0xffffffff is returned and @err_ret will contain the error
0582  *  code.
0583  */
0584 u32 sdio_readl(struct sdio_func *func, unsigned int addr, int *err_ret)
0585 {
0586     int ret;
0587 
0588     ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4);
0589     if (err_ret)
0590         *err_ret = ret;
0591     if (ret)
0592         return 0xFFFFFFFF;
0593 
0594     return le32_to_cpup((__le32 *)func->tmpbuf);
0595 }
0596 EXPORT_SYMBOL_GPL(sdio_readl);
0597 
0598 /**
0599  *  sdio_writel - write a 32 bit integer to a SDIO function
0600  *  @func: SDIO function to access
0601  *  @b: integer to write
0602  *  @addr: address to write to
0603  *  @err_ret: optional status value from transfer
0604  *
0605  *  Writes a 32 bit integer to the address space of a given SDIO
0606  *  function. @err_ret will contain the status of the actual
0607  *  transfer.
0608  */
0609 void sdio_writel(struct sdio_func *func, u32 b, unsigned int addr, int *err_ret)
0610 {
0611     int ret;
0612 
0613     *(__le32 *)func->tmpbuf = cpu_to_le32(b);
0614 
0615     ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
0616     if (err_ret)
0617         *err_ret = ret;
0618 }
0619 EXPORT_SYMBOL_GPL(sdio_writel);
0620 
0621 /**
0622  *  sdio_f0_readb - read a single byte from SDIO function 0
0623  *  @func: an SDIO function of the card
0624  *  @addr: address to read
0625  *  @err_ret: optional status value from transfer
0626  *
0627  *  Reads a single byte from the address space of SDIO function 0.
0628  *  If there is a problem reading the address, 0xff is returned
0629  *  and @err_ret will contain the error code.
0630  */
0631 unsigned char sdio_f0_readb(struct sdio_func *func, unsigned int addr,
0632     int *err_ret)
0633 {
0634     int ret;
0635     unsigned char val;
0636 
0637     if (!func) {
0638         if (err_ret)
0639             *err_ret = -EINVAL;
0640         return 0xFF;
0641     }
0642 
0643     ret = mmc_io_rw_direct(func->card, 0, 0, addr, 0, &val);
0644     if (err_ret)
0645         *err_ret = ret;
0646     if (ret)
0647         return 0xFF;
0648 
0649     return val;
0650 }
0651 EXPORT_SYMBOL_GPL(sdio_f0_readb);
0652 
0653 /**
0654  *  sdio_f0_writeb - write a single byte to SDIO function 0
0655  *  @func: an SDIO function of the card
0656  *  @b: byte to write
0657  *  @addr: address to write to
0658  *  @err_ret: optional status value from transfer
0659  *
0660  *  Writes a single byte to the address space of SDIO function 0.
0661  *  @err_ret will contain the status of the actual transfer.
0662  *
0663  *  Only writes to the vendor specific CCCR registers (0xF0 -
0664  *  0xFF) are permiited; @err_ret will be set to -EINVAL for *
0665  *  writes outside this range.
0666  */
0667 void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
0668     int *err_ret)
0669 {
0670     int ret;
0671 
0672     if (!func) {
0673         if (err_ret)
0674             *err_ret = -EINVAL;
0675         return;
0676     }
0677 
0678     if ((addr < 0xF0 || addr > 0xFF) && (!mmc_card_lenient_fn0(func->card))) {
0679         if (err_ret)
0680             *err_ret = -EINVAL;
0681         return;
0682     }
0683 
0684     ret = mmc_io_rw_direct(func->card, 1, 0, addr, b, NULL);
0685     if (err_ret)
0686         *err_ret = ret;
0687 }
0688 EXPORT_SYMBOL_GPL(sdio_f0_writeb);
0689 
0690 /**
0691  *  sdio_get_host_pm_caps - get host power management capabilities
0692  *  @func: SDIO function attached to host
0693  *
0694  *  Returns a capability bitmask corresponding to power management
0695  *  features supported by the host controller that the card function
0696  *  might rely upon during a system suspend.  The host doesn't need
0697  *  to be claimed, nor the function active, for this information to be
0698  *  obtained.
0699  */
0700 mmc_pm_flag_t sdio_get_host_pm_caps(struct sdio_func *func)
0701 {
0702     if (!func)
0703         return 0;
0704 
0705     return func->card->host->pm_caps;
0706 }
0707 EXPORT_SYMBOL_GPL(sdio_get_host_pm_caps);
0708 
0709 /**
0710  *  sdio_set_host_pm_flags - set wanted host power management capabilities
0711  *  @func: SDIO function attached to host
0712  *  @flags: Power Management flags to set
0713  *
0714  *  Set a capability bitmask corresponding to wanted host controller
0715  *  power management features for the upcoming suspend state.
0716  *  This must be called, if needed, each time the suspend method of
0717  *  the function driver is called, and must contain only bits that
0718  *  were returned by sdio_get_host_pm_caps().
0719  *  The host doesn't need to be claimed, nor the function active,
0720  *  for this information to be set.
0721  */
0722 int sdio_set_host_pm_flags(struct sdio_func *func, mmc_pm_flag_t flags)
0723 {
0724     struct mmc_host *host;
0725 
0726     if (!func)
0727         return -EINVAL;
0728 
0729     host = func->card->host;
0730 
0731     if (flags & ~host->pm_caps)
0732         return -EINVAL;
0733 
0734     /* function suspend methods are serialized, hence no lock needed */
0735     host->pm_flags |= flags;
0736     return 0;
0737 }
0738 EXPORT_SYMBOL_GPL(sdio_set_host_pm_flags);
0739 
0740 /**
0741  *  sdio_retune_crc_disable - temporarily disable retuning on CRC errors
0742  *  @func: SDIO function attached to host
0743  *
0744  *  If the SDIO card is known to be in a state where it might produce
0745  *  CRC errors on the bus in response to commands (like if we know it is
0746  *  transitioning between power states), an SDIO function driver can
0747  *  call this function to temporarily disable the SD/MMC core behavior of
0748  *  triggering an automatic retuning.
0749  *
0750  *  This function should be called while the host is claimed and the host
0751  *  should remain claimed until sdio_retune_crc_enable() is called.
0752  *  Specifically, the expected sequence of calls is:
0753  *  - sdio_claim_host()
0754  *  - sdio_retune_crc_disable()
0755  *  - some number of calls like sdio_writeb() and sdio_readb()
0756  *  - sdio_retune_crc_enable()
0757  *  - sdio_release_host()
0758  */
0759 void sdio_retune_crc_disable(struct sdio_func *func)
0760 {
0761     func->card->host->retune_crc_disable = true;
0762 }
0763 EXPORT_SYMBOL_GPL(sdio_retune_crc_disable);
0764 
0765 /**
0766  *  sdio_retune_crc_enable - re-enable retuning on CRC errors
0767  *  @func: SDIO function attached to host
0768  *
0769  *  This is the compement to sdio_retune_crc_disable().
0770  */
0771 void sdio_retune_crc_enable(struct sdio_func *func)
0772 {
0773     func->card->host->retune_crc_disable = false;
0774 }
0775 EXPORT_SYMBOL_GPL(sdio_retune_crc_enable);
0776 
0777 /**
0778  *  sdio_retune_hold_now - start deferring retuning requests till release
0779  *  @func: SDIO function attached to host
0780  *
0781  *  This function can be called if it's currently a bad time to do
0782  *  a retune of the SDIO card.  Retune requests made during this time
0783  *  will be held and we'll actually do the retune sometime after the
0784  *  release.
0785  *
0786  *  This function could be useful if an SDIO card is in a power state
0787  *  where it can respond to a small subset of commands that doesn't
0788  *  include the retuning command.  Care should be taken when using
0789  *  this function since (presumably) the retuning request we might be
0790  *  deferring was made for a good reason.
0791  *
0792  *  This function should be called while the host is claimed.
0793  */
0794 void sdio_retune_hold_now(struct sdio_func *func)
0795 {
0796     mmc_retune_hold_now(func->card->host);
0797 }
0798 EXPORT_SYMBOL_GPL(sdio_retune_hold_now);
0799 
0800 /**
0801  *  sdio_retune_release - signal that it's OK to retune now
0802  *  @func: SDIO function attached to host
0803  *
0804  *  This is the complement to sdio_retune_hold_now().  Calling this
0805  *  function won't make a retune happen right away but will allow
0806  *  them to be scheduled normally.
0807  *
0808  *  This function should be called while the host is claimed.
0809  */
0810 void sdio_retune_release(struct sdio_func *func)
0811 {
0812     mmc_retune_release(func->card->host);
0813 }
0814 EXPORT_SYMBOL_GPL(sdio_retune_release);