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/kernel.h>
14 #include <linux/mmc/host.h>
15 #include <linux/mmc/card.h>
16 #include <linux/mmc/sdio.h>
17 #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 * This is legacy code, which needs to be re-worked some day. Basically we need
209 * to take into account the properties of the host, as to enable the SDIO func
210 * driver layer to allocate optimal buffers.
212 static inline unsigned int _sdio_align_size(unsigned int sz)
215 * FIXME: We don't have a system for the controller to tell
216 * the core about its problems yet, so for now we just 32-bit
223 * sdio_align_size - pads a transfer size to a more optimal value
224 * @func: SDIO function
225 * @sz: original transfer size
227 * Pads the original data size with a number of extra bytes in
228 * order to avoid controller bugs and/or performance hits
229 * (e.g. some controllers revert to PIO for certain sizes).
231 * If possible, it will also adjust the size so that it can be
232 * handled in just a single request.
234 * Returns the improved size, which might be unmodified.
236 unsigned int sdio_align_size(struct sdio_func *func, unsigned int sz)
238 unsigned int orig_sz;
239 unsigned int blk_sz, byte_sz;
245 * Do a first check with the controller, in case it
246 * wants to increase the size up to a point where it
247 * might need more than one block.
249 sz = _sdio_align_size(sz);
252 * If we can still do this with just a byte transfer, then
255 if (sz <= sdio_max_byte_size(func))
258 if (func->card->cccr.multi_block) {
260 * Check if the transfer is already block aligned
262 if ((sz % func->cur_blksize) == 0)
266 * Realign it so that it can be done with one request,
267 * and recheck if the controller still likes it.
269 blk_sz = ((sz + func->cur_blksize - 1) /
270 func->cur_blksize) * func->cur_blksize;
271 blk_sz = _sdio_align_size(blk_sz);
274 * This value is only good if it is still just
277 if ((blk_sz % func->cur_blksize) == 0)
281 * We failed to do one request, but at least try to
282 * pad the remainder properly.
284 byte_sz = _sdio_align_size(sz % func->cur_blksize);
285 if (byte_sz <= sdio_max_byte_size(func)) {
286 blk_sz = sz / func->cur_blksize;
287 return blk_sz * func->cur_blksize + byte_sz;
291 * We need multiple requests, so first check that the
292 * controller can handle the chunk size;
294 chunk_sz = _sdio_align_size(sdio_max_byte_size(func));
295 if (chunk_sz == sdio_max_byte_size(func)) {
297 * Fix up the size of the remainder (if any)
299 byte_sz = orig_sz % chunk_sz;
301 byte_sz = _sdio_align_size(byte_sz);
304 return (orig_sz / chunk_sz) * chunk_sz + byte_sz;
309 * The controller is simply incapable of transferring the size
310 * we want in decent manner, so just return the original size.
314 EXPORT_SYMBOL_GPL(sdio_align_size);
316 /* Split an arbitrarily sized data transfer into several
317 * IO_RW_EXTENDED commands. */
318 static int sdio_io_rw_ext_helper(struct sdio_func *func, int write,
319 unsigned addr, int incr_addr, u8 *buf, unsigned size)
321 unsigned remainder = size;
325 if (!func || (func->num > 7))
328 /* Do the bulk of the transfer using block mode (if supported). */
329 if (func->card->cccr.multi_block && (size > sdio_max_byte_size(func))) {
330 /* Blocks per command is limited by host count, host transfer
331 * size and the maximum for IO_RW_EXTENDED of 511 blocks. */
332 max_blocks = min(func->card->host->max_blk_count, 511u);
334 while (remainder >= func->cur_blksize) {
337 blocks = remainder / func->cur_blksize;
338 if (blocks > max_blocks)
340 size = blocks * func->cur_blksize;
342 ret = mmc_io_rw_extended(func->card, write,
343 func->num, addr, incr_addr, buf,
344 blocks, func->cur_blksize);
355 /* Write the remainder using byte mode. */
356 while (remainder > 0) {
357 size = min(remainder, sdio_max_byte_size(func));
359 /* Indicate byte mode by setting "blocks" = 0 */
360 ret = mmc_io_rw_extended(func->card, write, func->num, addr,
361 incr_addr, buf, 0, size);
374 * sdio_readb - read a single byte from a SDIO function
375 * @func: SDIO function to access
376 * @addr: address to read
377 * @err_ret: optional status value from transfer
379 * Reads a single byte from the address space of a given SDIO
380 * function. If there is a problem reading the address, 0xff
381 * is returned and @err_ret will contain the error code.
383 u8 sdio_readb(struct sdio_func *func, unsigned int addr, int *err_ret)
394 ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val);
402 EXPORT_SYMBOL_GPL(sdio_readb);
405 * sdio_writeb - write a single byte to a SDIO function
406 * @func: SDIO function to access
408 * @addr: address to write to
409 * @err_ret: optional status value from transfer
411 * Writes a single byte to the address space of a given SDIO
412 * function. @err_ret will contain the status of the actual
415 void sdio_writeb(struct sdio_func *func, u8 b, unsigned int addr, int *err_ret)
425 ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL);
429 EXPORT_SYMBOL_GPL(sdio_writeb);
432 * sdio_writeb_readb - write and read a byte from SDIO function
433 * @func: SDIO function to access
434 * @write_byte: byte to write
435 * @addr: address to write to
436 * @err_ret: optional status value from transfer
438 * Performs a RAW (Read after Write) operation as defined by SDIO spec -
439 * single byte is written to address space of a given SDIO function and
440 * response is read back from the same address, both using single request.
441 * If there is a problem with the operation, 0xff is returned and
442 * @err_ret will contain the error code.
444 u8 sdio_writeb_readb(struct sdio_func *func, u8 write_byte,
445 unsigned int addr, int *err_ret)
450 ret = mmc_io_rw_direct(func->card, 1, func->num, addr,
459 EXPORT_SYMBOL_GPL(sdio_writeb_readb);
462 * sdio_memcpy_fromio - read a chunk of memory from a SDIO function
463 * @func: SDIO function to access
464 * @dst: buffer to store the data
465 * @addr: address to begin reading from
466 * @count: number of bytes to read
468 * Reads from the address space of a given SDIO function. Return
469 * value indicates if the transfer succeeded or not.
471 int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
472 unsigned int addr, int count)
474 return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count);
476 EXPORT_SYMBOL_GPL(sdio_memcpy_fromio);
479 * sdio_memcpy_toio - write a chunk of memory to a SDIO function
480 * @func: SDIO function to access
481 * @addr: address to start writing to
482 * @src: buffer that contains the data to write
483 * @count: number of bytes to write
485 * Writes to the address space of a given SDIO function. Return
486 * value indicates if the transfer succeeded or not.
488 int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
489 void *src, int count)
491 return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count);
493 EXPORT_SYMBOL_GPL(sdio_memcpy_toio);
496 * sdio_readsb - read from a FIFO on a SDIO function
497 * @func: SDIO function to access
498 * @dst: buffer to store the data
499 * @addr: address of (single byte) FIFO
500 * @count: number of bytes to read
502 * Reads from the specified FIFO of a given SDIO function. Return
503 * value indicates if the transfer succeeded or not.
505 int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
508 return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count);
510 EXPORT_SYMBOL_GPL(sdio_readsb);
513 * sdio_writesb - write to a FIFO of a SDIO function
514 * @func: SDIO function to access
515 * @addr: address of (single byte) FIFO
516 * @src: buffer that contains the data to write
517 * @count: number of bytes to write
519 * Writes to the specified FIFO of a given SDIO function. Return
520 * value indicates if the transfer succeeded or not.
522 int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,
525 return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count);
527 EXPORT_SYMBOL_GPL(sdio_writesb);
530 * sdio_readw - read a 16 bit integer from a SDIO function
531 * @func: SDIO function to access
532 * @addr: address to read
533 * @err_ret: optional status value from transfer
535 * Reads a 16 bit integer from the address space of a given SDIO
536 * function. If there is a problem reading the address, 0xffff
537 * is returned and @err_ret will contain the error code.
539 u16 sdio_readw(struct sdio_func *func, unsigned int addr, int *err_ret)
543 ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);
549 return le16_to_cpup((__le16 *)func->tmpbuf);
551 EXPORT_SYMBOL_GPL(sdio_readw);
554 * sdio_writew - write a 16 bit integer to a SDIO function
555 * @func: SDIO function to access
556 * @b: integer to write
557 * @addr: address to write to
558 * @err_ret: optional status value from transfer
560 * Writes a 16 bit integer to the address space of a given SDIO
561 * function. @err_ret will contain the status of the actual
564 void sdio_writew(struct sdio_func *func, u16 b, unsigned int addr, int *err_ret)
568 *(__le16 *)func->tmpbuf = cpu_to_le16(b);
570 ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
574 EXPORT_SYMBOL_GPL(sdio_writew);
577 * sdio_readl - read a 32 bit integer from a SDIO function
578 * @func: SDIO function to access
579 * @addr: address to read
580 * @err_ret: optional status value from transfer
582 * Reads a 32 bit integer from the address space of a given SDIO
583 * function. If there is a problem reading the address,
584 * 0xffffffff is returned and @err_ret will contain the error
587 u32 sdio_readl(struct sdio_func *func, unsigned int addr, int *err_ret)
591 ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4);
597 return le32_to_cpup((__le32 *)func->tmpbuf);
599 EXPORT_SYMBOL_GPL(sdio_readl);
602 * sdio_writel - write a 32 bit integer to a SDIO function
603 * @func: SDIO function to access
604 * @b: integer to write
605 * @addr: address to write to
606 * @err_ret: optional status value from transfer
608 * Writes a 32 bit integer to the address space of a given SDIO
609 * function. @err_ret will contain the status of the actual
612 void sdio_writel(struct sdio_func *func, u32 b, unsigned int addr, int *err_ret)
616 *(__le32 *)func->tmpbuf = cpu_to_le32(b);
618 ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
622 EXPORT_SYMBOL_GPL(sdio_writel);
625 * sdio_f0_readb - read a single byte from SDIO function 0
626 * @func: an SDIO function of the card
627 * @addr: address to read
628 * @err_ret: optional status value from transfer
630 * Reads a single byte from the address space of SDIO function 0.
631 * If there is a problem reading the address, 0xff is returned
632 * and @err_ret will contain the error code.
634 unsigned char sdio_f0_readb(struct sdio_func *func, unsigned int addr,
646 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,
681 if ((addr < 0xF0 || addr > 0xFF) && (!mmc_card_lenient_fn0(func->card))) {
687 ret = mmc_io_rw_direct(func->card, 1, 0, addr, b, NULL);
691 EXPORT_SYMBOL_GPL(sdio_f0_writeb);
694 * sdio_get_host_pm_caps - get host power management capabilities
695 * @func: SDIO function attached to host
697 * Returns a capability bitmask corresponding to power management
698 * features supported by the host controller that the card function
699 * might rely upon during a system suspend. The host doesn't need
700 * to be claimed, nor the function active, for this information to be
703 mmc_pm_flag_t sdio_get_host_pm_caps(struct sdio_func *func)
708 return func->card->host->pm_caps;
710 EXPORT_SYMBOL_GPL(sdio_get_host_pm_caps);
713 * sdio_set_host_pm_flags - set wanted host power management capabilities
714 * @func: SDIO function attached to host
716 * Set a capability bitmask corresponding to wanted host controller
717 * power management features for the upcoming suspend state.
718 * This must be called, if needed, each time the suspend method of
719 * the function driver is called, and must contain only bits that
720 * were returned by sdio_get_host_pm_caps().
721 * The host doesn't need to be claimed, nor the function active,
722 * for this information to be set.
724 int sdio_set_host_pm_flags(struct sdio_func *func, mmc_pm_flag_t flags)
726 struct mmc_host *host;
731 host = func->card->host;
733 if (flags & ~host->pm_caps)
736 /* function suspend methods are serialized, hence no lock needed */
737 host->pm_flags |= flags;
740 EXPORT_SYMBOL_GPL(sdio_set_host_pm_flags);