perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / mmc / core / sdio_io.c
blobd40744bbafa9a95e7a36bd8efb980ac9a78c2f1e
1 /*
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>
18 #include "sdio_ops.h"
19 #include "core.h"
20 #include "card.h"
22 /**
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)
31 if (WARN_ON(!func))
32 return;
34 mmc_claim_host(func->card->host);
36 EXPORT_SYMBOL_GPL(sdio_claim_host);
38 /**
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
43 * operations.
45 void sdio_release_host(struct sdio_func *func)
47 if (WARN_ON(!func))
48 return;
50 mmc_release_host(func->card->host);
52 EXPORT_SYMBOL_GPL(sdio_release_host);
54 /**
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
59 * access is possible.
61 int sdio_enable_func(struct sdio_func *func)
63 int ret;
64 unsigned char reg;
65 unsigned long timeout;
67 if (!func)
68 return -EINVAL;
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, &reg);
73 if (ret)
74 goto err;
76 reg |= 1 << func->num;
78 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);
79 if (ret)
80 goto err;
82 timeout = jiffies + msecs_to_jiffies(func->enable_timeout);
84 while (1) {
85 ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IORx, 0, &reg);
86 if (ret)
87 goto err;
88 if (reg & (1 << func->num))
89 break;
90 ret = -ETIME;
91 if (time_after(jiffies, timeout))
92 goto err;
95 pr_debug("SDIO: Enabled device %s\n", sdio_func_id(func));
97 return 0;
99 err:
100 pr_debug("SDIO: Failed to enable device %s\n", sdio_func_id(func));
101 return ret;
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)
114 int ret;
115 unsigned char reg;
117 if (!func)
118 return -EINVAL;
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, &reg);
123 if (ret)
124 goto err;
126 reg &= ~(1 << func->num);
128 ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);
129 if (ret)
130 goto err;
132 pr_debug("SDIO: Disabled device %s\n", sdio_func_id(func));
134 return 0;
136 err:
137 pr_debug("SDIO: Failed to disable device %s\n", sdio_func_id(func));
138 return -EIO;
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)
163 int ret;
165 if (blksz > func->card->host->max_blk_size)
166 return -EINVAL;
168 if (blksz == 0) {
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,
175 blksz & 0xff, NULL);
176 if (ret)
177 return ret;
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);
181 if (ret)
182 return ret;
183 func->cur_blksize = blksz;
184 return 0;
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);
197 else
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;
224 unsigned chunk_sz;
226 orig_sz = 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
237 * we're done.
239 if (sz <= sdio_max_byte_size(func))
240 return sz;
242 if (func->card->cccr.multi_block) {
244 * Check if the transfer is already block aligned
246 if ((sz % func->cur_blksize) == 0)
247 return sz;
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
259 * one request.
261 if ((blk_sz % func->cur_blksize) == 0)
262 return blk_sz;
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;
274 } else {
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;
286 if (byte_sz) {
287 byte_sz = mmc_align_data_size(func->card,
288 byte_sz);
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.
299 return orig_sz;
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;
309 unsigned max_blocks;
310 int ret;
312 if (!func || (func->num > 7))
313 return -EINVAL;
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) {
322 unsigned blocks;
324 blocks = remainder / func->cur_blksize;
325 if (blocks > max_blocks)
326 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);
332 if (ret)
333 return ret;
335 remainder -= size;
336 buf += size;
337 if (incr_addr)
338 addr += size;
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);
349 if (ret)
350 return ret;
352 remainder -= size;
353 buf += size;
354 if (incr_addr)
355 addr += size;
357 return 0;
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)
372 int ret;
373 u8 val;
375 if (!func) {
376 if (err_ret)
377 *err_ret = -EINVAL;
378 return 0xFF;
381 ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val);
382 if (err_ret)
383 *err_ret = ret;
384 if (ret)
385 return 0xFF;
387 return val;
389 EXPORT_SYMBOL_GPL(sdio_readb);
392 * sdio_writeb - write a single byte to a SDIO function
393 * @func: SDIO function to access
394 * @b: byte to write
395 * @addr: address to write to
396 * @err_ret: optional status value from transfer
398 * Writes a single byte to the address space of a given SDIO
399 * function. @err_ret will contain the status of the actual
400 * transfer.
402 void sdio_writeb(struct sdio_func *func, u8 b, unsigned int addr, int *err_ret)
404 int ret;
406 if (!func) {
407 if (err_ret)
408 *err_ret = -EINVAL;
409 return;
412 ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL);
413 if (err_ret)
414 *err_ret = ret;
416 EXPORT_SYMBOL_GPL(sdio_writeb);
419 * sdio_writeb_readb - write and read a byte from SDIO function
420 * @func: SDIO function to access
421 * @write_byte: byte to write
422 * @addr: address to write to
423 * @err_ret: optional status value from transfer
425 * Performs a RAW (Read after Write) operation as defined by SDIO spec -
426 * single byte is written to address space of a given SDIO function and
427 * response is read back from the same address, both using single request.
428 * If there is a problem with the operation, 0xff is returned and
429 * @err_ret will contain the error code.
431 u8 sdio_writeb_readb(struct sdio_func *func, u8 write_byte,
432 unsigned int addr, int *err_ret)
434 int ret;
435 u8 val;
437 ret = mmc_io_rw_direct(func->card, 1, func->num, addr,
438 write_byte, &val);
439 if (err_ret)
440 *err_ret = ret;
441 if (ret)
442 return 0xff;
444 return val;
446 EXPORT_SYMBOL_GPL(sdio_writeb_readb);
449 * sdio_memcpy_fromio - read a chunk of memory from a SDIO function
450 * @func: SDIO function to access
451 * @dst: buffer to store the data
452 * @addr: address to begin reading from
453 * @count: number of bytes to read
455 * Reads from the address space of a given SDIO function. Return
456 * value indicates if the transfer succeeded or not.
458 int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
459 unsigned int addr, int count)
461 return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count);
463 EXPORT_SYMBOL_GPL(sdio_memcpy_fromio);
466 * sdio_memcpy_toio - write a chunk of memory to a SDIO function
467 * @func: SDIO function to access
468 * @addr: address to start writing to
469 * @src: buffer that contains the data to write
470 * @count: number of bytes to write
472 * Writes to the address space of a given SDIO function. Return
473 * value indicates if the transfer succeeded or not.
475 int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
476 void *src, int count)
478 return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count);
480 EXPORT_SYMBOL_GPL(sdio_memcpy_toio);
483 * sdio_readsb - read from a FIFO on a SDIO function
484 * @func: SDIO function to access
485 * @dst: buffer to store the data
486 * @addr: address of (single byte) FIFO
487 * @count: number of bytes to read
489 * Reads from the specified FIFO of a given SDIO function. Return
490 * value indicates if the transfer succeeded or not.
492 int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
493 int count)
495 return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count);
497 EXPORT_SYMBOL_GPL(sdio_readsb);
500 * sdio_writesb - write to a FIFO of a SDIO function
501 * @func: SDIO function to access
502 * @addr: address of (single byte) FIFO
503 * @src: buffer that contains the data to write
504 * @count: number of bytes to write
506 * Writes to the specified FIFO of a given SDIO function. Return
507 * value indicates if the transfer succeeded or not.
509 int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,
510 int count)
512 return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count);
514 EXPORT_SYMBOL_GPL(sdio_writesb);
517 * sdio_readw - read a 16 bit integer from a SDIO function
518 * @func: SDIO function to access
519 * @addr: address to read
520 * @err_ret: optional status value from transfer
522 * Reads a 16 bit integer from the address space of a given SDIO
523 * function. If there is a problem reading the address, 0xffff
524 * is returned and @err_ret will contain the error code.
526 u16 sdio_readw(struct sdio_func *func, unsigned int addr, int *err_ret)
528 int ret;
530 ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);
531 if (err_ret)
532 *err_ret = ret;
533 if (ret)
534 return 0xFFFF;
536 return le16_to_cpup((__le16 *)func->tmpbuf);
538 EXPORT_SYMBOL_GPL(sdio_readw);
541 * sdio_writew - write a 16 bit integer to a SDIO function
542 * @func: SDIO function to access
543 * @b: integer to write
544 * @addr: address to write to
545 * @err_ret: optional status value from transfer
547 * Writes a 16 bit integer to the address space of a given SDIO
548 * function. @err_ret will contain the status of the actual
549 * transfer.
551 void sdio_writew(struct sdio_func *func, u16 b, unsigned int addr, int *err_ret)
553 int ret;
555 *(__le16 *)func->tmpbuf = cpu_to_le16(b);
557 ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
558 if (err_ret)
559 *err_ret = ret;
561 EXPORT_SYMBOL_GPL(sdio_writew);
564 * sdio_readl - read a 32 bit integer from a SDIO function
565 * @func: SDIO function to access
566 * @addr: address to read
567 * @err_ret: optional status value from transfer
569 * Reads a 32 bit integer from the address space of a given SDIO
570 * function. If there is a problem reading the address,
571 * 0xffffffff is returned and @err_ret will contain the error
572 * code.
574 u32 sdio_readl(struct sdio_func *func, unsigned int addr, int *err_ret)
576 int ret;
578 ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4);
579 if (err_ret)
580 *err_ret = ret;
581 if (ret)
582 return 0xFFFFFFFF;
584 return le32_to_cpup((__le32 *)func->tmpbuf);
586 EXPORT_SYMBOL_GPL(sdio_readl);
589 * sdio_writel - write a 32 bit integer to a SDIO function
590 * @func: SDIO function to access
591 * @b: integer to write
592 * @addr: address to write to
593 * @err_ret: optional status value from transfer
595 * Writes a 32 bit integer to the address space of a given SDIO
596 * function. @err_ret will contain the status of the actual
597 * transfer.
599 void sdio_writel(struct sdio_func *func, u32 b, unsigned int addr, int *err_ret)
601 int ret;
603 *(__le32 *)func->tmpbuf = cpu_to_le32(b);
605 ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
606 if (err_ret)
607 *err_ret = ret;
609 EXPORT_SYMBOL_GPL(sdio_writel);
612 * sdio_f0_readb - read a single byte from SDIO function 0
613 * @func: an SDIO function of the card
614 * @addr: address to read
615 * @err_ret: optional status value from transfer
617 * Reads a single byte from the address space of SDIO function 0.
618 * If there is a problem reading the address, 0xff is returned
619 * and @err_ret will contain the error code.
621 unsigned char sdio_f0_readb(struct sdio_func *func, unsigned int addr,
622 int *err_ret)
624 int ret;
625 unsigned char val;
627 if (!func) {
628 if (err_ret)
629 *err_ret = -EINVAL;
630 return 0xFF;
633 ret = mmc_io_rw_direct(func->card, 0, 0, addr, 0, &val);
634 if (err_ret)
635 *err_ret = ret;
636 if (ret)
637 return 0xFF;
639 return val;
641 EXPORT_SYMBOL_GPL(sdio_f0_readb);
644 * sdio_f0_writeb - write a single byte to SDIO function 0
645 * @func: an SDIO function of the card
646 * @b: byte to write
647 * @addr: address to write to
648 * @err_ret: optional status value from transfer
650 * Writes a single byte to the address space of SDIO function 0.
651 * @err_ret will contain the status of the actual transfer.
653 * Only writes to the vendor specific CCCR registers (0xF0 -
654 * 0xFF) are permiited; @err_ret will be set to -EINVAL for *
655 * writes outside this range.
657 void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
658 int *err_ret)
660 int ret;
662 if (!func) {
663 if (err_ret)
664 *err_ret = -EINVAL;
665 return;
668 if ((addr < 0xF0 || addr > 0xFF) && (!mmc_card_lenient_fn0(func->card))) {
669 if (err_ret)
670 *err_ret = -EINVAL;
671 return;
674 ret = mmc_io_rw_direct(func->card, 1, 0, addr, b, NULL);
675 if (err_ret)
676 *err_ret = ret;
678 EXPORT_SYMBOL_GPL(sdio_f0_writeb);
681 * sdio_get_host_pm_caps - get host power management capabilities
682 * @func: SDIO function attached to host
684 * Returns a capability bitmask corresponding to power management
685 * features supported by the host controller that the card function
686 * might rely upon during a system suspend. The host doesn't need
687 * to be claimed, nor the function active, for this information to be
688 * obtained.
690 mmc_pm_flag_t sdio_get_host_pm_caps(struct sdio_func *func)
692 if (!func)
693 return 0;
695 return func->card->host->pm_caps;
697 EXPORT_SYMBOL_GPL(sdio_get_host_pm_caps);
700 * sdio_set_host_pm_flags - set wanted host power management capabilities
701 * @func: SDIO function attached to host
703 * Set a capability bitmask corresponding to wanted host controller
704 * power management features for the upcoming suspend state.
705 * This must be called, if needed, each time the suspend method of
706 * the function driver is called, and must contain only bits that
707 * were returned by sdio_get_host_pm_caps().
708 * The host doesn't need to be claimed, nor the function active,
709 * for this information to be set.
711 int sdio_set_host_pm_flags(struct sdio_func *func, mmc_pm_flag_t flags)
713 struct mmc_host *host;
715 if (!func)
716 return -EINVAL;
718 host = func->card->host;
720 if (flags & ~host->pm_caps)
721 return -EINVAL;
723 /* function suspend methods are serialized, hence no lock needed */
724 host->pm_flags |= flags;
725 return 0;
727 EXPORT_SYMBOL_GPL(sdio_set_host_pm_flags);