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>
20 * sdio_claim_host - exclusively claim a bus for a certain SDIO function
21 * @func: SDIO function that will be accessed
23 * Claim a bus for a set of operations. The SDIO function given
24 * is used to figure out which bus is relevant.
26 void sdio_claim_host(struct sdio_func *func)
31 mmc_claim_host(func->card->host);
33 EXPORT_SYMBOL_GPL(sdio_claim_host);
36 * sdio_release_host - release a bus for a certain SDIO function
37 * @func: SDIO function that was accessed
39 * Release a bus, allowing others to claim the bus for their
42 void sdio_release_host(struct sdio_func *func)
47 mmc_release_host(func->card->host);
49 EXPORT_SYMBOL_GPL(sdio_release_host);
52 * sdio_enable_func - enables a SDIO function for usage
53 * @func: SDIO function to enable
55 * Powers up and activates a SDIO function so that register
58 int sdio_enable_func(struct sdio_func *func)
62 unsigned long timeout;
67 pr_debug("SDIO: Enabling device %s...\n", sdio_func_id(func));
69 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, ®);
73 reg |= 1 << func->num;
75 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);
79 timeout = jiffies + msecs_to_jiffies(func->enable_timeout);
82 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IORx, 0, ®);
85 if (reg & (1 << func->num))
88 if (time_after(jiffies, timeout))
92 pr_debug("SDIO: Enabled device %s\n", sdio_func_id(func));
97 pr_debug("SDIO: Failed to enable device %s\n", sdio_func_id(func));
100 EXPORT_SYMBOL_GPL(sdio_enable_func);
103 * sdio_disable_func - disable a SDIO function
104 * @func: SDIO function to disable
106 * Powers down and deactivates a SDIO function. Register access
107 * to this function will fail until the function is reenabled.
109 int sdio_disable_func(struct sdio_func *func)
117 pr_debug("SDIO: Disabling device %s...\n", sdio_func_id(func));
119 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, ®);
123 reg &= ~(1 << func->num);
125 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);
129 pr_debug("SDIO: Disabled device %s\n", sdio_func_id(func));
134 pr_debug("SDIO: Failed to disable device %s\n", sdio_func_id(func));
137 EXPORT_SYMBOL_GPL(sdio_disable_func);
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.
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.
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.
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.
158 int sdio_set_block_size(struct sdio_func *func, unsigned blksz)
162 if (blksz > func->card->host->max_blk_size)
166 blksz = min(func->max_blksize, func->card->host->max_blk_size);
167 blksz = min(blksz, 512u);
170 ret = mmc_io_rw_direct(func->card, 1, 0,
171 SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE,
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);
180 func->cur_blksize = blksz;
183 EXPORT_SYMBOL_GPL(sdio_set_block_size);
186 * Calculate the maximum byte mode transfer size
188 static inline unsigned int sdio_max_byte_size(struct sdio_func *func)
190 unsigned mval = func->card->host->max_blk_size;
192 if (mmc_blksz_for_byte_mode(func->card))
193 mval = min(mval, func->cur_blksize);
195 mval = min(mval, func->max_blksize);
197 if (mmc_card_broken_byte_mode_512(func->card))
198 return min(mval, 511u);
200 return min(mval, 512u); /* maximum size for byte mode */
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.
208 static inline unsigned int _sdio_align_size(unsigned int sz)
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
219 * sdio_align_size - pads a transfer size to a more optimal value
220 * @func: SDIO function
221 * @sz: original transfer size
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).
227 * If possible, it will also adjust the size so that it can be
228 * handled in just a single request.
230 * Returns the improved size, which might be unmodified.
232 unsigned int sdio_align_size(struct sdio_func *func, unsigned int sz)
234 unsigned int orig_sz;
235 unsigned int blk_sz, byte_sz;
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.
245 sz = _sdio_align_size(sz);
248 * If we can still do this with just a byte transfer, then
251 if (sz <= sdio_max_byte_size(func))
254 if (func->card->cccr.multi_block) {
256 * Check if the transfer is already block aligned
258 if ((sz % func->cur_blksize) == 0)
262 * Realign it so that it can be done with one request,
263 * and recheck if the controller still likes it.
265 blk_sz = ((sz + func->cur_blksize - 1) /
266 func->cur_blksize) * func->cur_blksize;
267 blk_sz = _sdio_align_size(blk_sz);
270 * This value is only good if it is still just
273 if ((blk_sz % func->cur_blksize) == 0)
277 * We failed to do one request, but at least try to
278 * pad the remainder properly.
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;
287 * We need multiple requests, so first check that the
288 * controller can handle the chunk size;
290 chunk_sz = _sdio_align_size(sdio_max_byte_size(func));
291 if (chunk_sz == sdio_max_byte_size(func)) {
293 * Fix up the size of the remainder (if any)
295 byte_sz = orig_sz % chunk_sz;
297 byte_sz = _sdio_align_size(byte_sz);
300 return (orig_sz / chunk_sz) * chunk_sz + byte_sz;
305 * The controller is simply incapable of transferring the size
306 * we want in decent manner, so just return the original size.
310 EXPORT_SYMBOL_GPL(sdio_align_size);
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)
317 unsigned remainder = size;
321 if (!func || (func->num > 7))
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);
330 while (remainder >= func->cur_blksize) {
333 blocks = remainder / func->cur_blksize;
334 if (blocks > max_blocks)
336 size = blocks * func->cur_blksize;
338 ret = mmc_io_rw_extended(func->card, write,
339 func->num, addr, incr_addr, buf,
340 blocks, func->cur_blksize);
351 /* Write the remainder using byte mode. */
352 while (remainder > 0) {
353 size = min(remainder, sdio_max_byte_size(func));
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);
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
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.
379 u8 sdio_readb(struct sdio_func *func, unsigned int addr, int *err_ret)
390 ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val);
398 EXPORT_SYMBOL_GPL(sdio_readb);
401 * sdio_writeb - write a single byte to a SDIO function
402 * @func: SDIO function to access
404 * @addr: address to write to
405 * @err_ret: optional status value from transfer
407 * Writes a single byte to the address space of a given SDIO
408 * function. @err_ret will contain the status of the actual
411 void sdio_writeb(struct sdio_func *func, u8 b, unsigned int addr, int *err_ret)
421 ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL);
425 EXPORT_SYMBOL_GPL(sdio_writeb);
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
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.
440 u8 sdio_writeb_readb(struct sdio_func *func, u8 write_byte,
441 unsigned int addr, int *err_ret)
446 ret = mmc_io_rw_direct(func->card, 1, func->num, addr,
455 EXPORT_SYMBOL_GPL(sdio_writeb_readb);
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
464 * Reads from the address space of a given SDIO function. Return
465 * value indicates if the transfer succeeded or not.
467 int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
468 unsigned int addr, int count)
470 return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count);
472 EXPORT_SYMBOL_GPL(sdio_memcpy_fromio);
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
481 * Writes to the address space of a given SDIO function. Return
482 * value indicates if the transfer succeeded or not.
484 int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
485 void *src, int count)
487 return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count);
489 EXPORT_SYMBOL_GPL(sdio_memcpy_toio);
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
498 * Reads from the specified FIFO of a given SDIO function. Return
499 * value indicates if the transfer succeeded or not.
501 int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
504 return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count);
506 EXPORT_SYMBOL_GPL(sdio_readsb);
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
515 * Writes to the specified FIFO of a given SDIO function. Return
516 * value indicates if the transfer succeeded or not.
518 int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,
521 return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count);
523 EXPORT_SYMBOL_GPL(sdio_writesb);
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
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.
535 u16 sdio_readw(struct sdio_func *func, unsigned int addr, int *err_ret)
539 ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);
545 return le16_to_cpup((__le16 *)func->tmpbuf);
547 EXPORT_SYMBOL_GPL(sdio_readw);
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
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
560 void sdio_writew(struct sdio_func *func, u16 b, unsigned int addr, int *err_ret)
564 *(__le16 *)func->tmpbuf = cpu_to_le16(b);
566 ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
570 EXPORT_SYMBOL_GPL(sdio_writew);
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
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
583 u32 sdio_readl(struct sdio_func *func, unsigned int addr, int *err_ret)
587 ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4);
593 return le32_to_cpup((__le32 *)func->tmpbuf);
595 EXPORT_SYMBOL_GPL(sdio_readl);
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
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
608 void sdio_writel(struct sdio_func *func, u32 b, unsigned int addr, int *err_ret)
612 *(__le32 *)func->tmpbuf = cpu_to_le32(b);
614 ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
618 EXPORT_SYMBOL_GPL(sdio_writel);
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
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.
630 unsigned char sdio_f0_readb(struct sdio_func *func, unsigned int addr,
642 ret = mmc_io_rw_direct(func->card, 0, 0, addr, 0, &val);
650 EXPORT_SYMBOL_GPL(sdio_f0_readb);
653 * sdio_f0_writeb - write a single byte to SDIO function 0
654 * @func: an SDIO function of the card
656 * @addr: address to write to
657 * @err_ret: optional status value from transfer
659 * Writes a single byte to the address space of SDIO function 0.
660 * @err_ret will contain the status of the actual transfer.
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.
666 void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
677 if ((addr < 0xF0 || addr > 0xFF) && (!mmc_card_lenient_fn0(func->card))) {
683 ret = mmc_io_rw_direct(func->card, 1, 0, addr, b, NULL);
687 EXPORT_SYMBOL_GPL(sdio_f0_writeb);
690 * sdio_get_host_pm_caps - get host power management capabilities
691 * @func: SDIO function attached to host
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
699 mmc_pm_flag_t sdio_get_host_pm_caps(struct sdio_func *func)
704 return func->card->host->pm_caps;
706 EXPORT_SYMBOL_GPL(sdio_get_host_pm_caps);
709 * sdio_set_host_pm_flags - set wanted host power management capabilities
710 * @func: SDIO function attached to host
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.
720 int sdio_set_host_pm_flags(struct sdio_func *func, mmc_pm_flag_t flags)
722 struct mmc_host *host;
727 host = func->card->host;
729 if (flags & ~host->pm_caps)
732 /* function suspend methods are serialized, hence no lock needed */
733 host->pm_flags |= flags;
736 EXPORT_SYMBOL_GPL(sdio_set_host_pm_flags);