1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * linux/drivers/mmc/core/sdio_io.c
5 * Copyright 2007-2008 Pierre Ossman
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>
21 * sdio_claim_host - exclusively claim a bus for a certain SDIO function
22 * @func: SDIO function that will be accessed
24 * Claim a bus for a set of operations. The SDIO function given
25 * is used to figure out which bus is relevant.
27 void sdio_claim_host(struct sdio_func
*func
)
32 mmc_claim_host(func
->card
->host
);
34 EXPORT_SYMBOL_GPL(sdio_claim_host
);
37 * sdio_release_host - release a bus for a certain SDIO function
38 * @func: SDIO function that was accessed
40 * Release a bus, allowing others to claim the bus for their
43 void sdio_release_host(struct sdio_func
*func
)
48 mmc_release_host(func
->card
->host
);
50 EXPORT_SYMBOL_GPL(sdio_release_host
);
53 * sdio_enable_func - enables a SDIO function for usage
54 * @func: SDIO function to enable
56 * Powers up and activates a SDIO function so that register
59 int sdio_enable_func(struct sdio_func
*func
)
63 unsigned long timeout
;
68 pr_debug("SDIO: Enabling device %s...\n", sdio_func_id(func
));
70 ret
= mmc_io_rw_direct(func
->card
, 0, 0, SDIO_CCCR_IOEx
, 0, ®
);
74 reg
|= 1 << func
->num
;
76 ret
= mmc_io_rw_direct(func
->card
, 1, 0, SDIO_CCCR_IOEx
, reg
, NULL
);
80 timeout
= jiffies
+ msecs_to_jiffies(func
->enable_timeout
);
83 ret
= mmc_io_rw_direct(func
->card
, 0, 0, SDIO_CCCR_IORx
, 0, ®
);
86 if (reg
& (1 << func
->num
))
89 if (time_after(jiffies
, timeout
))
93 pr_debug("SDIO: Enabled device %s\n", sdio_func_id(func
));
98 pr_debug("SDIO: Failed to enable device %s\n", sdio_func_id(func
));
101 EXPORT_SYMBOL_GPL(sdio_enable_func
);
104 * sdio_disable_func - disable a SDIO function
105 * @func: SDIO function to disable
107 * Powers down and deactivates a SDIO function. Register access
108 * to this function will fail until the function is reenabled.
110 int sdio_disable_func(struct sdio_func
*func
)
118 pr_debug("SDIO: Disabling device %s...\n", sdio_func_id(func
));
120 ret
= mmc_io_rw_direct(func
->card
, 0, 0, SDIO_CCCR_IOEx
, 0, ®
);
124 reg
&= ~(1 << func
->num
);
126 ret
= mmc_io_rw_direct(func
->card
, 1, 0, SDIO_CCCR_IOEx
, reg
, NULL
);
130 pr_debug("SDIO: Disabled device %s\n", sdio_func_id(func
));
135 pr_debug("SDIO: Failed to disable device %s\n", sdio_func_id(func
));
138 EXPORT_SYMBOL_GPL(sdio_disable_func
);
141 * sdio_set_block_size - set the block size of an SDIO function
142 * @func: SDIO function to change
143 * @blksz: new block size or 0 to use the default.
145 * The default block size is the largest supported by both the function
146 * and the host, with a maximum of 512 to ensure that arbitrarily sized
147 * data transfer use the optimal (least) number of commands.
149 * A driver may call this to override the default block size set by the
150 * core. This can be used to set a block size greater than the maximum
151 * that reported by the card; it is the driver's responsibility to ensure
152 * it uses a value that the card supports.
154 * Returns 0 on success, -EINVAL if the host does not support the
155 * requested block size, or -EIO (etc.) if one of the resultant FBR block
156 * size register writes failed.
159 int sdio_set_block_size(struct sdio_func
*func
, unsigned blksz
)
163 if (blksz
> func
->card
->host
->max_blk_size
)
167 blksz
= min(func
->max_blksize
, func
->card
->host
->max_blk_size
);
168 blksz
= min(blksz
, 512u);
171 ret
= mmc_io_rw_direct(func
->card
, 1, 0,
172 SDIO_FBR_BASE(func
->num
) + SDIO_FBR_BLKSIZE
,
176 ret
= mmc_io_rw_direct(func
->card
, 1, 0,
177 SDIO_FBR_BASE(func
->num
) + SDIO_FBR_BLKSIZE
+ 1,
178 (blksz
>> 8) & 0xff, NULL
);
181 func
->cur_blksize
= blksz
;
184 EXPORT_SYMBOL_GPL(sdio_set_block_size
);
187 * Calculate the maximum byte mode transfer size
189 static inline unsigned int sdio_max_byte_size(struct sdio_func
*func
)
191 unsigned mval
= func
->card
->host
->max_blk_size
;
193 if (mmc_blksz_for_byte_mode(func
->card
))
194 mval
= min(mval
, func
->cur_blksize
);
196 mval
= min(mval
, func
->max_blksize
);
198 if (mmc_card_broken_byte_mode_512(func
->card
))
199 return min(mval
, 511u);
201 return min(mval
, 512u); /* maximum size for byte mode */
205 * This is legacy code, which needs to be re-worked some day. Basically we need
206 * to take into account the properties of the host, as to enable the SDIO func
207 * driver layer to allocate optimal buffers.
209 static inline unsigned int _sdio_align_size(unsigned int sz
)
212 * FIXME: We don't have a system for the controller to tell
213 * the core about its problems yet, so for now we just 32-bit
220 * sdio_align_size - pads a transfer size to a more optimal value
221 * @func: SDIO function
222 * @sz: original transfer size
224 * Pads the original data size with a number of extra bytes in
225 * order to avoid controller bugs and/or performance hits
226 * (e.g. some controllers revert to PIO for certain sizes).
228 * If possible, it will also adjust the size so that it can be
229 * handled in just a single request.
231 * Returns the improved size, which might be unmodified.
233 unsigned int sdio_align_size(struct sdio_func
*func
, unsigned int sz
)
235 unsigned int orig_sz
;
236 unsigned int blk_sz
, byte_sz
;
242 * Do a first check with the controller, in case it
243 * wants to increase the size up to a point where it
244 * might need more than one block.
246 sz
= _sdio_align_size(sz
);
249 * If we can still do this with just a byte transfer, then
252 if (sz
<= sdio_max_byte_size(func
))
255 if (func
->card
->cccr
.multi_block
) {
257 * Check if the transfer is already block aligned
259 if ((sz
% func
->cur_blksize
) == 0)
263 * Realign it so that it can be done with one request,
264 * and recheck if the controller still likes it.
266 blk_sz
= ((sz
+ func
->cur_blksize
- 1) /
267 func
->cur_blksize
) * func
->cur_blksize
;
268 blk_sz
= _sdio_align_size(blk_sz
);
271 * This value is only good if it is still just
274 if ((blk_sz
% func
->cur_blksize
) == 0)
278 * We failed to do one request, but at least try to
279 * pad the remainder properly.
281 byte_sz
= _sdio_align_size(sz
% func
->cur_blksize
);
282 if (byte_sz
<= sdio_max_byte_size(func
)) {
283 blk_sz
= sz
/ func
->cur_blksize
;
284 return blk_sz
* func
->cur_blksize
+ byte_sz
;
288 * We need multiple requests, so first check that the
289 * controller can handle the chunk size;
291 chunk_sz
= _sdio_align_size(sdio_max_byte_size(func
));
292 if (chunk_sz
== sdio_max_byte_size(func
)) {
294 * Fix up the size of the remainder (if any)
296 byte_sz
= orig_sz
% chunk_sz
;
298 byte_sz
= _sdio_align_size(byte_sz
);
301 return (orig_sz
/ chunk_sz
) * chunk_sz
+ byte_sz
;
306 * The controller is simply incapable of transferring the size
307 * we want in decent manner, so just return the original size.
311 EXPORT_SYMBOL_GPL(sdio_align_size
);
313 /* Split an arbitrarily sized data transfer into several
314 * IO_RW_EXTENDED commands. */
315 static int sdio_io_rw_ext_helper(struct sdio_func
*func
, int write
,
316 unsigned addr
, int incr_addr
, u8
*buf
, unsigned size
)
318 unsigned remainder
= size
;
322 if (!func
|| (func
->num
> 7))
325 /* Do the bulk of the transfer using block mode (if supported). */
326 if (func
->card
->cccr
.multi_block
&& (size
> sdio_max_byte_size(func
))) {
327 /* Blocks per command is limited by host count, host transfer
328 * size and the maximum for IO_RW_EXTENDED of 511 blocks. */
329 max_blocks
= min(func
->card
->host
->max_blk_count
, 511u);
331 while (remainder
>= func
->cur_blksize
) {
334 blocks
= remainder
/ func
->cur_blksize
;
335 if (blocks
> max_blocks
)
337 size
= blocks
* func
->cur_blksize
;
339 ret
= mmc_io_rw_extended(func
->card
, write
,
340 func
->num
, addr
, incr_addr
, buf
,
341 blocks
, func
->cur_blksize
);
352 /* Write the remainder using byte mode. */
353 while (remainder
> 0) {
354 size
= min(remainder
, sdio_max_byte_size(func
));
356 /* Indicate byte mode by setting "blocks" = 0 */
357 ret
= mmc_io_rw_extended(func
->card
, write
, func
->num
, addr
,
358 incr_addr
, buf
, 0, size
);
371 * sdio_readb - read a single byte from a SDIO function
372 * @func: SDIO function to access
373 * @addr: address to read
374 * @err_ret: optional status value from transfer
376 * Reads a single byte from the address space of a given SDIO
377 * function. If there is a problem reading the address, 0xff
378 * is returned and @err_ret will contain the error code.
380 u8
sdio_readb(struct sdio_func
*func
, unsigned int addr
, int *err_ret
)
391 ret
= mmc_io_rw_direct(func
->card
, 0, func
->num
, addr
, 0, &val
);
399 EXPORT_SYMBOL_GPL(sdio_readb
);
402 * sdio_writeb - write a single byte to a SDIO function
403 * @func: SDIO function to access
405 * @addr: address to write to
406 * @err_ret: optional status value from transfer
408 * Writes a single byte to the address space of a given SDIO
409 * function. @err_ret will contain the status of the actual
412 void sdio_writeb(struct sdio_func
*func
, u8 b
, unsigned int addr
, int *err_ret
)
422 ret
= mmc_io_rw_direct(func
->card
, 1, func
->num
, addr
, b
, NULL
);
426 EXPORT_SYMBOL_GPL(sdio_writeb
);
429 * sdio_writeb_readb - write and read a byte from SDIO function
430 * @func: SDIO function to access
431 * @write_byte: byte to write
432 * @addr: address to write to
433 * @err_ret: optional status value from transfer
435 * Performs a RAW (Read after Write) operation as defined by SDIO spec -
436 * single byte is written to address space of a given SDIO function and
437 * response is read back from the same address, both using single request.
438 * If there is a problem with the operation, 0xff is returned and
439 * @err_ret will contain the error code.
441 u8
sdio_writeb_readb(struct sdio_func
*func
, u8 write_byte
,
442 unsigned int addr
, int *err_ret
)
447 ret
= mmc_io_rw_direct(func
->card
, 1, func
->num
, addr
,
456 EXPORT_SYMBOL_GPL(sdio_writeb_readb
);
459 * sdio_memcpy_fromio - read a chunk of memory from a SDIO function
460 * @func: SDIO function to access
461 * @dst: buffer to store the data
462 * @addr: address to begin reading from
463 * @count: number of bytes to read
465 * Reads from the address space of a given SDIO function. Return
466 * value indicates if the transfer succeeded or not.
468 int sdio_memcpy_fromio(struct sdio_func
*func
, void *dst
,
469 unsigned int addr
, int count
)
471 return sdio_io_rw_ext_helper(func
, 0, addr
, 1, dst
, count
);
473 EXPORT_SYMBOL_GPL(sdio_memcpy_fromio
);
476 * sdio_memcpy_toio - write a chunk of memory to a SDIO function
477 * @func: SDIO function to access
478 * @addr: address to start writing to
479 * @src: buffer that contains the data to write
480 * @count: number of bytes to write
482 * Writes to the address space of a given SDIO function. Return
483 * value indicates if the transfer succeeded or not.
485 int sdio_memcpy_toio(struct sdio_func
*func
, unsigned int addr
,
486 void *src
, int count
)
488 return sdio_io_rw_ext_helper(func
, 1, addr
, 1, src
, count
);
490 EXPORT_SYMBOL_GPL(sdio_memcpy_toio
);
493 * sdio_readsb - read from a FIFO on a SDIO function
494 * @func: SDIO function to access
495 * @dst: buffer to store the data
496 * @addr: address of (single byte) FIFO
497 * @count: number of bytes to read
499 * Reads from the specified FIFO of a given SDIO function. Return
500 * value indicates if the transfer succeeded or not.
502 int sdio_readsb(struct sdio_func
*func
, void *dst
, unsigned int addr
,
505 return sdio_io_rw_ext_helper(func
, 0, addr
, 0, dst
, count
);
507 EXPORT_SYMBOL_GPL(sdio_readsb
);
510 * sdio_writesb - write to a FIFO of a SDIO function
511 * @func: SDIO function to access
512 * @addr: address of (single byte) FIFO
513 * @src: buffer that contains the data to write
514 * @count: number of bytes to write
516 * Writes to the specified FIFO of a given SDIO function. Return
517 * value indicates if the transfer succeeded or not.
519 int sdio_writesb(struct sdio_func
*func
, unsigned int addr
, void *src
,
522 return sdio_io_rw_ext_helper(func
, 1, addr
, 0, src
, count
);
524 EXPORT_SYMBOL_GPL(sdio_writesb
);
527 * sdio_readw - read a 16 bit integer from a SDIO function
528 * @func: SDIO function to access
529 * @addr: address to read
530 * @err_ret: optional status value from transfer
532 * Reads a 16 bit integer from the address space of a given SDIO
533 * function. If there is a problem reading the address, 0xffff
534 * is returned and @err_ret will contain the error code.
536 u16
sdio_readw(struct sdio_func
*func
, unsigned int addr
, int *err_ret
)
540 ret
= sdio_memcpy_fromio(func
, func
->tmpbuf
, addr
, 2);
546 return le16_to_cpup((__le16
*)func
->tmpbuf
);
548 EXPORT_SYMBOL_GPL(sdio_readw
);
551 * sdio_writew - write a 16 bit integer to a SDIO function
552 * @func: SDIO function to access
553 * @b: integer to write
554 * @addr: address to write to
555 * @err_ret: optional status value from transfer
557 * Writes a 16 bit integer to the address space of a given SDIO
558 * function. @err_ret will contain the status of the actual
561 void sdio_writew(struct sdio_func
*func
, u16 b
, unsigned int addr
, int *err_ret
)
565 *(__le16
*)func
->tmpbuf
= cpu_to_le16(b
);
567 ret
= sdio_memcpy_toio(func
, addr
, func
->tmpbuf
, 2);
571 EXPORT_SYMBOL_GPL(sdio_writew
);
574 * sdio_readl - read a 32 bit integer from a SDIO function
575 * @func: SDIO function to access
576 * @addr: address to read
577 * @err_ret: optional status value from transfer
579 * Reads a 32 bit integer from the address space of a given SDIO
580 * function. If there is a problem reading the address,
581 * 0xffffffff is returned and @err_ret will contain the error
584 u32
sdio_readl(struct sdio_func
*func
, unsigned int addr
, int *err_ret
)
588 ret
= sdio_memcpy_fromio(func
, func
->tmpbuf
, addr
, 4);
594 return le32_to_cpup((__le32
*)func
->tmpbuf
);
596 EXPORT_SYMBOL_GPL(sdio_readl
);
599 * sdio_writel - write a 32 bit integer to a SDIO function
600 * @func: SDIO function to access
601 * @b: integer to write
602 * @addr: address to write to
603 * @err_ret: optional status value from transfer
605 * Writes a 32 bit integer to the address space of a given SDIO
606 * function. @err_ret will contain the status of the actual
609 void sdio_writel(struct sdio_func
*func
, u32 b
, unsigned int addr
, int *err_ret
)
613 *(__le32
*)func
->tmpbuf
= cpu_to_le32(b
);
615 ret
= sdio_memcpy_toio(func
, addr
, func
->tmpbuf
, 4);
619 EXPORT_SYMBOL_GPL(sdio_writel
);
622 * sdio_f0_readb - read a single byte from SDIO function 0
623 * @func: an SDIO function of the card
624 * @addr: address to read
625 * @err_ret: optional status value from transfer
627 * Reads a single byte from the address space of SDIO function 0.
628 * If there is a problem reading the address, 0xff is returned
629 * and @err_ret will contain the error code.
631 unsigned char sdio_f0_readb(struct sdio_func
*func
, unsigned int addr
,
643 ret
= mmc_io_rw_direct(func
->card
, 0, 0, addr
, 0, &val
);
651 EXPORT_SYMBOL_GPL(sdio_f0_readb
);
654 * sdio_f0_writeb - write a single byte to SDIO function 0
655 * @func: an SDIO function of the card
657 * @addr: address to write to
658 * @err_ret: optional status value from transfer
660 * Writes a single byte to the address space of SDIO function 0.
661 * @err_ret will contain the status of the actual transfer.
663 * Only writes to the vendor specific CCCR registers (0xF0 -
664 * 0xFF) are permiited; @err_ret will be set to -EINVAL for *
665 * writes outside this range.
667 void sdio_f0_writeb(struct sdio_func
*func
, unsigned char b
, unsigned int addr
,
678 if ((addr
< 0xF0 || addr
> 0xFF) && (!mmc_card_lenient_fn0(func
->card
))) {
684 ret
= mmc_io_rw_direct(func
->card
, 1, 0, addr
, b
, NULL
);
688 EXPORT_SYMBOL_GPL(sdio_f0_writeb
);
691 * sdio_get_host_pm_caps - get host power management capabilities
692 * @func: SDIO function attached to host
694 * Returns a capability bitmask corresponding to power management
695 * features supported by the host controller that the card function
696 * might rely upon during a system suspend. The host doesn't need
697 * to be claimed, nor the function active, for this information to be
700 mmc_pm_flag_t
sdio_get_host_pm_caps(struct sdio_func
*func
)
705 return func
->card
->host
->pm_caps
;
707 EXPORT_SYMBOL_GPL(sdio_get_host_pm_caps
);
710 * sdio_set_host_pm_flags - set wanted host power management capabilities
711 * @func: SDIO function attached to host
713 * Set a capability bitmask corresponding to wanted host controller
714 * power management features for the upcoming suspend state.
715 * This must be called, if needed, each time the suspend method of
716 * the function driver is called, and must contain only bits that
717 * were returned by sdio_get_host_pm_caps().
718 * The host doesn't need to be claimed, nor the function active,
719 * for this information to be set.
721 int sdio_set_host_pm_flags(struct sdio_func
*func
, mmc_pm_flag_t flags
)
723 struct mmc_host
*host
;
728 host
= func
->card
->host
;
730 if (flags
& ~host
->pm_caps
)
733 /* function suspend methods are serialized, hence no lock needed */
734 host
->pm_flags
|= flags
;
737 EXPORT_SYMBOL_GPL(sdio_set_host_pm_flags
);
740 * sdio_retune_crc_disable - temporarily disable retuning on CRC errors
741 * @func: SDIO function attached to host
743 * If the SDIO card is known to be in a state where it might produce
744 * CRC errors on the bus in response to commands (like if we know it is
745 * transitioning between power states), an SDIO function driver can
746 * call this function to temporarily disable the SD/MMC core behavior of
747 * triggering an automatic retuning.
749 * This function should be called while the host is claimed and the host
750 * should remain claimed until sdio_retune_crc_enable() is called.
751 * Specifically, the expected sequence of calls is:
752 * - sdio_claim_host()
753 * - sdio_retune_crc_disable()
754 * - some number of calls like sdio_writeb() and sdio_readb()
755 * - sdio_retune_crc_enable()
756 * - sdio_release_host()
758 void sdio_retune_crc_disable(struct sdio_func
*func
)
760 func
->card
->host
->retune_crc_disable
= true;
762 EXPORT_SYMBOL_GPL(sdio_retune_crc_disable
);
765 * sdio_retune_crc_enable - re-enable retuning on CRC errors
766 * @func: SDIO function attached to host
768 * This is the compement to sdio_retune_crc_disable().
770 void sdio_retune_crc_enable(struct sdio_func
*func
)
772 func
->card
->host
->retune_crc_disable
= false;
774 EXPORT_SYMBOL_GPL(sdio_retune_crc_enable
);
777 * sdio_retune_hold_now - start deferring retuning requests till release
778 * @func: SDIO function attached to host
780 * This function can be called if it's currently a bad time to do
781 * a retune of the SDIO card. Retune requests made during this time
782 * will be held and we'll actually do the retune sometime after the
785 * This function could be useful if an SDIO card is in a power state
786 * where it can respond to a small subset of commands that doesn't
787 * include the retuning command. Care should be taken when using
788 * this function since (presumably) the retuning request we might be
789 * deferring was made for a good reason.
791 * This function should be called while the host is claimed.
793 void sdio_retune_hold_now(struct sdio_func
*func
)
795 mmc_retune_hold_now(func
->card
->host
);
797 EXPORT_SYMBOL_GPL(sdio_retune_hold_now
);
800 * sdio_retune_release - signal that it's OK to retune now
801 * @func: SDIO function attached to host
803 * This is the complement to sdio_retune_hold_now(). Calling this
804 * function won't make a retune happen right away but will allow
805 * them to be scheduled normally.
807 * This function should be called while the host is claimed.
809 void sdio_retune_release(struct sdio_func
*func
)
811 mmc_retune_release(func
->card
->host
);
813 EXPORT_SYMBOL_GPL(sdio_retune_release
);