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>
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 * sdio_align_size - pads a transfer size to a more optimal value
206 * @func: SDIO function
207 * @sz: original transfer size
209 * Pads the original data size with a number of extra bytes in
210 * order to avoid controller bugs and/or performance hits
211 * (e.g. some controllers revert to PIO for certain sizes).
213 * If possible, it will also adjust the size so that it can be
214 * handled in just a single request.
216 * Returns the improved size, which might be unmodified.
218 unsigned int sdio_align_size(struct sdio_func *func, unsigned int sz)
220 unsigned int orig_sz;
221 unsigned int blk_sz, byte_sz;
227 * Do a first check with the controller, in case it
228 * wants to increase the size up to a point where it
229 * might need more than one block.
231 sz = mmc_align_data_size(func->card, sz);
234 * If we can still do this with just a byte transfer, then
237 if (sz <= sdio_max_byte_size(func))
240 if (func->card->cccr.multi_block) {
242 * Check if the transfer is already block aligned
244 if ((sz % func->cur_blksize) == 0)
248 * Realign it so that it can be done with one request,
249 * and recheck if the controller still likes it.
251 blk_sz = ((sz + func->cur_blksize - 1) /
252 func->cur_blksize) * func->cur_blksize;
253 blk_sz = mmc_align_data_size(func->card, blk_sz);
256 * This value is only good if it is still just
259 if ((blk_sz % func->cur_blksize) == 0)
263 * We failed to do one request, but at least try to
264 * pad the remainder properly.
266 byte_sz = mmc_align_data_size(func->card,
267 sz % func->cur_blksize);
268 if (byte_sz <= sdio_max_byte_size(func)) {
269 blk_sz = sz / func->cur_blksize;
270 return blk_sz * func->cur_blksize + byte_sz;
274 * We need multiple requests, so first check that the
275 * controller can handle the chunk size;
277 chunk_sz = mmc_align_data_size(func->card,
278 sdio_max_byte_size(func));
279 if (chunk_sz == sdio_max_byte_size(func)) {
281 * Fix up the size of the remainder (if any)
283 byte_sz = orig_sz % chunk_sz;
285 byte_sz = mmc_align_data_size(func->card,
289 return (orig_sz / chunk_sz) * chunk_sz + byte_sz;
294 * The controller is simply incapable of transferring the size
295 * we want in decent manner, so just return the original size.
299 EXPORT_SYMBOL_GPL(sdio_align_size);
301 /* Split an arbitrarily sized data transfer into several
302 * IO_RW_EXTENDED commands. */
303 static int sdio_io_rw_ext_helper(struct sdio_func *func, int write,
304 unsigned addr, int incr_addr, u8 *buf, unsigned size)
306 unsigned remainder = size;
310 /* Do the bulk of the transfer using block mode (if supported). */
311 if (func->card->cccr.multi_block && (size > sdio_max_byte_size(func))) {
312 /* Blocks per command is limited by host count, host transfer
313 * size and the maximum for IO_RW_EXTENDED of 511 blocks. */
314 max_blocks = min(func->card->host->max_blk_count, 511u);
316 while (remainder >= func->cur_blksize) {
319 blocks = remainder / func->cur_blksize;
320 if (blocks > max_blocks)
322 size = blocks * func->cur_blksize;
324 ret = mmc_io_rw_extended(func->card, write,
325 func->num, addr, incr_addr, buf,
326 blocks, func->cur_blksize);
337 /* Write the remainder using byte mode. */
338 while (remainder > 0) {
339 size = min(remainder, sdio_max_byte_size(func));
341 /* Indicate byte mode by setting "blocks" = 0 */
342 ret = mmc_io_rw_extended(func->card, write, func->num, addr,
343 incr_addr, buf, 0, size);
356 * sdio_readb - read a single byte from a SDIO function
357 * @func: SDIO function to access
358 * @addr: address to read
359 * @err_ret: optional status value from transfer
361 * Reads a single byte from the address space of a given SDIO
362 * function. If there is a problem reading the address, 0xff
363 * is returned and @err_ret will contain the error code.
365 u8 sdio_readb(struct sdio_func *func, unsigned int addr, int *err_ret)
375 ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val);
384 EXPORT_SYMBOL_GPL(sdio_readb);
387 * sdio_readb_ext - read a single byte from a SDIO function
388 * @func: SDIO function to access
389 * @addr: address to read
390 * @err_ret: optional status value from transfer
391 * @in: value to add to argument
393 * Reads a single byte from the address space of a given SDIO
394 * function. If there is a problem reading the address, 0xff
395 * is returned and @err_ret will contain the error code.
397 unsigned char sdio_readb_ext(struct sdio_func *func, unsigned int addr,
398 int *err_ret, unsigned in)
408 ret = mmc_io_rw_direct(func->card, 0, func->num, addr, (u8)in, &val);
417 EXPORT_SYMBOL_GPL(sdio_readb_ext);
420 * sdio_writeb - write a single byte to a SDIO function
421 * @func: SDIO function to access
423 * @addr: address to write to
424 * @err_ret: optional status value from transfer
426 * Writes a single byte to the address space of a given SDIO
427 * function. @err_ret will contain the status of the actual
430 void sdio_writeb(struct sdio_func *func, u8 b, unsigned int addr, int *err_ret)
436 ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL);
440 EXPORT_SYMBOL_GPL(sdio_writeb);
443 * sdio_writeb_readb - write and read a byte from SDIO function
444 * @func: SDIO function to access
445 * @write_byte: byte to write
446 * @addr: address to write to
447 * @err_ret: optional status value from transfer
449 * Performs a RAW (Read after Write) operation as defined by SDIO spec -
450 * single byte is written to address space of a given SDIO function and
451 * response is read back from the same address, both using single request.
452 * If there is a problem with the operation, 0xff is returned and
453 * @err_ret will contain the error code.
455 u8 sdio_writeb_readb(struct sdio_func *func, u8 write_byte,
456 unsigned int addr, int *err_ret)
461 ret = mmc_io_rw_direct(func->card, 1, func->num, addr,
470 EXPORT_SYMBOL_GPL(sdio_writeb_readb);
473 * sdio_memcpy_fromio - read a chunk of memory from a SDIO function
474 * @func: SDIO function to access
475 * @dst: buffer to store the data
476 * @addr: address to begin reading from
477 * @count: number of bytes to read
479 * Reads from the address space of a given SDIO function. Return
480 * value indicates if the transfer succeeded or not.
482 int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
483 unsigned int addr, int count)
485 return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count);
487 EXPORT_SYMBOL_GPL(sdio_memcpy_fromio);
490 * sdio_memcpy_toio - write a chunk of memory to a SDIO function
491 * @func: SDIO function to access
492 * @addr: address to start writing to
493 * @src: buffer that contains the data to write
494 * @count: number of bytes to write
496 * Writes to the address space of a given SDIO function. Return
497 * value indicates if the transfer succeeded or not.
499 int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
500 void *src, int count)
502 return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count);
504 EXPORT_SYMBOL_GPL(sdio_memcpy_toio);
507 * sdio_readsb - read from a FIFO on a SDIO function
508 * @func: SDIO function to access
509 * @dst: buffer to store the data
510 * @addr: address of (single byte) FIFO
511 * @count: number of bytes to read
513 * Reads from the specified FIFO of a given SDIO function. Return
514 * value indicates if the transfer succeeded or not.
516 int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
519 return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count);
521 EXPORT_SYMBOL_GPL(sdio_readsb);
524 * sdio_writesb - write to a FIFO of a SDIO function
525 * @func: SDIO function to access
526 * @addr: address of (single byte) FIFO
527 * @src: buffer that contains the data to write
528 * @count: number of bytes to write
530 * Writes to the specified FIFO of a given SDIO function. Return
531 * value indicates if the transfer succeeded or not.
533 int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,
536 return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count);
538 EXPORT_SYMBOL_GPL(sdio_writesb);
541 * sdio_readw - read a 16 bit integer from a SDIO function
542 * @func: SDIO function to access
543 * @addr: address to read
544 * @err_ret: optional status value from transfer
546 * Reads a 16 bit integer from the address space of a given SDIO
547 * function. If there is a problem reading the address, 0xffff
548 * is returned and @err_ret will contain the error code.
550 u16 sdio_readw(struct sdio_func *func, unsigned int addr, int *err_ret)
557 ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);
564 return le16_to_cpup((__le16 *)func->tmpbuf);
566 EXPORT_SYMBOL_GPL(sdio_readw);
569 * sdio_writew - write a 16 bit integer to a SDIO function
570 * @func: SDIO function to access
571 * @b: integer to write
572 * @addr: address to write to
573 * @err_ret: optional status value from transfer
575 * Writes a 16 bit integer to the address space of a given SDIO
576 * function. @err_ret will contain the status of the actual
579 void sdio_writew(struct sdio_func *func, u16 b, unsigned int addr, int *err_ret)
583 *(__le16 *)func->tmpbuf = cpu_to_le16(b);
585 ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
589 EXPORT_SYMBOL_GPL(sdio_writew);
592 * sdio_readl - read a 32 bit integer from a SDIO function
593 * @func: SDIO function to access
594 * @addr: address to read
595 * @err_ret: optional status value from transfer
597 * Reads a 32 bit integer from the address space of a given SDIO
598 * function. If there is a problem reading the address,
599 * 0xffffffff is returned and @err_ret will contain the error
602 u32 sdio_readl(struct sdio_func *func, unsigned int addr, int *err_ret)
609 ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4);
616 return le32_to_cpup((__le32 *)func->tmpbuf);
618 EXPORT_SYMBOL_GPL(sdio_readl);
621 * sdio_writel - write a 32 bit integer to a SDIO function
622 * @func: SDIO function to access
623 * @b: integer to write
624 * @addr: address to write to
625 * @err_ret: optional status value from transfer
627 * Writes a 32 bit integer to the address space of a given SDIO
628 * function. @err_ret will contain the status of the actual
631 void sdio_writel(struct sdio_func *func, u32 b, unsigned int addr, int *err_ret)
635 *(__le32 *)func->tmpbuf = cpu_to_le32(b);
637 ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
641 EXPORT_SYMBOL_GPL(sdio_writel);
644 * sdio_f0_readb - read a single byte from SDIO function 0
645 * @func: an SDIO function of the card
646 * @addr: address to read
647 * @err_ret: optional status value from transfer
649 * Reads a single byte from the address space of SDIO function 0.
650 * If there is a problem reading the address, 0xff is returned
651 * and @err_ret will contain the error code.
653 unsigned char sdio_f0_readb(struct sdio_func *func, unsigned int addr,
664 ret = mmc_io_rw_direct(func->card, 0, 0, addr, 0, &val);
673 EXPORT_SYMBOL_GPL(sdio_f0_readb);
676 * sdio_f0_writeb - write a single byte to SDIO function 0
677 * @func: an SDIO function of the card
679 * @addr: address to write to
680 * @err_ret: optional status value from transfer
682 * Writes a single byte to the address space of SDIO function 0.
683 * @err_ret will contain the status of the actual transfer.
685 * Only writes to the vendor specific CCCR registers (0xF0 -
686 * 0xFF) are permiited; @err_ret will be set to -EINVAL for *
687 * writes outside this range.
689 void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
696 if ((addr < 0xF0 || addr > 0xFF) && (!mmc_card_lenient_fn0(func->card))) {
702 ret = mmc_io_rw_direct(func->card, 1, 0, addr, b, NULL);
706 EXPORT_SYMBOL_GPL(sdio_f0_writeb);
709 * sdio_get_host_pm_caps - get host power management capabilities
710 * @func: SDIO function attached to host
712 * Returns a capability bitmask corresponding to power management
713 * features supported by the host controller that the card function
714 * might rely upon during a system suspend. The host doesn't need
715 * to be claimed, nor the function active, for this information to be
718 mmc_pm_flag_t sdio_get_host_pm_caps(struct sdio_func *func)
723 return func->card->host->pm_caps;
725 EXPORT_SYMBOL_GPL(sdio_get_host_pm_caps);
728 * sdio_set_host_pm_flags - set wanted host power management capabilities
729 * @func: SDIO function attached to host
731 * Set a capability bitmask corresponding to wanted host controller
732 * power management features for the upcoming suspend state.
733 * This must be called, if needed, each time the suspend method of
734 * the function driver is called, and must contain only bits that
735 * were returned by sdio_get_host_pm_caps().
736 * The host doesn't need to be claimed, nor the function active,
737 * for this information to be set.
739 int sdio_set_host_pm_flags(struct sdio_func *func, mmc_pm_flag_t flags)
741 struct mmc_host *host;
746 host = func->card->host;
748 if (flags & ~host->pm_caps)
751 /* function suspend methods are serialized, hence no lock needed */
752 host->pm_flags |= flags;
755 EXPORT_SYMBOL_GPL(sdio_set_host_pm_flags);