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/mmc/host.h>
13 #include <linux/mmc/card.h>
14 #include <linux/mmc/sdio.h>
15 #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 = min(func->card->host->max_seg_size,
191 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 return min(mval, 512u); /* maximum size for byte mode */
202 * sdio_align_size - pads a transfer size to a more optimal value
203 * @func: SDIO function
204 * @sz: original transfer size
206 * Pads the original data size with a number of extra bytes in
207 * order to avoid controller bugs and/or performance hits
208 * (e.g. some controllers revert to PIO for certain sizes).
210 * If possible, it will also adjust the size so that it can be
211 * handled in just a single request.
213 * Returns the improved size, which might be unmodified.
215 unsigned int sdio_align_size(struct sdio_func *func, unsigned int sz)
217 unsigned int orig_sz;
218 unsigned int blk_sz, byte_sz;
224 * Do a first check with the controller, in case it
225 * wants to increase the size up to a point where it
226 * might need more than one block.
228 sz = mmc_align_data_size(func->card, sz);
231 * If we can still do this with just a byte transfer, then
234 if (sz <= sdio_max_byte_size(func))
237 if (func->card->cccr.multi_block) {
239 * Check if the transfer is already block aligned
241 if ((sz % func->cur_blksize) == 0)
245 * Realign it so that it can be done with one request,
246 * and recheck if the controller still likes it.
248 blk_sz = ((sz + func->cur_blksize - 1) /
249 func->cur_blksize) * func->cur_blksize;
250 blk_sz = mmc_align_data_size(func->card, blk_sz);
253 * This value is only good if it is still just
256 if ((blk_sz % func->cur_blksize) == 0)
260 * We failed to do one request, but at least try to
261 * pad the remainder properly.
263 byte_sz = mmc_align_data_size(func->card,
264 sz % func->cur_blksize);
265 if (byte_sz <= sdio_max_byte_size(func)) {
266 blk_sz = sz / func->cur_blksize;
267 return blk_sz * func->cur_blksize + byte_sz;
271 * We need multiple requests, so first check that the
272 * controller can handle the chunk size;
274 chunk_sz = mmc_align_data_size(func->card,
275 sdio_max_byte_size(func));
276 if (chunk_sz == sdio_max_byte_size(func)) {
278 * Fix up the size of the remainder (if any)
280 byte_sz = orig_sz % chunk_sz;
282 byte_sz = mmc_align_data_size(func->card,
286 return (orig_sz / chunk_sz) * chunk_sz + byte_sz;
291 * The controller is simply incapable of transferring the size
292 * we want in decent manner, so just return the original size.
296 EXPORT_SYMBOL_GPL(sdio_align_size);
298 /* Split an arbitrarily sized data transfer into several
299 * IO_RW_EXTENDED commands. */
300 static int sdio_io_rw_ext_helper(struct sdio_func *func, int write,
301 unsigned addr, int incr_addr, u8 *buf, unsigned size)
303 unsigned remainder = size;
307 /* Do the bulk of the transfer using block mode (if supported). */
308 if (func->card->cccr.multi_block && (size > sdio_max_byte_size(func))) {
309 /* Blocks per command is limited by host count, host transfer
310 * size (we only use a single sg entry) and the maximum for
311 * IO_RW_EXTENDED of 511 blocks. */
312 max_blocks = min(func->card->host->max_blk_count,
313 func->card->host->max_seg_size / func->cur_blksize);
314 max_blocks = min(max_blocks, 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 ret = mmc_io_rw_extended(func->card, write, func->num, addr,
342 incr_addr, buf, 1, size);
355 * sdio_readb - read a single byte from a SDIO function
356 * @func: SDIO function to access
357 * @addr: address to read
358 * @err_ret: optional status value from transfer
360 * Reads a single byte from the address space of a given SDIO
361 * function. If there is a problem reading the address, 0xff
362 * is returned and @err_ret will contain the error code.
364 u8 sdio_readb(struct sdio_func *func, unsigned int addr, int *err_ret)
374 ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val);
383 EXPORT_SYMBOL_GPL(sdio_readb);
386 * sdio_writeb - write a single byte to a SDIO function
387 * @func: SDIO function to access
389 * @addr: address to write to
390 * @err_ret: optional status value from transfer
392 * Writes a single byte to the address space of a given SDIO
393 * function. @err_ret will contain the status of the actual
396 void sdio_writeb(struct sdio_func *func, u8 b, unsigned int addr, int *err_ret)
402 ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL);
406 EXPORT_SYMBOL_GPL(sdio_writeb);
409 * sdio_memcpy_fromio - read a chunk of memory from a SDIO function
410 * @func: SDIO function to access
411 * @dst: buffer to store the data
412 * @addr: address to begin reading from
413 * @count: number of bytes to read
415 * Reads from the address space of a given SDIO function. Return
416 * value indicates if the transfer succeeded or not.
418 int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
419 unsigned int addr, int count)
421 return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count);
423 EXPORT_SYMBOL_GPL(sdio_memcpy_fromio);
426 * sdio_memcpy_toio - write a chunk of memory to a SDIO function
427 * @func: SDIO function to access
428 * @addr: address to start writing to
429 * @src: buffer that contains the data to write
430 * @count: number of bytes to write
432 * Writes to the address space of a given SDIO function. Return
433 * value indicates if the transfer succeeded or not.
435 int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
436 void *src, int count)
438 return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count);
440 EXPORT_SYMBOL_GPL(sdio_memcpy_toio);
443 * sdio_readsb - read from a FIFO on a SDIO function
444 * @func: SDIO function to access
445 * @dst: buffer to store the data
446 * @addr: address of (single byte) FIFO
447 * @count: number of bytes to read
449 * Reads from the specified FIFO of a given SDIO function. Return
450 * value indicates if the transfer succeeded or not.
452 int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
455 return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count);
457 EXPORT_SYMBOL_GPL(sdio_readsb);
460 * sdio_writesb - write to a FIFO of a SDIO function
461 * @func: SDIO function to access
462 * @addr: address of (single byte) FIFO
463 * @src: buffer that contains the data to write
464 * @count: number of bytes to write
466 * Writes to the specified FIFO of a given SDIO function. Return
467 * value indicates if the transfer succeeded or not.
469 int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,
472 return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count);
474 EXPORT_SYMBOL_GPL(sdio_writesb);
477 * sdio_readw - read a 16 bit integer from a SDIO function
478 * @func: SDIO function to access
479 * @addr: address to read
480 * @err_ret: optional status value from transfer
482 * Reads a 16 bit integer from the address space of a given SDIO
483 * function. If there is a problem reading the address, 0xffff
484 * is returned and @err_ret will contain the error code.
486 u16 sdio_readw(struct sdio_func *func, unsigned int addr, int *err_ret)
493 ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);
500 return le16_to_cpup((__le16 *)func->tmpbuf);
502 EXPORT_SYMBOL_GPL(sdio_readw);
505 * sdio_writew - write a 16 bit integer to a SDIO function
506 * @func: SDIO function to access
507 * @b: integer to write
508 * @addr: address to write to
509 * @err_ret: optional status value from transfer
511 * Writes a 16 bit integer to the address space of a given SDIO
512 * function. @err_ret will contain the status of the actual
515 void sdio_writew(struct sdio_func *func, u16 b, unsigned int addr, int *err_ret)
519 *(__le16 *)func->tmpbuf = cpu_to_le16(b);
521 ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
525 EXPORT_SYMBOL_GPL(sdio_writew);
528 * sdio_readl - read a 32 bit integer from a SDIO function
529 * @func: SDIO function to access
530 * @addr: address to read
531 * @err_ret: optional status value from transfer
533 * Reads a 32 bit integer from the address space of a given SDIO
534 * function. If there is a problem reading the address,
535 * 0xffffffff is returned and @err_ret will contain the error
538 u32 sdio_readl(struct sdio_func *func, unsigned int addr, int *err_ret)
545 ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4);
552 return le32_to_cpup((__le32 *)func->tmpbuf);
554 EXPORT_SYMBOL_GPL(sdio_readl);
557 * sdio_writel - write a 32 bit integer to a SDIO function
558 * @func: SDIO function to access
559 * @b: integer to write
560 * @addr: address to write to
561 * @err_ret: optional status value from transfer
563 * Writes a 32 bit integer to the address space of a given SDIO
564 * function. @err_ret will contain the status of the actual
567 void sdio_writel(struct sdio_func *func, u32 b, unsigned int addr, int *err_ret)
571 *(__le32 *)func->tmpbuf = cpu_to_le32(b);
573 ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
577 EXPORT_SYMBOL_GPL(sdio_writel);
580 * sdio_f0_readb - read a single byte from SDIO function 0
581 * @func: an SDIO function of the card
582 * @addr: address to read
583 * @err_ret: optional status value from transfer
585 * Reads a single byte from the address space of SDIO function 0.
586 * If there is a problem reading the address, 0xff is returned
587 * and @err_ret will contain the error code.
589 unsigned char sdio_f0_readb(struct sdio_func *func, unsigned int addr,
600 ret = mmc_io_rw_direct(func->card, 0, 0, addr, 0, &val);
609 EXPORT_SYMBOL_GPL(sdio_f0_readb);
612 * sdio_f0_writeb - write a single byte to SDIO function 0
613 * @func: an SDIO function of the card
615 * @addr: address to write to
616 * @err_ret: optional status value from transfer
618 * Writes a single byte to the address space of SDIO function 0.
619 * @err_ret will contain the status of the actual transfer.
621 * Only writes to the vendor specific CCCR registers (0xF0 -
622 * 0xFF) are permiited; @err_ret will be set to -EINVAL for *
623 * writes outside this range.
625 void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
632 if ((addr < 0xF0 || addr > 0xFF) && (!mmc_card_lenient_fn0(func->card))) {
638 ret = mmc_io_rw_direct(func->card, 1, 0, addr, b, NULL);
642 EXPORT_SYMBOL_GPL(sdio_f0_writeb);