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>
23 * sdio_claim_host - exclusively claim a bus for a certain SDIO function
24 * @func: SDIO function that will be accessed
26 * Claim a bus for a set of operations. The SDIO function given
27 * is used to figure out which bus is relevant.
29 void sdio_claim_host(struct sdio_func *func)
34 mmc_claim_host(func->card->host);
36 EXPORT_SYMBOL_GPL(sdio_claim_host);
39 * sdio_release_host - release a bus for a certain SDIO function
40 * @func: SDIO function that was accessed
42 * Release a bus, allowing others to claim the bus for their
45 void sdio_release_host(struct sdio_func *func)
50 mmc_release_host(func->card->host);
52 EXPORT_SYMBOL_GPL(sdio_release_host);
55 * sdio_enable_func - enables a SDIO function for usage
56 * @func: SDIO function to enable
58 * Powers up and activates a SDIO function so that register
61 int sdio_enable_func(struct sdio_func *func)
65 unsigned long timeout;
70 pr_debug("SDIO: Enabling device %s...\n", sdio_func_id(func));
72 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, ®);
76 reg |= 1 << func->num;
78 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);
82 timeout = jiffies + msecs_to_jiffies(func->enable_timeout);
85 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IORx, 0, ®);
88 if (reg & (1 << func->num))
91 if (time_after(jiffies, timeout))
95 pr_debug("SDIO: Enabled device %s\n", sdio_func_id(func));
100 pr_debug("SDIO: Failed to enable device %s\n", sdio_func_id(func));
103 EXPORT_SYMBOL_GPL(sdio_enable_func);
106 * sdio_disable_func - disable a SDIO function
107 * @func: SDIO function to disable
109 * Powers down and deactivates a SDIO function. Register access
110 * to this function will fail until the function is reenabled.
112 int sdio_disable_func(struct sdio_func *func)
120 pr_debug("SDIO: Disabling device %s...\n", sdio_func_id(func));
122 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, ®);
126 reg &= ~(1 << func->num);
128 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);
132 pr_debug("SDIO: Disabled device %s\n", sdio_func_id(func));
137 pr_debug("SDIO: Failed to disable device %s\n", sdio_func_id(func));
140 EXPORT_SYMBOL_GPL(sdio_disable_func);
143 * sdio_set_block_size - set the block size of an SDIO function
144 * @func: SDIO function to change
145 * @blksz: new block size or 0 to use the default.
147 * The default block size is the largest supported by both the function
148 * and the host, with a maximum of 512 to ensure that arbitrarily sized
149 * data transfer use the optimal (least) number of commands.
151 * A driver may call this to override the default block size set by the
152 * core. This can be used to set a block size greater than the maximum
153 * that reported by the card; it is the driver's responsibility to ensure
154 * it uses a value that the card supports.
156 * Returns 0 on success, -EINVAL if the host does not support the
157 * requested block size, or -EIO (etc.) if one of the resultant FBR block
158 * size register writes failed.
161 int sdio_set_block_size(struct sdio_func *func, unsigned blksz)
165 if (blksz > func->card->host->max_blk_size)
169 blksz = min(func->max_blksize, func->card->host->max_blk_size);
170 blksz = min(blksz, 512u);
173 ret = mmc_io_rw_direct(func->card, 1, 0,
174 SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE,
178 ret = mmc_io_rw_direct(func->card, 1, 0,
179 SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE + 1,
180 (blksz >> 8) & 0xff, NULL);
183 func->cur_blksize = blksz;
186 EXPORT_SYMBOL_GPL(sdio_set_block_size);
189 * Calculate the maximum byte mode transfer size
191 static inline unsigned int sdio_max_byte_size(struct sdio_func *func)
193 unsigned mval = func->card->host->max_blk_size;
195 if (mmc_blksz_for_byte_mode(func->card))
196 mval = min(mval, func->cur_blksize);
198 mval = min(mval, func->max_blksize);
200 if (mmc_card_broken_byte_mode_512(func->card))
201 return min(mval, 511u);
203 return min(mval, 512u); /* maximum size for byte mode */
207 * sdio_align_size - pads a transfer size to a more optimal value
208 * @func: SDIO function
209 * @sz: original transfer size
211 * Pads the original data size with a number of extra bytes in
212 * order to avoid controller bugs and/or performance hits
213 * (e.g. some controllers revert to PIO for certain sizes).
215 * If possible, it will also adjust the size so that it can be
216 * handled in just a single request.
218 * Returns the improved size, which might be unmodified.
220 unsigned int sdio_align_size(struct sdio_func *func, unsigned int sz)
222 unsigned int orig_sz;
223 unsigned int blk_sz, byte_sz;
229 * Do a first check with the controller, in case it
230 * wants to increase the size up to a point where it
231 * might need more than one block.
233 sz = mmc_align_data_size(func->card, sz);
236 * If we can still do this with just a byte transfer, then
239 if (sz <= sdio_max_byte_size(func))
242 if (func->card->cccr.multi_block) {
244 * Check if the transfer is already block aligned
246 if ((sz % func->cur_blksize) == 0)
250 * Realign it so that it can be done with one request,
251 * and recheck if the controller still likes it.
253 blk_sz = ((sz + func->cur_blksize - 1) /
254 func->cur_blksize) * func->cur_blksize;
255 blk_sz = mmc_align_data_size(func->card, blk_sz);
258 * This value is only good if it is still just
261 if ((blk_sz % func->cur_blksize) == 0)
265 * We failed to do one request, but at least try to
266 * pad the remainder properly.
268 byte_sz = mmc_align_data_size(func->card,
269 sz % func->cur_blksize);
270 if (byte_sz <= sdio_max_byte_size(func)) {
271 blk_sz = sz / func->cur_blksize;
272 return blk_sz * func->cur_blksize + byte_sz;
276 * We need multiple requests, so first check that the
277 * controller can handle the chunk size;
279 chunk_sz = mmc_align_data_size(func->card,
280 sdio_max_byte_size(func));
281 if (chunk_sz == sdio_max_byte_size(func)) {
283 * Fix up the size of the remainder (if any)
285 byte_sz = orig_sz % chunk_sz;
287 byte_sz = mmc_align_data_size(func->card,
291 return (orig_sz / chunk_sz) * chunk_sz + byte_sz;
296 * The controller is simply incapable of transferring the size
297 * we want in decent manner, so just return the original size.
301 EXPORT_SYMBOL_GPL(sdio_align_size);
303 /* Split an arbitrarily sized data transfer into several
304 * IO_RW_EXTENDED commands. */
305 static int sdio_io_rw_ext_helper(struct sdio_func *func, int write,
306 unsigned addr, int incr_addr, u8 *buf, unsigned size)
308 unsigned remainder = size;
312 if (!func || (func->num > 7))
315 /* Do the bulk of the transfer using block mode (if supported). */
316 if (func->card->cccr.multi_block && (size > sdio_max_byte_size(func))) {
317 /* Blocks per command is limited by host count, host transfer
318 * size and the maximum for IO_RW_EXTENDED of 511 blocks. */
319 max_blocks = min(func->card->host->max_blk_count, 511u);
321 while (remainder >= func->cur_blksize) {
324 blocks = remainder / func->cur_blksize;
325 if (blocks > max_blocks)
327 size = blocks * func->cur_blksize;
329 ret = mmc_io_rw_extended(func->card, write,
330 func->num, addr, incr_addr, buf,
331 blocks, func->cur_blksize);
342 /* Write the remainder using byte mode. */
343 while (remainder > 0) {
344 size = min(remainder, sdio_max_byte_size(func));
346 /* Indicate byte mode by setting "blocks" = 0 */
347 ret = mmc_io_rw_extended(func->card, write, func->num, addr,
348 incr_addr, buf, 0, size);
361 * sdio_readb - read a single byte from a SDIO function
362 * @func: SDIO function to access
363 * @addr: address to read
364 * @err_ret: optional status value from transfer
366 * Reads a single byte from the address space of a given SDIO
367 * function. If there is a problem reading the address, 0xff
368 * is returned and @err_ret will contain the error code.
370 u8 sdio_readb(struct sdio_func *func, unsigned int addr, int *err_ret)
383 ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val);
392 EXPORT_SYMBOL_GPL(sdio_readb);
395 * sdio_writeb - write a single byte to a SDIO function
396 * @func: SDIO function to access
398 * @addr: address to write to
399 * @err_ret: optional status value from transfer
401 * Writes a single byte to the address space of a given SDIO
402 * function. @err_ret will contain the status of the actual
405 void sdio_writeb(struct sdio_func *func, u8 b, unsigned int addr, int *err_ret)
414 ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL);
418 EXPORT_SYMBOL_GPL(sdio_writeb);
421 * sdio_writeb_readb - write and read a byte from SDIO function
422 * @func: SDIO function to access
423 * @write_byte: byte to write
424 * @addr: address to write to
425 * @err_ret: optional status value from transfer
427 * Performs a RAW (Read after Write) operation as defined by SDIO spec -
428 * single byte is written to address space of a given SDIO function and
429 * response is read back from the same address, both using single request.
430 * If there is a problem with the operation, 0xff is returned and
431 * @err_ret will contain the error code.
433 u8 sdio_writeb_readb(struct sdio_func *func, u8 write_byte,
434 unsigned int addr, int *err_ret)
439 ret = mmc_io_rw_direct(func->card, 1, func->num, addr,
448 EXPORT_SYMBOL_GPL(sdio_writeb_readb);
451 * sdio_memcpy_fromio - read a chunk of memory from a SDIO function
452 * @func: SDIO function to access
453 * @dst: buffer to store the data
454 * @addr: address to begin reading from
455 * @count: number of bytes to read
457 * Reads from the address space of a given SDIO function. Return
458 * value indicates if the transfer succeeded or not.
460 int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
461 unsigned int addr, int count)
463 return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count);
465 EXPORT_SYMBOL_GPL(sdio_memcpy_fromio);
468 * sdio_memcpy_toio - write a chunk of memory to a SDIO function
469 * @func: SDIO function to access
470 * @addr: address to start writing to
471 * @src: buffer that contains the data to write
472 * @count: number of bytes to write
474 * Writes to the address space of a given SDIO function. Return
475 * value indicates if the transfer succeeded or not.
477 int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
478 void *src, int count)
480 return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count);
482 EXPORT_SYMBOL_GPL(sdio_memcpy_toio);
485 * sdio_readsb - read from a FIFO on a SDIO function
486 * @func: SDIO function to access
487 * @dst: buffer to store the data
488 * @addr: address of (single byte) FIFO
489 * @count: number of bytes to read
491 * Reads from the specified FIFO of a given SDIO function. Return
492 * value indicates if the transfer succeeded or not.
494 int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
497 return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count);
499 EXPORT_SYMBOL_GPL(sdio_readsb);
502 * sdio_writesb - write to a FIFO of a SDIO function
503 * @func: SDIO function to access
504 * @addr: address of (single byte) FIFO
505 * @src: buffer that contains the data to write
506 * @count: number of bytes to write
508 * Writes to the specified FIFO of a given SDIO function. Return
509 * value indicates if the transfer succeeded or not.
511 int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,
514 return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count);
516 EXPORT_SYMBOL_GPL(sdio_writesb);
519 * sdio_readw - read a 16 bit integer from a SDIO function
520 * @func: SDIO function to access
521 * @addr: address to read
522 * @err_ret: optional status value from transfer
524 * Reads a 16 bit integer from the address space of a given SDIO
525 * function. If there is a problem reading the address, 0xffff
526 * is returned and @err_ret will contain the error code.
528 u16 sdio_readw(struct sdio_func *func, unsigned int addr, int *err_ret)
535 ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);
542 return le16_to_cpup((__le16 *)func->tmpbuf);
544 EXPORT_SYMBOL_GPL(sdio_readw);
547 * sdio_writew - write a 16 bit integer to a SDIO function
548 * @func: SDIO function to access
549 * @b: integer to write
550 * @addr: address to write to
551 * @err_ret: optional status value from transfer
553 * Writes a 16 bit integer to the address space of a given SDIO
554 * function. @err_ret will contain the status of the actual
557 void sdio_writew(struct sdio_func *func, u16 b, unsigned int addr, int *err_ret)
561 *(__le16 *)func->tmpbuf = cpu_to_le16(b);
563 ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
567 EXPORT_SYMBOL_GPL(sdio_writew);
570 * sdio_readl - read a 32 bit integer from a SDIO function
571 * @func: SDIO function to access
572 * @addr: address to read
573 * @err_ret: optional status value from transfer
575 * Reads a 32 bit integer from the address space of a given SDIO
576 * function. If there is a problem reading the address,
577 * 0xffffffff is returned and @err_ret will contain the error
580 u32 sdio_readl(struct sdio_func *func, unsigned int addr, int *err_ret)
587 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,
645 ret = mmc_io_rw_direct(func->card, 0, 0, addr, 0, &val);
654 EXPORT_SYMBOL_GPL(sdio_f0_readb);
657 * sdio_f0_writeb - write a single byte to SDIO function 0
658 * @func: an SDIO function of the card
660 * @addr: address to write to
661 * @err_ret: optional status value from transfer
663 * Writes a single byte to the address space of SDIO function 0.
664 * @err_ret will contain the status of the actual transfer.
666 * Only writes to the vendor specific CCCR registers (0xF0 -
667 * 0xFF) are permiited; @err_ret will be set to -EINVAL for *
668 * writes outside this range.
670 void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
680 if ((addr < 0xF0 || addr > 0xFF) && (!mmc_card_lenient_fn0(func->card))) {
686 ret = mmc_io_rw_direct(func->card, 1, 0, addr, b, NULL);
690 EXPORT_SYMBOL_GPL(sdio_f0_writeb);
693 * sdio_get_host_pm_caps - get host power management capabilities
694 * @func: SDIO function attached to host
696 * Returns a capability bitmask corresponding to power management
697 * features supported by the host controller that the card function
698 * might rely upon during a system suspend. The host doesn't need
699 * to be claimed, nor the function active, for this information to be
702 mmc_pm_flag_t sdio_get_host_pm_caps(struct sdio_func *func)
707 return func->card->host->pm_caps;
709 EXPORT_SYMBOL_GPL(sdio_get_host_pm_caps);
712 * sdio_set_host_pm_flags - set wanted host power management capabilities
713 * @func: SDIO function attached to host
715 * Set a capability bitmask corresponding to wanted host controller
716 * power management features for the upcoming suspend state.
717 * This must be called, if needed, each time the suspend method of
718 * the function driver is called, and must contain only bits that
719 * were returned by sdio_get_host_pm_caps().
720 * The host doesn't need to be claimed, nor the function active,
721 * for this information to be set.
723 int sdio_set_host_pm_flags(struct sdio_func *func, mmc_pm_flag_t flags)
725 struct mmc_host *host;
730 host = func->card->host;
732 if (flags & ~host->pm_caps)
735 /* function suspend methods are serialized, hence no lock needed */
736 host->pm_flags |= flags;
739 EXPORT_SYMBOL_GPL(sdio_set_host_pm_flags);