Pierre Ossman | 46f555f | 2007-05-27 12:57:15 +0200 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/mmc/core/sdio_io.c |
| 3 | * |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 4 | * Copyright 2007-2008 Pierre Ossman |
Pierre Ossman | 46f555f | 2007-05-27 12:57:15 +0200 | [diff] [blame] | 5 | * |
| 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. |
| 10 | */ |
| 11 | |
Paul Gortmaker | 3ef77af | 2011-07-10 12:42:00 -0400 | [diff] [blame^] | 12 | #include <linux/export.h> |
Pierre Ossman | 46f555f | 2007-05-27 12:57:15 +0200 | [diff] [blame] | 13 | #include <linux/mmc/host.h> |
| 14 | #include <linux/mmc/card.h> |
Pierre Ossman | fa64efa | 2007-05-27 14:22:37 +0200 | [diff] [blame] | 15 | #include <linux/mmc/sdio.h> |
Pierre Ossman | 46f555f | 2007-05-27 12:57:15 +0200 | [diff] [blame] | 16 | #include <linux/mmc/sdio_func.h> |
| 17 | |
| 18 | #include "sdio_ops.h" |
| 19 | |
| 20 | /** |
| 21 | * sdio_claim_host - exclusively claim a bus for a certain SDIO function |
| 22 | * @func: SDIO function that will be accessed |
| 23 | * |
| 24 | * Claim a bus for a set of operations. The SDIO function given |
| 25 | * is used to figure out which bus is relevant. |
| 26 | */ |
| 27 | void sdio_claim_host(struct sdio_func *func) |
| 28 | { |
| 29 | BUG_ON(!func); |
| 30 | BUG_ON(!func->card); |
| 31 | |
| 32 | mmc_claim_host(func->card->host); |
| 33 | } |
| 34 | EXPORT_SYMBOL_GPL(sdio_claim_host); |
| 35 | |
| 36 | /** |
| 37 | * sdio_release_host - release a bus for a certain SDIO function |
| 38 | * @func: SDIO function that was accessed |
| 39 | * |
| 40 | * Release a bus, allowing others to claim the bus for their |
| 41 | * operations. |
| 42 | */ |
| 43 | void sdio_release_host(struct sdio_func *func) |
| 44 | { |
| 45 | BUG_ON(!func); |
| 46 | BUG_ON(!func->card); |
| 47 | |
| 48 | mmc_release_host(func->card->host); |
| 49 | } |
| 50 | EXPORT_SYMBOL_GPL(sdio_release_host); |
| 51 | |
| 52 | /** |
Pierre Ossman | fa64efa | 2007-05-27 14:22:37 +0200 | [diff] [blame] | 53 | * sdio_enable_func - enables a SDIO function for usage |
| 54 | * @func: SDIO function to enable |
| 55 | * |
| 56 | * Powers up and activates a SDIO function so that register |
| 57 | * access is possible. |
| 58 | */ |
| 59 | int sdio_enable_func(struct sdio_func *func) |
| 60 | { |
| 61 | int ret; |
| 62 | unsigned char reg; |
| 63 | unsigned long timeout; |
| 64 | |
| 65 | BUG_ON(!func); |
| 66 | BUG_ON(!func->card); |
| 67 | |
| 68 | pr_debug("SDIO: Enabling device %s...\n", sdio_func_id(func)); |
| 69 | |
| 70 | ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, ®); |
| 71 | if (ret) |
| 72 | goto err; |
| 73 | |
| 74 | reg |= 1 << func->num; |
| 75 | |
| 76 | ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL); |
| 77 | if (ret) |
| 78 | goto err; |
| 79 | |
Benzi Zbit | 62a7573 | 2008-07-10 02:41:43 +0300 | [diff] [blame] | 80 | timeout = jiffies + msecs_to_jiffies(func->enable_timeout); |
Pierre Ossman | fa64efa | 2007-05-27 14:22:37 +0200 | [diff] [blame] | 81 | |
| 82 | while (1) { |
| 83 | ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IORx, 0, ®); |
| 84 | if (ret) |
| 85 | goto err; |
| 86 | if (reg & (1 << func->num)) |
| 87 | break; |
| 88 | ret = -ETIME; |
| 89 | if (time_after(jiffies, timeout)) |
| 90 | goto err; |
| 91 | } |
| 92 | |
| 93 | pr_debug("SDIO: Enabled device %s\n", sdio_func_id(func)); |
| 94 | |
| 95 | return 0; |
| 96 | |
| 97 | err: |
| 98 | pr_debug("SDIO: Failed to enable device %s\n", sdio_func_id(func)); |
| 99 | return ret; |
| 100 | } |
| 101 | EXPORT_SYMBOL_GPL(sdio_enable_func); |
| 102 | |
| 103 | /** |
| 104 | * sdio_disable_func - disable a SDIO function |
| 105 | * @func: SDIO function to disable |
| 106 | * |
| 107 | * Powers down and deactivates a SDIO function. Register access |
| 108 | * to this function will fail until the function is reenabled. |
| 109 | */ |
| 110 | int sdio_disable_func(struct sdio_func *func) |
| 111 | { |
| 112 | int ret; |
| 113 | unsigned char reg; |
| 114 | |
| 115 | BUG_ON(!func); |
| 116 | BUG_ON(!func->card); |
| 117 | |
| 118 | pr_debug("SDIO: Disabling device %s...\n", sdio_func_id(func)); |
| 119 | |
| 120 | ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, ®); |
| 121 | if (ret) |
| 122 | goto err; |
| 123 | |
| 124 | reg &= ~(1 << func->num); |
| 125 | |
| 126 | ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL); |
| 127 | if (ret) |
| 128 | goto err; |
| 129 | |
| 130 | pr_debug("SDIO: Disabled device %s\n", sdio_func_id(func)); |
| 131 | |
| 132 | return 0; |
| 133 | |
| 134 | err: |
| 135 | pr_debug("SDIO: Failed to disable device %s\n", sdio_func_id(func)); |
| 136 | return -EIO; |
| 137 | } |
| 138 | EXPORT_SYMBOL_GPL(sdio_disable_func); |
| 139 | |
| 140 | /** |
David Vrabel | 9a08f82 | 2007-08-08 14:23:48 +0100 | [diff] [blame] | 141 | * sdio_set_block_size - set the block size of an SDIO function |
| 142 | * @func: SDIO function to change |
| 143 | * @blksz: new block size or 0 to use the default. |
| 144 | * |
| 145 | * The default block size is the largest supported by both the function |
| 146 | * and the host, with a maximum of 512 to ensure that arbitrarily sized |
| 147 | * data transfer use the optimal (least) number of commands. |
| 148 | * |
| 149 | * A driver may call this to override the default block size set by the |
| 150 | * core. This can be used to set a block size greater than the maximum |
| 151 | * that reported by the card; it is the driver's responsibility to ensure |
| 152 | * it uses a value that the card supports. |
| 153 | * |
| 154 | * Returns 0 on success, -EINVAL if the host does not support the |
| 155 | * requested block size, or -EIO (etc.) if one of the resultant FBR block |
| 156 | * size register writes failed. |
| 157 | * |
| 158 | */ |
| 159 | int sdio_set_block_size(struct sdio_func *func, unsigned blksz) |
| 160 | { |
| 161 | int ret; |
| 162 | |
| 163 | if (blksz > func->card->host->max_blk_size) |
| 164 | return -EINVAL; |
| 165 | |
| 166 | if (blksz == 0) { |
Tomas Winkler | 6d37333 | 2008-06-30 10:50:24 +0300 | [diff] [blame] | 167 | blksz = min(func->max_blksize, func->card->host->max_blk_size); |
| 168 | blksz = min(blksz, 512u); |
David Vrabel | 9a08f82 | 2007-08-08 14:23:48 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | ret = mmc_io_rw_direct(func->card, 1, 0, |
| 172 | SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE, |
| 173 | blksz & 0xff, NULL); |
| 174 | if (ret) |
| 175 | return ret; |
| 176 | ret = mmc_io_rw_direct(func->card, 1, 0, |
| 177 | SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE + 1, |
| 178 | (blksz >> 8) & 0xff, NULL); |
| 179 | if (ret) |
| 180 | return ret; |
| 181 | func->cur_blksize = blksz; |
| 182 | return 0; |
| 183 | } |
David Vrabel | 9a08f82 | 2007-08-08 14:23:48 +0100 | [diff] [blame] | 184 | EXPORT_SYMBOL_GPL(sdio_set_block_size); |
| 185 | |
Pierre Ossman | eea0f58 | 2008-06-28 13:22:40 +0200 | [diff] [blame] | 186 | /* |
| 187 | * Calculate the maximum byte mode transfer size |
| 188 | */ |
| 189 | static inline unsigned int sdio_max_byte_size(struct sdio_func *func) |
| 190 | { |
Tomas Winkler | ea90130 | 2008-07-10 03:01:56 +0300 | [diff] [blame] | 191 | unsigned mval = min(func->card->host->max_seg_size, |
| 192 | func->card->host->max_blk_size); |
Bing Zhao | 3fb7fb4 | 2010-03-05 13:43:25 -0800 | [diff] [blame] | 193 | |
| 194 | if (mmc_blksz_for_byte_mode(func->card)) |
| 195 | mval = min(mval, func->cur_blksize); |
| 196 | else |
| 197 | mval = min(mval, func->max_blksize); |
| 198 | |
Tomas Winkler | ea90130 | 2008-07-10 03:01:56 +0300 | [diff] [blame] | 199 | return min(mval, 512u); /* maximum size for byte mode */ |
Pierre Ossman | eea0f58 | 2008-06-28 13:22:40 +0200 | [diff] [blame] | 200 | } |
| 201 | |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 202 | /** |
| 203 | * sdio_align_size - pads a transfer size to a more optimal value |
| 204 | * @func: SDIO function |
| 205 | * @sz: original transfer size |
| 206 | * |
| 207 | * Pads the original data size with a number of extra bytes in |
| 208 | * order to avoid controller bugs and/or performance hits |
| 209 | * (e.g. some controllers revert to PIO for certain sizes). |
| 210 | * |
| 211 | * If possible, it will also adjust the size so that it can be |
| 212 | * handled in just a single request. |
| 213 | * |
| 214 | * Returns the improved size, which might be unmodified. |
| 215 | */ |
| 216 | unsigned int sdio_align_size(struct sdio_func *func, unsigned int sz) |
| 217 | { |
| 218 | unsigned int orig_sz; |
| 219 | unsigned int blk_sz, byte_sz; |
| 220 | unsigned chunk_sz; |
| 221 | |
| 222 | orig_sz = sz; |
| 223 | |
| 224 | /* |
| 225 | * Do a first check with the controller, in case it |
| 226 | * wants to increase the size up to a point where it |
| 227 | * might need more than one block. |
| 228 | */ |
| 229 | sz = mmc_align_data_size(func->card, sz); |
| 230 | |
| 231 | /* |
| 232 | * If we can still do this with just a byte transfer, then |
| 233 | * we're done. |
| 234 | */ |
Pierre Ossman | eea0f58 | 2008-06-28 13:22:40 +0200 | [diff] [blame] | 235 | if (sz <= sdio_max_byte_size(func)) |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 236 | return sz; |
| 237 | |
| 238 | if (func->card->cccr.multi_block) { |
| 239 | /* |
| 240 | * Check if the transfer is already block aligned |
| 241 | */ |
| 242 | if ((sz % func->cur_blksize) == 0) |
| 243 | return sz; |
| 244 | |
| 245 | /* |
| 246 | * Realign it so that it can be done with one request, |
| 247 | * and recheck if the controller still likes it. |
| 248 | */ |
| 249 | blk_sz = ((sz + func->cur_blksize - 1) / |
| 250 | func->cur_blksize) * func->cur_blksize; |
| 251 | blk_sz = mmc_align_data_size(func->card, blk_sz); |
| 252 | |
| 253 | /* |
| 254 | * This value is only good if it is still just |
| 255 | * one request. |
| 256 | */ |
| 257 | if ((blk_sz % func->cur_blksize) == 0) |
| 258 | return blk_sz; |
| 259 | |
| 260 | /* |
| 261 | * We failed to do one request, but at least try to |
| 262 | * pad the remainder properly. |
| 263 | */ |
| 264 | byte_sz = mmc_align_data_size(func->card, |
| 265 | sz % func->cur_blksize); |
Pierre Ossman | eea0f58 | 2008-06-28 13:22:40 +0200 | [diff] [blame] | 266 | if (byte_sz <= sdio_max_byte_size(func)) { |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 267 | blk_sz = sz / func->cur_blksize; |
| 268 | return blk_sz * func->cur_blksize + byte_sz; |
| 269 | } |
| 270 | } else { |
| 271 | /* |
| 272 | * We need multiple requests, so first check that the |
| 273 | * controller can handle the chunk size; |
| 274 | */ |
| 275 | chunk_sz = mmc_align_data_size(func->card, |
Pierre Ossman | eea0f58 | 2008-06-28 13:22:40 +0200 | [diff] [blame] | 276 | sdio_max_byte_size(func)); |
| 277 | if (chunk_sz == sdio_max_byte_size(func)) { |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 278 | /* |
| 279 | * Fix up the size of the remainder (if any) |
| 280 | */ |
| 281 | byte_sz = orig_sz % chunk_sz; |
| 282 | if (byte_sz) { |
| 283 | byte_sz = mmc_align_data_size(func->card, |
| 284 | byte_sz); |
| 285 | } |
| 286 | |
| 287 | return (orig_sz / chunk_sz) * chunk_sz + byte_sz; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * The controller is simply incapable of transferring the size |
| 293 | * we want in decent manner, so just return the original size. |
| 294 | */ |
| 295 | return orig_sz; |
| 296 | } |
| 297 | EXPORT_SYMBOL_GPL(sdio_align_size); |
| 298 | |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 299 | /* Split an arbitrarily sized data transfer into several |
| 300 | * IO_RW_EXTENDED commands. */ |
| 301 | static int sdio_io_rw_ext_helper(struct sdio_func *func, int write, |
| 302 | unsigned addr, int incr_addr, u8 *buf, unsigned size) |
| 303 | { |
| 304 | unsigned remainder = size; |
| 305 | unsigned max_blocks; |
| 306 | int ret; |
| 307 | |
| 308 | /* Do the bulk of the transfer using block mode (if supported). */ |
Pierre Ossman | eea0f58 | 2008-06-28 13:22:40 +0200 | [diff] [blame] | 309 | if (func->card->cccr.multi_block && (size > sdio_max_byte_size(func))) { |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 310 | /* Blocks per command is limited by host count, host transfer |
| 311 | * size (we only use a single sg entry) and the maximum for |
| 312 | * IO_RW_EXTENDED of 511 blocks. */ |
Tomas Winkler | 6d37333 | 2008-06-30 10:50:24 +0300 | [diff] [blame] | 313 | max_blocks = min(func->card->host->max_blk_count, |
| 314 | func->card->host->max_seg_size / func->cur_blksize); |
| 315 | max_blocks = min(max_blocks, 511u); |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 316 | |
| 317 | while (remainder > func->cur_blksize) { |
| 318 | unsigned blocks; |
| 319 | |
| 320 | blocks = remainder / func->cur_blksize; |
| 321 | if (blocks > max_blocks) |
| 322 | blocks = max_blocks; |
| 323 | size = blocks * func->cur_blksize; |
| 324 | |
| 325 | ret = mmc_io_rw_extended(func->card, write, |
| 326 | func->num, addr, incr_addr, buf, |
| 327 | blocks, func->cur_blksize); |
| 328 | if (ret) |
| 329 | return ret; |
| 330 | |
| 331 | remainder -= size; |
| 332 | buf += size; |
| 333 | if (incr_addr) |
| 334 | addr += size; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | /* Write the remainder using byte mode. */ |
| 339 | while (remainder > 0) { |
Pierre Ossman | eea0f58 | 2008-06-28 13:22:40 +0200 | [diff] [blame] | 340 | size = min(remainder, sdio_max_byte_size(func)); |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 341 | |
| 342 | ret = mmc_io_rw_extended(func->card, write, func->num, addr, |
| 343 | incr_addr, buf, 1, size); |
| 344 | if (ret) |
| 345 | return ret; |
| 346 | |
| 347 | remainder -= size; |
| 348 | buf += size; |
| 349 | if (incr_addr) |
| 350 | addr += size; |
| 351 | } |
| 352 | return 0; |
| 353 | } |
| 354 | |
David Vrabel | 9a08f82 | 2007-08-08 14:23:48 +0100 | [diff] [blame] | 355 | /** |
Pierre Ossman | 46f555f | 2007-05-27 12:57:15 +0200 | [diff] [blame] | 356 | * sdio_readb - read a single byte from a SDIO function |
| 357 | * @func: SDIO function to access |
| 358 | * @addr: address to read |
| 359 | * @err_ret: optional status value from transfer |
| 360 | * |
| 361 | * Reads a single byte from the address space of a given SDIO |
| 362 | * function. If there is a problem reading the address, 0xff |
| 363 | * is returned and @err_ret will contain the error code. |
| 364 | */ |
Tomas Winkler | 6d37333 | 2008-06-30 10:50:24 +0300 | [diff] [blame] | 365 | u8 sdio_readb(struct sdio_func *func, unsigned int addr, int *err_ret) |
Pierre Ossman | 46f555f | 2007-05-27 12:57:15 +0200 | [diff] [blame] | 366 | { |
| 367 | int ret; |
Tomas Winkler | 6d37333 | 2008-06-30 10:50:24 +0300 | [diff] [blame] | 368 | u8 val; |
Pierre Ossman | 46f555f | 2007-05-27 12:57:15 +0200 | [diff] [blame] | 369 | |
| 370 | BUG_ON(!func); |
| 371 | |
| 372 | if (err_ret) |
| 373 | *err_ret = 0; |
| 374 | |
| 375 | ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val); |
| 376 | if (ret) { |
| 377 | if (err_ret) |
| 378 | *err_ret = ret; |
| 379 | return 0xFF; |
| 380 | } |
| 381 | |
| 382 | return val; |
| 383 | } |
| 384 | EXPORT_SYMBOL_GPL(sdio_readb); |
| 385 | |
| 386 | /** |
| 387 | * sdio_writeb - write a single byte to a SDIO function |
| 388 | * @func: SDIO function to access |
| 389 | * @b: byte to write |
| 390 | * @addr: address to write to |
| 391 | * @err_ret: optional status value from transfer |
| 392 | * |
| 393 | * Writes a single byte to the address space of a given SDIO |
| 394 | * function. @err_ret will contain the status of the actual |
| 395 | * transfer. |
| 396 | */ |
Tomas Winkler | 6d37333 | 2008-06-30 10:50:24 +0300 | [diff] [blame] | 397 | void sdio_writeb(struct sdio_func *func, u8 b, unsigned int addr, int *err_ret) |
Pierre Ossman | 46f555f | 2007-05-27 12:57:15 +0200 | [diff] [blame] | 398 | { |
| 399 | int ret; |
| 400 | |
| 401 | BUG_ON(!func); |
| 402 | |
| 403 | ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL); |
| 404 | if (err_ret) |
| 405 | *err_ret = ret; |
| 406 | } |
| 407 | EXPORT_SYMBOL_GPL(sdio_writeb); |
| 408 | |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 409 | /** |
Grazvydas Ignotas | 6c1f716 | 2010-05-26 14:42:09 -0700 | [diff] [blame] | 410 | * sdio_writeb_readb - write and read a byte from SDIO function |
| 411 | * @func: SDIO function to access |
| 412 | * @write_byte: byte to write |
| 413 | * @addr: address to write to |
| 414 | * @err_ret: optional status value from transfer |
| 415 | * |
| 416 | * Performs a RAW (Read after Write) operation as defined by SDIO spec - |
| 417 | * single byte is written to address space of a given SDIO function and |
| 418 | * response is read back from the same address, both using single request. |
| 419 | * If there is a problem with the operation, 0xff is returned and |
| 420 | * @err_ret will contain the error code. |
| 421 | */ |
| 422 | u8 sdio_writeb_readb(struct sdio_func *func, u8 write_byte, |
| 423 | unsigned int addr, int *err_ret) |
| 424 | { |
| 425 | int ret; |
| 426 | u8 val; |
| 427 | |
| 428 | ret = mmc_io_rw_direct(func->card, 1, func->num, addr, |
| 429 | write_byte, &val); |
| 430 | if (err_ret) |
| 431 | *err_ret = ret; |
| 432 | if (ret) |
| 433 | val = 0xff; |
| 434 | |
| 435 | return val; |
| 436 | } |
| 437 | EXPORT_SYMBOL_GPL(sdio_writeb_readb); |
| 438 | |
| 439 | /** |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 440 | * sdio_memcpy_fromio - read a chunk of memory from a SDIO function |
| 441 | * @func: SDIO function to access |
| 442 | * @dst: buffer to store the data |
| 443 | * @addr: address to begin reading from |
| 444 | * @count: number of bytes to read |
| 445 | * |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 446 | * Reads from the address space of a given SDIO function. Return |
| 447 | * value indicates if the transfer succeeded or not. |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 448 | */ |
| 449 | int sdio_memcpy_fromio(struct sdio_func *func, void *dst, |
| 450 | unsigned int addr, int count) |
| 451 | { |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 452 | return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count); |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 453 | } |
| 454 | EXPORT_SYMBOL_GPL(sdio_memcpy_fromio); |
| 455 | |
| 456 | /** |
| 457 | * sdio_memcpy_toio - write a chunk of memory to a SDIO function |
| 458 | * @func: SDIO function to access |
| 459 | * @addr: address to start writing to |
| 460 | * @src: buffer that contains the data to write |
| 461 | * @count: number of bytes to write |
| 462 | * |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 463 | * Writes to the address space of a given SDIO function. Return |
| 464 | * value indicates if the transfer succeeded or not. |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 465 | */ |
| 466 | int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr, |
| 467 | void *src, int count) |
| 468 | { |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 469 | return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count); |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 470 | } |
| 471 | EXPORT_SYMBOL_GPL(sdio_memcpy_toio); |
| 472 | |
| 473 | /** |
| 474 | * sdio_readsb - read from a FIFO on a SDIO function |
| 475 | * @func: SDIO function to access |
| 476 | * @dst: buffer to store the data |
| 477 | * @addr: address of (single byte) FIFO |
| 478 | * @count: number of bytes to read |
| 479 | * |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 480 | * Reads from the specified FIFO of a given SDIO function. Return |
| 481 | * value indicates if the transfer succeeded or not. |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 482 | */ |
| 483 | int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr, |
| 484 | int count) |
| 485 | { |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 486 | return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count); |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 487 | } |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 488 | EXPORT_SYMBOL_GPL(sdio_readsb); |
| 489 | |
| 490 | /** |
| 491 | * sdio_writesb - write to a FIFO of a SDIO function |
| 492 | * @func: SDIO function to access |
| 493 | * @addr: address of (single byte) FIFO |
| 494 | * @src: buffer that contains the data to write |
| 495 | * @count: number of bytes to write |
| 496 | * |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 497 | * Writes to the specified FIFO of a given SDIO function. Return |
| 498 | * value indicates if the transfer succeeded or not. |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 499 | */ |
| 500 | int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src, |
| 501 | int count) |
| 502 | { |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 503 | return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count); |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 504 | } |
| 505 | EXPORT_SYMBOL_GPL(sdio_writesb); |
| 506 | |
| 507 | /** |
| 508 | * sdio_readw - read a 16 bit integer from a SDIO function |
| 509 | * @func: SDIO function to access |
| 510 | * @addr: address to read |
| 511 | * @err_ret: optional status value from transfer |
| 512 | * |
| 513 | * Reads a 16 bit integer from the address space of a given SDIO |
| 514 | * function. If there is a problem reading the address, 0xffff |
| 515 | * is returned and @err_ret will contain the error code. |
| 516 | */ |
Tomas Winkler | 6d37333 | 2008-06-30 10:50:24 +0300 | [diff] [blame] | 517 | u16 sdio_readw(struct sdio_func *func, unsigned int addr, int *err_ret) |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 518 | { |
| 519 | int ret; |
| 520 | |
| 521 | if (err_ret) |
| 522 | *err_ret = 0; |
| 523 | |
| 524 | ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2); |
| 525 | if (ret) { |
| 526 | if (err_ret) |
| 527 | *err_ret = ret; |
| 528 | return 0xFFFF; |
| 529 | } |
| 530 | |
Tomas Winkler | 6d37333 | 2008-06-30 10:50:24 +0300 | [diff] [blame] | 531 | return le16_to_cpup((__le16 *)func->tmpbuf); |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 532 | } |
| 533 | EXPORT_SYMBOL_GPL(sdio_readw); |
| 534 | |
| 535 | /** |
| 536 | * sdio_writew - write a 16 bit integer to a SDIO function |
| 537 | * @func: SDIO function to access |
| 538 | * @b: integer to write |
| 539 | * @addr: address to write to |
| 540 | * @err_ret: optional status value from transfer |
| 541 | * |
| 542 | * Writes a 16 bit integer to the address space of a given SDIO |
| 543 | * function. @err_ret will contain the status of the actual |
| 544 | * transfer. |
| 545 | */ |
Tomas Winkler | 6d37333 | 2008-06-30 10:50:24 +0300 | [diff] [blame] | 546 | void sdio_writew(struct sdio_func *func, u16 b, unsigned int addr, int *err_ret) |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 547 | { |
| 548 | int ret; |
| 549 | |
Tomas Winkler | 6d37333 | 2008-06-30 10:50:24 +0300 | [diff] [blame] | 550 | *(__le16 *)func->tmpbuf = cpu_to_le16(b); |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 551 | |
| 552 | ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2); |
| 553 | if (err_ret) |
| 554 | *err_ret = ret; |
| 555 | } |
| 556 | EXPORT_SYMBOL_GPL(sdio_writew); |
| 557 | |
| 558 | /** |
| 559 | * sdio_readl - read a 32 bit integer from a SDIO function |
| 560 | * @func: SDIO function to access |
| 561 | * @addr: address to read |
| 562 | * @err_ret: optional status value from transfer |
| 563 | * |
| 564 | * Reads a 32 bit integer from the address space of a given SDIO |
| 565 | * function. If there is a problem reading the address, |
| 566 | * 0xffffffff is returned and @err_ret will contain the error |
| 567 | * code. |
| 568 | */ |
Tomas Winkler | 6d37333 | 2008-06-30 10:50:24 +0300 | [diff] [blame] | 569 | u32 sdio_readl(struct sdio_func *func, unsigned int addr, int *err_ret) |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 570 | { |
| 571 | int ret; |
| 572 | |
| 573 | if (err_ret) |
| 574 | *err_ret = 0; |
| 575 | |
| 576 | ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4); |
| 577 | if (ret) { |
| 578 | if (err_ret) |
| 579 | *err_ret = ret; |
| 580 | return 0xFFFFFFFF; |
| 581 | } |
| 582 | |
Tomas Winkler | 6d37333 | 2008-06-30 10:50:24 +0300 | [diff] [blame] | 583 | return le32_to_cpup((__le32 *)func->tmpbuf); |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 584 | } |
| 585 | EXPORT_SYMBOL_GPL(sdio_readl); |
| 586 | |
| 587 | /** |
| 588 | * sdio_writel - write a 32 bit integer to a SDIO function |
| 589 | * @func: SDIO function to access |
| 590 | * @b: integer to write |
| 591 | * @addr: address to write to |
| 592 | * @err_ret: optional status value from transfer |
| 593 | * |
| 594 | * Writes a 32 bit integer to the address space of a given SDIO |
| 595 | * function. @err_ret will contain the status of the actual |
| 596 | * transfer. |
| 597 | */ |
Tomas Winkler | 6d37333 | 2008-06-30 10:50:24 +0300 | [diff] [blame] | 598 | void sdio_writel(struct sdio_func *func, u32 b, unsigned int addr, int *err_ret) |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 599 | { |
| 600 | int ret; |
| 601 | |
Tomas Winkler | 6d37333 | 2008-06-30 10:50:24 +0300 | [diff] [blame] | 602 | *(__le32 *)func->tmpbuf = cpu_to_le32(b); |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 603 | |
| 604 | ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4); |
| 605 | if (err_ret) |
| 606 | *err_ret = ret; |
| 607 | } |
| 608 | EXPORT_SYMBOL_GPL(sdio_writel); |
| 609 | |
David Vrabel | 7806cdb | 2007-08-10 13:29:46 +0100 | [diff] [blame] | 610 | /** |
| 611 | * sdio_f0_readb - read a single byte from SDIO function 0 |
| 612 | * @func: an SDIO function of the card |
| 613 | * @addr: address to read |
| 614 | * @err_ret: optional status value from transfer |
| 615 | * |
| 616 | * Reads a single byte from the address space of SDIO function 0. |
| 617 | * If there is a problem reading the address, 0xff is returned |
| 618 | * and @err_ret will contain the error code. |
| 619 | */ |
| 620 | unsigned char sdio_f0_readb(struct sdio_func *func, unsigned int addr, |
| 621 | int *err_ret) |
| 622 | { |
| 623 | int ret; |
| 624 | unsigned char val; |
| 625 | |
| 626 | BUG_ON(!func); |
| 627 | |
| 628 | if (err_ret) |
| 629 | *err_ret = 0; |
| 630 | |
| 631 | ret = mmc_io_rw_direct(func->card, 0, 0, addr, 0, &val); |
| 632 | if (ret) { |
| 633 | if (err_ret) |
| 634 | *err_ret = ret; |
| 635 | return 0xFF; |
| 636 | } |
| 637 | |
| 638 | return val; |
| 639 | } |
| 640 | EXPORT_SYMBOL_GPL(sdio_f0_readb); |
| 641 | |
| 642 | /** |
| 643 | * sdio_f0_writeb - write a single byte to SDIO function 0 |
| 644 | * @func: an SDIO function of the card |
| 645 | * @b: byte to write |
| 646 | * @addr: address to write to |
| 647 | * @err_ret: optional status value from transfer |
| 648 | * |
| 649 | * Writes a single byte to the address space of SDIO function 0. |
| 650 | * @err_ret will contain the status of the actual transfer. |
| 651 | * |
| 652 | * Only writes to the vendor specific CCCR registers (0xF0 - |
| 653 | * 0xFF) are permiited; @err_ret will be set to -EINVAL for * |
| 654 | * writes outside this range. |
| 655 | */ |
| 656 | void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr, |
| 657 | int *err_ret) |
| 658 | { |
| 659 | int ret; |
| 660 | |
| 661 | BUG_ON(!func); |
| 662 | |
Ohad Ben-Cohen | 7c979ec | 2009-09-22 16:45:18 -0700 | [diff] [blame] | 663 | if ((addr < 0xF0 || addr > 0xFF) && (!mmc_card_lenient_fn0(func->card))) { |
David Vrabel | 7806cdb | 2007-08-10 13:29:46 +0100 | [diff] [blame] | 664 | if (err_ret) |
| 665 | *err_ret = -EINVAL; |
| 666 | return; |
| 667 | } |
| 668 | |
| 669 | ret = mmc_io_rw_direct(func->card, 1, 0, addr, b, NULL); |
| 670 | if (err_ret) |
| 671 | *err_ret = ret; |
| 672 | } |
| 673 | EXPORT_SYMBOL_GPL(sdio_f0_writeb); |
Nicolas Pitre | da68c4e | 2010-03-05 13:43:31 -0800 | [diff] [blame] | 674 | |
| 675 | /** |
| 676 | * sdio_get_host_pm_caps - get host power management capabilities |
| 677 | * @func: SDIO function attached to host |
| 678 | * |
| 679 | * Returns a capability bitmask corresponding to power management |
| 680 | * features supported by the host controller that the card function |
| 681 | * might rely upon during a system suspend. The host doesn't need |
| 682 | * to be claimed, nor the function active, for this information to be |
| 683 | * obtained. |
| 684 | */ |
| 685 | mmc_pm_flag_t sdio_get_host_pm_caps(struct sdio_func *func) |
| 686 | { |
| 687 | BUG_ON(!func); |
| 688 | BUG_ON(!func->card); |
| 689 | |
| 690 | return func->card->host->pm_caps; |
| 691 | } |
| 692 | EXPORT_SYMBOL_GPL(sdio_get_host_pm_caps); |
| 693 | |
| 694 | /** |
| 695 | * sdio_set_host_pm_flags - set wanted host power management capabilities |
| 696 | * @func: SDIO function attached to host |
| 697 | * |
| 698 | * Set a capability bitmask corresponding to wanted host controller |
| 699 | * power management features for the upcoming suspend state. |
| 700 | * This must be called, if needed, each time the suspend method of |
| 701 | * the function driver is called, and must contain only bits that |
| 702 | * were returned by sdio_get_host_pm_caps(). |
| 703 | * The host doesn't need to be claimed, nor the function active, |
| 704 | * for this information to be set. |
| 705 | */ |
| 706 | int sdio_set_host_pm_flags(struct sdio_func *func, mmc_pm_flag_t flags) |
| 707 | { |
| 708 | struct mmc_host *host; |
| 709 | |
| 710 | BUG_ON(!func); |
| 711 | BUG_ON(!func->card); |
| 712 | |
| 713 | host = func->card->host; |
| 714 | |
| 715 | if (flags & ~host->pm_caps) |
| 716 | return -EINVAL; |
| 717 | |
| 718 | /* function suspend methods are serialized, hence no lock needed */ |
| 719 | host->pm_flags |= flags; |
| 720 | return 0; |
| 721 | } |
| 722 | EXPORT_SYMBOL_GPL(sdio_set_host_pm_flags); |