2 * linux/drivers/mmc/core/sdio_io.c
4 * Copyright 2007-2008 Pierre Ossman
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.
12 #include <linux/export.h>
13 #include <linux/mmc/host.h>
14 #include <linux/mmc/card.h>
15 #include <linux/mmc/sdio.h>
16 #include <linux/mmc/sdio_func.h>
24 * sdio_claim_host - exclusively claim a bus for a certain SDIO function
25 * @func: SDIO function that will be accessed
27 * Claim a bus for a set of operations. The SDIO function given
28 * is used to figure out which bus is relevant.
30 void sdio_claim_host(struct sdio_func
*func
)
35 mmc_claim_host(func
->card
->host
);
37 EXPORT_SYMBOL_GPL(sdio_claim_host
);
40 * sdio_release_host - release a bus for a certain SDIO function
41 * @func: SDIO function that was accessed
43 * Release a bus, allowing others to claim the bus for their
46 void sdio_release_host(struct sdio_func
*func
)
51 mmc_release_host(func
->card
->host
);
53 EXPORT_SYMBOL_GPL(sdio_release_host
);
56 * sdio_enable_func - enables a SDIO function for usage
57 * @func: SDIO function to enable
59 * Powers up and activates a SDIO function so that register
62 int sdio_enable_func(struct sdio_func
*func
)
66 unsigned long timeout
;
71 pr_debug("SDIO: Enabling device %s...\n", sdio_func_id(func
));
73 ret
= mmc_io_rw_direct(func
->card
, 0, 0, SDIO_CCCR_IOEx
, 0, ®
);
77 reg
|= 1 << func
->num
;
79 ret
= mmc_io_rw_direct(func
->card
, 1, 0, SDIO_CCCR_IOEx
, reg
, NULL
);
83 timeout
= jiffies
+ msecs_to_jiffies(func
->enable_timeout
);
86 ret
= mmc_io_rw_direct(func
->card
, 0, 0, SDIO_CCCR_IORx
, 0, ®
);
89 if (reg
& (1 << func
->num
))
92 if (time_after(jiffies
, timeout
))
96 pr_debug("SDIO: Enabled device %s\n", sdio_func_id(func
));
101 pr_debug("SDIO: Failed to enable device %s\n", sdio_func_id(func
));
104 EXPORT_SYMBOL_GPL(sdio_enable_func
);
107 * sdio_disable_func - disable a SDIO function
108 * @func: SDIO function to disable
110 * Powers down and deactivates a SDIO function. Register access
111 * to this function will fail until the function is reenabled.
113 int sdio_disable_func(struct sdio_func
*func
)
121 pr_debug("SDIO: Disabling device %s...\n", sdio_func_id(func
));
123 ret
= mmc_io_rw_direct(func
->card
, 0, 0, SDIO_CCCR_IOEx
, 0, ®
);
127 reg
&= ~(1 << func
->num
);
129 ret
= mmc_io_rw_direct(func
->card
, 1, 0, SDIO_CCCR_IOEx
, reg
, NULL
);
133 pr_debug("SDIO: Disabled device %s\n", sdio_func_id(func
));
138 pr_debug("SDIO: Failed to disable device %s\n", sdio_func_id(func
));
141 EXPORT_SYMBOL_GPL(sdio_disable_func
);
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.
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.
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.
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.
162 int sdio_set_block_size(struct sdio_func
*func
, unsigned blksz
)
166 if (blksz
> func
->card
->host
->max_blk_size
)
170 blksz
= min(func
->max_blksize
, func
->card
->host
->max_blk_size
);
171 blksz
= min(blksz
, 512u);
174 ret
= mmc_io_rw_direct(func
->card
, 1, 0,
175 SDIO_FBR_BASE(func
->num
) + SDIO_FBR_BLKSIZE
,
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
);
184 func
->cur_blksize
= blksz
;
187 EXPORT_SYMBOL_GPL(sdio_set_block_size
);
190 * Calculate the maximum byte mode transfer size
192 static inline unsigned int sdio_max_byte_size(struct sdio_func
*func
)
194 unsigned mval
= func
->card
->host
->max_blk_size
;
196 if (mmc_blksz_for_byte_mode(func
->card
))
197 mval
= min(mval
, func
->cur_blksize
);
199 mval
= min(mval
, func
->max_blksize
);
201 if (mmc_card_broken_byte_mode_512(func
->card
))
202 return min(mval
, 511u);
204 return min(mval
, 512u); /* maximum size for byte mode */
208 * sdio_align_size - pads a transfer size to a more optimal value
209 * @func: SDIO function
210 * @sz: original transfer size
212 * Pads the original data size with a number of extra bytes in
213 * order to avoid controller bugs and/or performance hits
214 * (e.g. some controllers revert to PIO for certain sizes).
216 * If possible, it will also adjust the size so that it can be
217 * handled in just a single request.
219 * Returns the improved size, which might be unmodified.
221 unsigned int sdio_align_size(struct sdio_func
*func
, unsigned int sz
)
223 unsigned int orig_sz
;
224 unsigned int blk_sz
, byte_sz
;
230 * Do a first check with the controller, in case it
231 * wants to increase the size up to a point where it
232 * might need more than one block.
234 sz
= mmc_align_data_size(func
->card
, sz
);
237 * If we can still do this with just a byte transfer, then
240 if (sz
<= sdio_max_byte_size(func
))
243 if (func
->card
->cccr
.multi_block
) {
245 * Check if the transfer is already block aligned
247 if ((sz
% func
->cur_blksize
) == 0)
251 * Realign it so that it can be done with one request,
252 * and recheck if the controller still likes it.
254 blk_sz
= ((sz
+ func
->cur_blksize
- 1) /
255 func
->cur_blksize
) * func
->cur_blksize
;
256 blk_sz
= mmc_align_data_size(func
->card
, blk_sz
);
259 * This value is only good if it is still just
262 if ((blk_sz
% func
->cur_blksize
) == 0)
266 * We failed to do one request, but at least try to
267 * pad the remainder properly.
269 byte_sz
= mmc_align_data_size(func
->card
,
270 sz
% func
->cur_blksize
);
271 if (byte_sz
<= sdio_max_byte_size(func
)) {
272 blk_sz
= sz
/ func
->cur_blksize
;
273 return blk_sz
* func
->cur_blksize
+ byte_sz
;
277 * We need multiple requests, so first check that the
278 * controller can handle the chunk size;
280 chunk_sz
= mmc_align_data_size(func
->card
,
281 sdio_max_byte_size(func
));
282 if (chunk_sz
== sdio_max_byte_size(func
)) {
284 * Fix up the size of the remainder (if any)
286 byte_sz
= orig_sz
% chunk_sz
;
288 byte_sz
= mmc_align_data_size(func
->card
,
292 return (orig_sz
/ chunk_sz
) * chunk_sz
+ byte_sz
;
297 * The controller is simply incapable of transferring the size
298 * we want in decent manner, so just return the original size.
302 EXPORT_SYMBOL_GPL(sdio_align_size
);
304 /* Split an arbitrarily sized data transfer into several
305 * IO_RW_EXTENDED commands. */
306 static int sdio_io_rw_ext_helper(struct sdio_func
*func
, int write
,
307 unsigned addr
, int incr_addr
, u8
*buf
, unsigned size
)
309 unsigned remainder
= size
;
313 if (!func
|| (func
->num
> 7))
316 /* Do the bulk of the transfer using block mode (if supported). */
317 if (func
->card
->cccr
.multi_block
&& (size
> sdio_max_byte_size(func
))) {
318 /* Blocks per command is limited by host count, host transfer
319 * size and the maximum for IO_RW_EXTENDED of 511 blocks. */
320 max_blocks
= min(func
->card
->host
->max_blk_count
, 511u);
322 while (remainder
>= func
->cur_blksize
) {
325 blocks
= remainder
/ func
->cur_blksize
;
326 if (blocks
> max_blocks
)
328 size
= blocks
* func
->cur_blksize
;
330 ret
= mmc_io_rw_extended(func
->card
, write
,
331 func
->num
, addr
, incr_addr
, buf
,
332 blocks
, func
->cur_blksize
);
343 /* Write the remainder using byte mode. */
344 while (remainder
> 0) {
345 size
= min(remainder
, sdio_max_byte_size(func
));
347 /* Indicate byte mode by setting "blocks" = 0 */
348 ret
= mmc_io_rw_extended(func
->card
, write
, func
->num
, addr
,
349 incr_addr
, buf
, 0, size
);
362 * sdio_readb - read a single byte from a SDIO function
363 * @func: SDIO function to access
364 * @addr: address to read
365 * @err_ret: optional status value from transfer
367 * Reads a single byte from the address space of a given SDIO
368 * function. If there is a problem reading the address, 0xff
369 * is returned and @err_ret will contain the error code.
371 u8
sdio_readb(struct sdio_func
*func
, unsigned int addr
, int *err_ret
)
382 ret
= mmc_io_rw_direct(func
->card
, 0, func
->num
, addr
, 0, &val
);
390 EXPORT_SYMBOL_GPL(sdio_readb
);
393 * sdio_writeb - write a single byte to a SDIO function
394 * @func: SDIO function to access
396 * @addr: address to write to
397 * @err_ret: optional status value from transfer
399 * Writes a single byte to the address space of a given SDIO
400 * function. @err_ret will contain the status of the actual
403 void sdio_writeb(struct sdio_func
*func
, u8 b
, unsigned int addr
, int *err_ret
)
413 ret
= mmc_io_rw_direct(func
->card
, 1, func
->num
, addr
, b
, NULL
);
417 EXPORT_SYMBOL_GPL(sdio_writeb
);
420 * sdio_writeb_readb - write and read a byte from SDIO function
421 * @func: SDIO function to access
422 * @write_byte: byte to write
423 * @addr: address to write to
424 * @err_ret: optional status value from transfer
426 * Performs a RAW (Read after Write) operation as defined by SDIO spec -
427 * single byte is written to address space of a given SDIO function and
428 * response is read back from the same address, both using single request.
429 * If there is a problem with the operation, 0xff is returned and
430 * @err_ret will contain the error code.
432 u8
sdio_writeb_readb(struct sdio_func
*func
, u8 write_byte
,
433 unsigned int addr
, int *err_ret
)
438 ret
= mmc_io_rw_direct(func
->card
, 1, func
->num
, addr
,
447 EXPORT_SYMBOL_GPL(sdio_writeb_readb
);
450 * sdio_memcpy_fromio - read a chunk of memory from a SDIO function
451 * @func: SDIO function to access
452 * @dst: buffer to store the data
453 * @addr: address to begin reading from
454 * @count: number of bytes to read
456 * Reads from the address space of a given SDIO function. Return
457 * value indicates if the transfer succeeded or not.
459 int sdio_memcpy_fromio(struct sdio_func
*func
, void *dst
,
460 unsigned int addr
, int count
)
462 return sdio_io_rw_ext_helper(func
, 0, addr
, 1, dst
, count
);
464 EXPORT_SYMBOL_GPL(sdio_memcpy_fromio
);
467 * sdio_memcpy_toio - write a chunk of memory to a SDIO function
468 * @func: SDIO function to access
469 * @addr: address to start writing to
470 * @src: buffer that contains the data to write
471 * @count: number of bytes to write
473 * Writes to the address space of a given SDIO function. Return
474 * value indicates if the transfer succeeded or not.
476 int sdio_memcpy_toio(struct sdio_func
*func
, unsigned int addr
,
477 void *src
, int count
)
479 return sdio_io_rw_ext_helper(func
, 1, addr
, 1, src
, count
);
481 EXPORT_SYMBOL_GPL(sdio_memcpy_toio
);
484 * sdio_readsb - read from a FIFO on a SDIO function
485 * @func: SDIO function to access
486 * @dst: buffer to store the data
487 * @addr: address of (single byte) FIFO
488 * @count: number of bytes to read
490 * Reads from the specified FIFO of a given SDIO function. Return
491 * value indicates if the transfer succeeded or not.
493 int sdio_readsb(struct sdio_func
*func
, void *dst
, unsigned int addr
,
496 return sdio_io_rw_ext_helper(func
, 0, addr
, 0, dst
, count
);
498 EXPORT_SYMBOL_GPL(sdio_readsb
);
501 * sdio_writesb - write to a FIFO of a SDIO function
502 * @func: SDIO function to access
503 * @addr: address of (single byte) FIFO
504 * @src: buffer that contains the data to write
505 * @count: number of bytes to write
507 * Writes to the specified FIFO of a given SDIO function. Return
508 * value indicates if the transfer succeeded or not.
510 int sdio_writesb(struct sdio_func
*func
, unsigned int addr
, void *src
,
513 return sdio_io_rw_ext_helper(func
, 1, addr
, 0, src
, count
);
515 EXPORT_SYMBOL_GPL(sdio_writesb
);
518 * sdio_readw - read a 16 bit integer from a SDIO function
519 * @func: SDIO function to access
520 * @addr: address to read
521 * @err_ret: optional status value from transfer
523 * Reads a 16 bit integer from the address space of a given SDIO
524 * function. If there is a problem reading the address, 0xffff
525 * is returned and @err_ret will contain the error code.
527 u16
sdio_readw(struct sdio_func
*func
, unsigned int addr
, int *err_ret
)
531 ret
= sdio_memcpy_fromio(func
, func
->tmpbuf
, addr
, 2);
537 return le16_to_cpup((__le16
*)func
->tmpbuf
);
539 EXPORT_SYMBOL_GPL(sdio_readw
);
542 * sdio_writew - write a 16 bit integer to a SDIO function
543 * @func: SDIO function to access
544 * @b: integer to write
545 * @addr: address to write to
546 * @err_ret: optional status value from transfer
548 * Writes a 16 bit integer to the address space of a given SDIO
549 * function. @err_ret will contain the status of the actual
552 void sdio_writew(struct sdio_func
*func
, u16 b
, unsigned int addr
, int *err_ret
)
556 *(__le16
*)func
->tmpbuf
= cpu_to_le16(b
);
558 ret
= sdio_memcpy_toio(func
, addr
, func
->tmpbuf
, 2);
562 EXPORT_SYMBOL_GPL(sdio_writew
);
565 * sdio_readl - read a 32 bit integer from a SDIO function
566 * @func: SDIO function to access
567 * @addr: address to read
568 * @err_ret: optional status value from transfer
570 * Reads a 32 bit integer from the address space of a given SDIO
571 * function. If there is a problem reading the address,
572 * 0xffffffff is returned and @err_ret will contain the error
575 u32
sdio_readl(struct sdio_func
*func
, unsigned int addr
, int *err_ret
)
579 ret
= sdio_memcpy_fromio(func
, func
->tmpbuf
, addr
, 4);
585 return le32_to_cpup((__le32
*)func
->tmpbuf
);
587 EXPORT_SYMBOL_GPL(sdio_readl
);
590 * sdio_writel - write a 32 bit integer to a SDIO function
591 * @func: SDIO function to access
592 * @b: integer to write
593 * @addr: address to write to
594 * @err_ret: optional status value from transfer
596 * Writes a 32 bit integer to the address space of a given SDIO
597 * function. @err_ret will contain the status of the actual
600 void sdio_writel(struct sdio_func
*func
, u32 b
, unsigned int addr
, int *err_ret
)
604 *(__le32
*)func
->tmpbuf
= cpu_to_le32(b
);
606 ret
= sdio_memcpy_toio(func
, addr
, func
->tmpbuf
, 4);
610 EXPORT_SYMBOL_GPL(sdio_writel
);
613 * sdio_f0_readb - read a single byte from SDIO function 0
614 * @func: an SDIO function of the card
615 * @addr: address to read
616 * @err_ret: optional status value from transfer
618 * Reads a single byte from the address space of SDIO function 0.
619 * If there is a problem reading the address, 0xff is returned
620 * and @err_ret will contain the error code.
622 unsigned char sdio_f0_readb(struct sdio_func
*func
, unsigned int addr
,
634 ret
= mmc_io_rw_direct(func
->card
, 0, 0, addr
, 0, &val
);
642 EXPORT_SYMBOL_GPL(sdio_f0_readb
);
645 * sdio_f0_writeb - write a single byte to SDIO function 0
646 * @func: an SDIO function of the card
648 * @addr: address to write to
649 * @err_ret: optional status value from transfer
651 * Writes a single byte to the address space of SDIO function 0.
652 * @err_ret will contain the status of the actual transfer.
654 * Only writes to the vendor specific CCCR registers (0xF0 -
655 * 0xFF) are permiited; @err_ret will be set to -EINVAL for *
656 * writes outside this range.
658 void sdio_f0_writeb(struct sdio_func
*func
, unsigned char b
, unsigned int addr
,
669 if ((addr
< 0xF0 || addr
> 0xFF) && (!mmc_card_lenient_fn0(func
->card
))) {
675 ret
= mmc_io_rw_direct(func
->card
, 1, 0, addr
, b
, NULL
);
679 EXPORT_SYMBOL_GPL(sdio_f0_writeb
);
682 * sdio_get_host_pm_caps - get host power management capabilities
683 * @func: SDIO function attached to host
685 * Returns a capability bitmask corresponding to power management
686 * features supported by the host controller that the card function
687 * might rely upon during a system suspend. The host doesn't need
688 * to be claimed, nor the function active, for this information to be
691 mmc_pm_flag_t
sdio_get_host_pm_caps(struct sdio_func
*func
)
696 return func
->card
->host
->pm_caps
;
698 EXPORT_SYMBOL_GPL(sdio_get_host_pm_caps
);
701 * sdio_set_host_pm_flags - set wanted host power management capabilities
702 * @func: SDIO function attached to host
704 * Set a capability bitmask corresponding to wanted host controller
705 * power management features for the upcoming suspend state.
706 * This must be called, if needed, each time the suspend method of
707 * the function driver is called, and must contain only bits that
708 * were returned by sdio_get_host_pm_caps().
709 * The host doesn't need to be claimed, nor the function active,
710 * for this information to be set.
712 int sdio_set_host_pm_flags(struct sdio_func
*func
, mmc_pm_flag_t flags
)
714 struct mmc_host
*host
;
719 host
= func
->card
->host
;
721 if (flags
& ~host
->pm_caps
)
724 /* function suspend methods are serialized, hence no lock needed */
725 host
->pm_flags
|= flags
;
728 EXPORT_SYMBOL_GPL(sdio_set_host_pm_flags
);
731 * sdio_retune_crc_disable - temporarily disable retuning on CRC errors
732 * @func: SDIO function attached to host
734 * If the SDIO card is known to be in a state where it might produce
735 * CRC errors on the bus in response to commands (like if we know it is
736 * transitioning between power states), an SDIO function driver can
737 * call this function to temporarily disable the SD/MMC core behavior of
738 * triggering an automatic retuning.
740 * This function should be called while the host is claimed and the host
741 * should remain claimed until sdio_retune_crc_enable() is called.
742 * Specifically, the expected sequence of calls is:
743 * - sdio_claim_host()
744 * - sdio_retune_crc_disable()
745 * - some number of calls like sdio_writeb() and sdio_readb()
746 * - sdio_retune_crc_enable()
747 * - sdio_release_host()
749 void sdio_retune_crc_disable(struct sdio_func
*func
)
751 func
->card
->host
->retune_crc_disable
= true;
753 EXPORT_SYMBOL_GPL(sdio_retune_crc_disable
);
756 * sdio_retune_crc_enable - re-enable retuning on CRC errors
757 * @func: SDIO function attached to host
759 * This is the compement to sdio_retune_crc_disable().
761 void sdio_retune_crc_enable(struct sdio_func
*func
)
763 func
->card
->host
->retune_crc_disable
= false;
765 EXPORT_SYMBOL_GPL(sdio_retune_crc_enable
);
768 * sdio_retune_hold_now - start deferring retuning requests till release
769 * @func: SDIO function attached to host
771 * This function can be called if it's currently a bad time to do
772 * a retune of the SDIO card. Retune requests made during this time
773 * will be held and we'll actually do the retune sometime after the
776 * This function could be useful if an SDIO card is in a power state
777 * where it can respond to a small subset of commands that doesn't
778 * include the retuning command. Care should be taken when using
779 * this function since (presumably) the retuning request we might be
780 * deferring was made for a good reason.
782 * This function should be called while the host is claimed.
784 void sdio_retune_hold_now(struct sdio_func
*func
)
786 mmc_retune_hold_now(func
->card
->host
);
788 EXPORT_SYMBOL_GPL(sdio_retune_hold_now
);
791 * sdio_retune_release - signal that it's OK to retune now
792 * @func: SDIO function attached to host
794 * This is the complement to sdio_retune_hold_now(). Calling this
795 * function won't make a retune happen right away but will allow
796 * them to be scheduled normally.
798 * This function should be called while the host is claimed.
800 void sdio_retune_release(struct sdio_func
*func
)
802 mmc_retune_release(func
->card
->host
);
804 EXPORT_SYMBOL_GPL(sdio_retune_release
);