2 * linux/drivers/mmc/core/sdio_io.c
4 * Copyright 2007 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
);
80 * FIXME: This should timeout based on information in the CIS,
81 * but we don't have card to parse that yet.
83 timeout
= jiffies
+ HZ
;
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
)
172 func
->card
->host
->max_blk_size
),
176 ret
= mmc_io_rw_direct(func
->card
, 1, 0,
177 SDIO_FBR_BASE(func
->num
) + SDIO_FBR_BLKSIZE
,
181 ret
= mmc_io_rw_direct(func
->card
, 1, 0,
182 SDIO_FBR_BASE(func
->num
) + SDIO_FBR_BLKSIZE
+ 1,
183 (blksz
>> 8) & 0xff, NULL
);
186 func
->cur_blksize
= blksz
;
190 EXPORT_SYMBOL_GPL(sdio_set_block_size
);
192 /* Split an arbitrarily sized data transfer into several
193 * IO_RW_EXTENDED commands. */
194 static int sdio_io_rw_ext_helper(struct sdio_func
*func
, int write
,
195 unsigned addr
, int incr_addr
, u8
*buf
, unsigned size
)
197 unsigned remainder
= size
;
201 /* Do the bulk of the transfer using block mode (if supported). */
202 if (func
->card
->cccr
.multi_block
) {
203 /* Blocks per command is limited by host count, host transfer
204 * size (we only use a single sg entry) and the maximum for
205 * IO_RW_EXTENDED of 511 blocks. */
206 max_blocks
= min(min(
207 func
->card
->host
->max_blk_count
,
208 func
->card
->host
->max_seg_size
/ func
->cur_blksize
),
211 while (remainder
> func
->cur_blksize
) {
214 blocks
= remainder
/ func
->cur_blksize
;
215 if (blocks
> max_blocks
)
217 size
= blocks
* func
->cur_blksize
;
219 ret
= mmc_io_rw_extended(func
->card
, write
,
220 func
->num
, addr
, incr_addr
, buf
,
221 blocks
, func
->cur_blksize
);
232 /* Write the remainder using byte mode. */
233 while (remainder
> 0) {
235 if (size
> func
->cur_blksize
)
236 size
= func
->cur_blksize
;
238 size
= 512; /* maximum size for byte mode */
240 ret
= mmc_io_rw_extended(func
->card
, write
, func
->num
, addr
,
241 incr_addr
, buf
, 1, size
);
254 * sdio_readb - read a single byte from a SDIO function
255 * @func: SDIO function to access
256 * @addr: address to read
257 * @err_ret: optional status value from transfer
259 * Reads a single byte from the address space of a given SDIO
260 * function. If there is a problem reading the address, 0xff
261 * is returned and @err_ret will contain the error code.
263 unsigned char sdio_readb(struct sdio_func
*func
, unsigned int addr
,
274 ret
= mmc_io_rw_direct(func
->card
, 0, func
->num
, addr
, 0, &val
);
283 EXPORT_SYMBOL_GPL(sdio_readb
);
286 * sdio_writeb - write a single byte to a SDIO function
287 * @func: SDIO function to access
289 * @addr: address to write to
290 * @err_ret: optional status value from transfer
292 * Writes a single byte to the address space of a given SDIO
293 * function. @err_ret will contain the status of the actual
296 void sdio_writeb(struct sdio_func
*func
, unsigned char b
, unsigned int addr
,
303 ret
= mmc_io_rw_direct(func
->card
, 1, func
->num
, addr
, b
, NULL
);
307 EXPORT_SYMBOL_GPL(sdio_writeb
);
310 * sdio_memcpy_fromio - read a chunk of memory from a SDIO function
311 * @func: SDIO function to access
312 * @dst: buffer to store the data
313 * @addr: address to begin reading from
314 * @count: number of bytes to read
316 * Reads from the address space of a given SDIO function. Return
317 * value indicates if the transfer succeeded or not.
319 int sdio_memcpy_fromio(struct sdio_func
*func
, void *dst
,
320 unsigned int addr
, int count
)
322 return sdio_io_rw_ext_helper(func
, 0, addr
, 1, dst
, count
);
324 EXPORT_SYMBOL_GPL(sdio_memcpy_fromio
);
327 * sdio_memcpy_toio - write a chunk of memory to a SDIO function
328 * @func: SDIO function to access
329 * @addr: address to start writing to
330 * @src: buffer that contains the data to write
331 * @count: number of bytes to write
333 * Writes to the address space of a given SDIO function. Return
334 * value indicates if the transfer succeeded or not.
336 int sdio_memcpy_toio(struct sdio_func
*func
, unsigned int addr
,
337 void *src
, int count
)
339 return sdio_io_rw_ext_helper(func
, 1, addr
, 1, src
, count
);
341 EXPORT_SYMBOL_GPL(sdio_memcpy_toio
);
344 * sdio_readsb - read from a FIFO on a SDIO function
345 * @func: SDIO function to access
346 * @dst: buffer to store the data
347 * @addr: address of (single byte) FIFO
348 * @count: number of bytes to read
350 * Reads from the specified FIFO of a given SDIO function. Return
351 * value indicates if the transfer succeeded or not.
353 int sdio_readsb(struct sdio_func
*func
, void *dst
, unsigned int addr
,
356 return sdio_io_rw_ext_helper(func
, 0, addr
, 0, dst
, count
);
359 EXPORT_SYMBOL_GPL(sdio_readsb
);
362 * sdio_writesb - write to a FIFO of a SDIO function
363 * @func: SDIO function to access
364 * @addr: address of (single byte) FIFO
365 * @src: buffer that contains the data to write
366 * @count: number of bytes to write
368 * Writes to the specified FIFO of a given SDIO function. Return
369 * value indicates if the transfer succeeded or not.
371 int sdio_writesb(struct sdio_func
*func
, unsigned int addr
, void *src
,
374 return sdio_io_rw_ext_helper(func
, 1, addr
, 0, src
, count
);
376 EXPORT_SYMBOL_GPL(sdio_writesb
);
379 * sdio_readw - read a 16 bit integer from a SDIO function
380 * @func: SDIO function to access
381 * @addr: address to read
382 * @err_ret: optional status value from transfer
384 * Reads a 16 bit integer from the address space of a given SDIO
385 * function. If there is a problem reading the address, 0xffff
386 * is returned and @err_ret will contain the error code.
388 unsigned short sdio_readw(struct sdio_func
*func
, unsigned int addr
,
396 ret
= sdio_memcpy_fromio(func
, func
->tmpbuf
, addr
, 2);
403 return le16_to_cpu(*(u16
*)func
->tmpbuf
);
405 EXPORT_SYMBOL_GPL(sdio_readw
);
408 * sdio_writew - write a 16 bit integer to a SDIO function
409 * @func: SDIO function to access
410 * @b: integer to write
411 * @addr: address to write to
412 * @err_ret: optional status value from transfer
414 * Writes a 16 bit integer to the address space of a given SDIO
415 * function. @err_ret will contain the status of the actual
418 void sdio_writew(struct sdio_func
*func
, unsigned short b
, unsigned int addr
,
423 *(u16
*)func
->tmpbuf
= cpu_to_le16(b
);
425 ret
= sdio_memcpy_toio(func
, addr
, func
->tmpbuf
, 2);
429 EXPORT_SYMBOL_GPL(sdio_writew
);
432 * sdio_readl - read a 32 bit integer from a SDIO function
433 * @func: SDIO function to access
434 * @addr: address to read
435 * @err_ret: optional status value from transfer
437 * Reads a 32 bit integer from the address space of a given SDIO
438 * function. If there is a problem reading the address,
439 * 0xffffffff is returned and @err_ret will contain the error
442 unsigned long sdio_readl(struct sdio_func
*func
, unsigned int addr
,
450 ret
= sdio_memcpy_fromio(func
, func
->tmpbuf
, addr
, 4);
457 return le32_to_cpu(*(u32
*)func
->tmpbuf
);
459 EXPORT_SYMBOL_GPL(sdio_readl
);
462 * sdio_writel - write a 32 bit integer to a SDIO function
463 * @func: SDIO function to access
464 * @b: integer to write
465 * @addr: address to write to
466 * @err_ret: optional status value from transfer
468 * Writes a 32 bit integer to the address space of a given SDIO
469 * function. @err_ret will contain the status of the actual
472 void sdio_writel(struct sdio_func
*func
, unsigned long b
, unsigned int addr
,
477 *(u32
*)func
->tmpbuf
= cpu_to_le32(b
);
479 ret
= sdio_memcpy_toio(func
, addr
, func
->tmpbuf
, 4);
483 EXPORT_SYMBOL_GPL(sdio_writel
);