sdio: add new function for RAW (Read after Write) operation
[safe/jmp/linux-2.6] / drivers / mmc / core / sdio_io.c
index cc42a41..0f687cd 100644 (file)
@@ -76,11 +76,7 @@ int sdio_enable_func(struct sdio_func *func)
        if (ret)
                goto err;
 
-       /*
-        * FIXME: This should timeout based on information in the CIS,
-        * but we don't have card to parse that yet.
-        */
-       timeout = jiffies + HZ;
+       timeout = jiffies + msecs_to_jiffies(func->enable_timeout);
 
        while (1) {
                ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IORx, 0, &reg);
@@ -167,10 +163,8 @@ int sdio_set_block_size(struct sdio_func *func, unsigned blksz)
                return -EINVAL;
 
        if (blksz == 0) {
-               blksz = min(min(
-                       func->max_blksize,
-                       func->card->host->max_blk_size),
-                       512u);
+               blksz = min(func->max_blksize, func->card->host->max_blk_size);
+               blksz = min(blksz, 512u);
        }
 
        ret = mmc_io_rw_direct(func->card, 1, 0,
@@ -193,11 +187,15 @@ EXPORT_SYMBOL_GPL(sdio_set_block_size);
  */
 static inline unsigned int sdio_max_byte_size(struct sdio_func *func)
 {
-       return min(min(min(
-               func->card->host->max_seg_size,
-               func->card->host->max_blk_size),
-               func->max_blksize),
-               512u); /* maximum size for byte mode */
+       unsigned mval = min(func->card->host->max_seg_size,
+                           func->card->host->max_blk_size);
+
+       if (mmc_blksz_for_byte_mode(func->card))
+               mval = min(mval, func->cur_blksize);
+       else
+               mval = min(mval, func->max_blksize);
+
+       return min(mval, 512u); /* maximum size for byte mode */
 }
 
 /**
@@ -311,10 +309,9 @@ static int sdio_io_rw_ext_helper(struct sdio_func *func, int write,
                /* Blocks per command is limited by host count, host transfer
                 * size (we only use a single sg entry) and the maximum for
                 * IO_RW_EXTENDED of 511 blocks. */
-               max_blocks = min(min(
-                       func->card->host->max_blk_count,
-                       func->card->host->max_seg_size / func->cur_blksize),
-                       511u);
+               max_blocks = min(func->card->host->max_blk_count,
+                       func->card->host->max_seg_size / func->cur_blksize);
+               max_blocks = min(max_blocks, 511u);
 
                while (remainder > func->cur_blksize) {
                        unsigned blocks;
@@ -364,11 +361,10 @@ static int sdio_io_rw_ext_helper(struct sdio_func *func, int write,
  *     function. If there is a problem reading the address, 0xff
  *     is returned and @err_ret will contain the error code.
  */
-unsigned char sdio_readb(struct sdio_func *func, unsigned int addr,
-       int *err_ret)
+u8 sdio_readb(struct sdio_func *func, unsigned int addr, int *err_ret)
 {
        int ret;
-       unsigned char val;
+       u8 val;
 
        BUG_ON(!func);
 
@@ -397,8 +393,7 @@ EXPORT_SYMBOL_GPL(sdio_readb);
  *     function. @err_ret will contain the status of the actual
  *     transfer.
  */
-void sdio_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
-       int *err_ret)
+void sdio_writeb(struct sdio_func *func, u8 b, unsigned int addr, int *err_ret)
 {
        int ret;
 
@@ -411,6 +406,36 @@ void sdio_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
 EXPORT_SYMBOL_GPL(sdio_writeb);
 
 /**
+ *     sdio_writeb_readb - write and read a byte from SDIO function
+ *     @func: SDIO function to access
+ *     @write_byte: byte to write
+ *     @addr: address to write to
+ *     @err_ret: optional status value from transfer
+ *
+ *     Performs a RAW (Read after Write) operation as defined by SDIO spec -
+ *     single byte is written to address space of a given SDIO function and
+ *     response is read back from the same address, both using single request.
+ *     If there is a problem with the operation, 0xff is returned and
+ *     @err_ret will contain the error code.
+ */
+u8 sdio_writeb_readb(struct sdio_func *func, u8 write_byte,
+       unsigned int addr, int *err_ret)
+{
+       int ret;
+       u8 val;
+
+       ret = mmc_io_rw_direct(func->card, 1, func->num, addr,
+                       write_byte, &val);
+       if (err_ret)
+               *err_ret = ret;
+       if (ret)
+               val = 0xff;
+
+       return val;
+}
+EXPORT_SYMBOL_GPL(sdio_writeb_readb);
+
+/**
  *     sdio_memcpy_fromio - read a chunk of memory from a SDIO function
  *     @func: SDIO function to access
  *     @dst: buffer to store the data
@@ -459,7 +484,6 @@ int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
 {
        return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count);
 }
-
 EXPORT_SYMBOL_GPL(sdio_readsb);
 
 /**
@@ -489,8 +513,7 @@ EXPORT_SYMBOL_GPL(sdio_writesb);
  *     function. If there is a problem reading the address, 0xffff
  *     is returned and @err_ret will contain the error code.
  */
-unsigned short sdio_readw(struct sdio_func *func, unsigned int addr,
-       int *err_ret)
+u16 sdio_readw(struct sdio_func *func, unsigned int addr, int *err_ret)
 {
        int ret;
 
@@ -504,7 +527,7 @@ unsigned short sdio_readw(struct sdio_func *func, unsigned int addr,
                return 0xFFFF;
        }
 
-       return le16_to_cpu(*(u16*)func->tmpbuf);
+       return le16_to_cpup((__le16 *)func->tmpbuf);
 }
 EXPORT_SYMBOL_GPL(sdio_readw);
 
@@ -519,12 +542,11 @@ EXPORT_SYMBOL_GPL(sdio_readw);
  *     function. @err_ret will contain the status of the actual
  *     transfer.
  */
-void sdio_writew(struct sdio_func *func, unsigned short b, unsigned int addr,
-       int *err_ret)
+void sdio_writew(struct sdio_func *func, u16 b, unsigned int addr, int *err_ret)
 {
        int ret;
 
-       *(u16*)func->tmpbuf = cpu_to_le16(b);
+       *(__le16 *)func->tmpbuf = cpu_to_le16(b);
 
        ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
        if (err_ret)
@@ -543,8 +565,7 @@ EXPORT_SYMBOL_GPL(sdio_writew);
  *     0xffffffff is returned and @err_ret will contain the error
  *     code.
  */
-unsigned long sdio_readl(struct sdio_func *func, unsigned int addr,
-       int *err_ret)
+u32 sdio_readl(struct sdio_func *func, unsigned int addr, int *err_ret)
 {
        int ret;
 
@@ -558,7 +579,7 @@ unsigned long sdio_readl(struct sdio_func *func, unsigned int addr,
                return 0xFFFFFFFF;
        }
 
-       return le32_to_cpu(*(u32*)func->tmpbuf);
+       return le32_to_cpup((__le32 *)func->tmpbuf);
 }
 EXPORT_SYMBOL_GPL(sdio_readl);
 
@@ -573,12 +594,11 @@ EXPORT_SYMBOL_GPL(sdio_readl);
  *     function. @err_ret will contain the status of the actual
  *     transfer.
  */
-void sdio_writel(struct sdio_func *func, unsigned long b, unsigned int addr,
-       int *err_ret)
+void sdio_writel(struct sdio_func *func, u32 b, unsigned int addr, int *err_ret)
 {
        int ret;
 
-       *(u32*)func->tmpbuf = cpu_to_le32(b);
+       *(__le32 *)func->tmpbuf = cpu_to_le32(b);
 
        ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
        if (err_ret)
@@ -639,7 +659,7 @@ void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
 
        BUG_ON(!func);
 
-       if (addr < 0xF0 || addr > 0xFF) {
+       if ((addr < 0xF0 || addr > 0xFF) && (!mmc_card_lenient_fn0(func->card))) {
                if (err_ret)
                        *err_ret = -EINVAL;
                return;
@@ -650,3 +670,52 @@ void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
                *err_ret = ret;
 }
 EXPORT_SYMBOL_GPL(sdio_f0_writeb);
+
+/**
+ *     sdio_get_host_pm_caps - get host power management capabilities
+ *     @func: SDIO function attached to host
+ *
+ *     Returns a capability bitmask corresponding to power management
+ *     features supported by the host controller that the card function
+ *     might rely upon during a system suspend.  The host doesn't need
+ *     to be claimed, nor the function active, for this information to be
+ *     obtained.
+ */
+mmc_pm_flag_t sdio_get_host_pm_caps(struct sdio_func *func)
+{
+       BUG_ON(!func);
+       BUG_ON(!func->card);
+
+       return func->card->host->pm_caps;
+}
+EXPORT_SYMBOL_GPL(sdio_get_host_pm_caps);
+
+/**
+ *     sdio_set_host_pm_flags - set wanted host power management capabilities
+ *     @func: SDIO function attached to host
+ *
+ *     Set a capability bitmask corresponding to wanted host controller
+ *     power management features for the upcoming suspend state.
+ *     This must be called, if needed, each time the suspend method of
+ *     the function driver is called, and must contain only bits that
+ *     were returned by sdio_get_host_pm_caps().
+ *     The host doesn't need to be claimed, nor the function active,
+ *     for this information to be set.
+ */
+int sdio_set_host_pm_flags(struct sdio_func *func, mmc_pm_flag_t flags)
+{
+       struct mmc_host *host;
+
+       BUG_ON(!func);
+       BUG_ON(!func->card);
+
+       host = func->card->host;
+
+       if (flags & ~host->pm_caps)
+               return -EINVAL;
+
+       /* function suspend methods are serialized, hence no lock needed */
+       host->pm_flags |= flags;
+       return 0;
+}
+EXPORT_SYMBOL_GPL(sdio_set_host_pm_flags);