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 | { |
Shawn Lin | 923dff8 | 2016-08-26 08:49:56 +0800 | [diff] [blame] | 29 | if (WARN_ON(!func)) |
| 30 | return; |
Pierre Ossman | 46f555f | 2007-05-27 12:57:15 +0200 | [diff] [blame] | 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 | { |
Shawn Lin | 923dff8 | 2016-08-26 08:49:56 +0800 | [diff] [blame] | 45 | if (WARN_ON(!func)) |
| 46 | return; |
Pierre Ossman | 46f555f | 2007-05-27 12:57:15 +0200 | [diff] [blame] | 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 | |
Shawn Lin | 923dff8 | 2016-08-26 08:49:56 +0800 | [diff] [blame] | 65 | if (!func) |
| 66 | return -EINVAL; |
Pierre Ossman | fa64efa | 2007-05-27 14:22:37 +0200 | [diff] [blame] | 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 | |
Shawn Lin | 923dff8 | 2016-08-26 08:49:56 +0800 | [diff] [blame] | 115 | if (!func) |
| 116 | return -EINVAL; |
Pierre Ossman | fa64efa | 2007-05-27 14:22:37 +0200 | [diff] [blame] | 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 | { |
Kyoungil Kim | 968a64e | 2012-10-22 20:01:00 +0900 | [diff] [blame] | 191 | unsigned mval = func->card->host->max_blk_size; |
Bing Zhao | 3fb7fb4 | 2010-03-05 13:43:25 -0800 | [diff] [blame] | 192 | |
| 193 | if (mmc_blksz_for_byte_mode(func->card)) |
| 194 | mval = min(mval, func->cur_blksize); |
| 195 | else |
| 196 | mval = min(mval, func->max_blksize); |
| 197 | |
Stefan Nilsson XK | 052d81d | 2011-10-26 10:52:17 +0200 | [diff] [blame] | 198 | if (mmc_card_broken_byte_mode_512(func->card)) |
| 199 | return min(mval, 511u); |
| 200 | |
Tomas Winkler | ea90130 | 2008-07-10 03:01:56 +0300 | [diff] [blame] | 201 | return min(mval, 512u); /* maximum size for byte mode */ |
Pierre Ossman | eea0f58 | 2008-06-28 13:22:40 +0200 | [diff] [blame] | 202 | } |
| 203 | |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 204 | /** |
| 205 | * sdio_align_size - pads a transfer size to a more optimal value |
| 206 | * @func: SDIO function |
| 207 | * @sz: original transfer size |
| 208 | * |
| 209 | * Pads the original data size with a number of extra bytes in |
| 210 | * order to avoid controller bugs and/or performance hits |
| 211 | * (e.g. some controllers revert to PIO for certain sizes). |
| 212 | * |
| 213 | * If possible, it will also adjust the size so that it can be |
| 214 | * handled in just a single request. |
| 215 | * |
| 216 | * Returns the improved size, which might be unmodified. |
| 217 | */ |
| 218 | unsigned int sdio_align_size(struct sdio_func *func, unsigned int sz) |
| 219 | { |
| 220 | unsigned int orig_sz; |
| 221 | unsigned int blk_sz, byte_sz; |
| 222 | unsigned chunk_sz; |
| 223 | |
| 224 | orig_sz = sz; |
| 225 | |
| 226 | /* |
| 227 | * Do a first check with the controller, in case it |
| 228 | * wants to increase the size up to a point where it |
| 229 | * might need more than one block. |
| 230 | */ |
| 231 | sz = mmc_align_data_size(func->card, sz); |
| 232 | |
| 233 | /* |
| 234 | * If we can still do this with just a byte transfer, then |
| 235 | * we're done. |
| 236 | */ |
Pierre Ossman | eea0f58 | 2008-06-28 13:22:40 +0200 | [diff] [blame] | 237 | if (sz <= sdio_max_byte_size(func)) |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 238 | return sz; |
| 239 | |
| 240 | if (func->card->cccr.multi_block) { |
| 241 | /* |
| 242 | * Check if the transfer is already block aligned |
| 243 | */ |
| 244 | if ((sz % func->cur_blksize) == 0) |
| 245 | return sz; |
| 246 | |
| 247 | /* |
| 248 | * Realign it so that it can be done with one request, |
| 249 | * and recheck if the controller still likes it. |
| 250 | */ |
| 251 | blk_sz = ((sz + func->cur_blksize - 1) / |
| 252 | func->cur_blksize) * func->cur_blksize; |
| 253 | blk_sz = mmc_align_data_size(func->card, blk_sz); |
| 254 | |
| 255 | /* |
| 256 | * This value is only good if it is still just |
| 257 | * one request. |
| 258 | */ |
| 259 | if ((blk_sz % func->cur_blksize) == 0) |
| 260 | return blk_sz; |
| 261 | |
| 262 | /* |
| 263 | * We failed to do one request, but at least try to |
| 264 | * pad the remainder properly. |
| 265 | */ |
| 266 | byte_sz = mmc_align_data_size(func->card, |
| 267 | sz % func->cur_blksize); |
Pierre Ossman | eea0f58 | 2008-06-28 13:22:40 +0200 | [diff] [blame] | 268 | if (byte_sz <= sdio_max_byte_size(func)) { |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 269 | blk_sz = sz / func->cur_blksize; |
| 270 | return blk_sz * func->cur_blksize + byte_sz; |
| 271 | } |
| 272 | } else { |
| 273 | /* |
| 274 | * We need multiple requests, so first check that the |
| 275 | * controller can handle the chunk size; |
| 276 | */ |
| 277 | chunk_sz = mmc_align_data_size(func->card, |
Pierre Ossman | eea0f58 | 2008-06-28 13:22:40 +0200 | [diff] [blame] | 278 | sdio_max_byte_size(func)); |
| 279 | if (chunk_sz == sdio_max_byte_size(func)) { |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 280 | /* |
| 281 | * Fix up the size of the remainder (if any) |
| 282 | */ |
| 283 | byte_sz = orig_sz % chunk_sz; |
| 284 | if (byte_sz) { |
| 285 | byte_sz = mmc_align_data_size(func->card, |
| 286 | byte_sz); |
| 287 | } |
| 288 | |
| 289 | return (orig_sz / chunk_sz) * chunk_sz + byte_sz; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | /* |
| 294 | * The controller is simply incapable of transferring the size |
| 295 | * we want in decent manner, so just return the original size. |
| 296 | */ |
| 297 | return orig_sz; |
| 298 | } |
| 299 | EXPORT_SYMBOL_GPL(sdio_align_size); |
| 300 | |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 301 | /* Split an arbitrarily sized data transfer into several |
| 302 | * IO_RW_EXTENDED commands. */ |
| 303 | static int sdio_io_rw_ext_helper(struct sdio_func *func, int write, |
| 304 | unsigned addr, int incr_addr, u8 *buf, unsigned size) |
| 305 | { |
| 306 | unsigned remainder = size; |
| 307 | unsigned max_blocks; |
| 308 | int ret; |
| 309 | |
Shawn Lin | 923dff8 | 2016-08-26 08:49:56 +0800 | [diff] [blame] | 310 | if (!func || (func->num > 7)) |
| 311 | return -EINVAL; |
| 312 | |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 313 | /* Do the bulk of the transfer using block mode (if supported). */ |
Pierre Ossman | eea0f58 | 2008-06-28 13:22:40 +0200 | [diff] [blame] | 314 | if (func->card->cccr.multi_block && (size > sdio_max_byte_size(func))) { |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 315 | /* Blocks per command is limited by host count, host transfer |
Kyoungil Kim | 968a64e | 2012-10-22 20:01:00 +0900 | [diff] [blame] | 316 | * size and the maximum for IO_RW_EXTENDED of 511 blocks. */ |
| 317 | max_blocks = min(func->card->host->max_blk_count, 511u); |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 318 | |
Stefan Nilsson XK | 052d81d | 2011-10-26 10:52:17 +0200 | [diff] [blame] | 319 | while (remainder >= func->cur_blksize) { |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 320 | unsigned blocks; |
| 321 | |
| 322 | blocks = remainder / func->cur_blksize; |
| 323 | if (blocks > max_blocks) |
| 324 | blocks = max_blocks; |
| 325 | size = blocks * func->cur_blksize; |
| 326 | |
| 327 | ret = mmc_io_rw_extended(func->card, write, |
| 328 | func->num, addr, incr_addr, buf, |
| 329 | blocks, func->cur_blksize); |
| 330 | if (ret) |
| 331 | return ret; |
| 332 | |
| 333 | remainder -= size; |
| 334 | buf += size; |
| 335 | if (incr_addr) |
| 336 | addr += size; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | /* Write the remainder using byte mode. */ |
| 341 | while (remainder > 0) { |
Pierre Ossman | eea0f58 | 2008-06-28 13:22:40 +0200 | [diff] [blame] | 342 | size = min(remainder, sdio_max_byte_size(func)); |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 343 | |
Stefan Nilsson XK | 052d81d | 2011-10-26 10:52:17 +0200 | [diff] [blame] | 344 | /* Indicate byte mode by setting "blocks" = 0 */ |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 345 | ret = mmc_io_rw_extended(func->card, write, func->num, addr, |
Stefan Nilsson XK | 052d81d | 2011-10-26 10:52:17 +0200 | [diff] [blame] | 346 | incr_addr, buf, 0, size); |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 347 | if (ret) |
| 348 | return ret; |
| 349 | |
| 350 | remainder -= size; |
| 351 | buf += size; |
| 352 | if (incr_addr) |
| 353 | addr += size; |
| 354 | } |
| 355 | return 0; |
| 356 | } |
| 357 | |
David Vrabel | 9a08f82 | 2007-08-08 14:23:48 +0100 | [diff] [blame] | 358 | /** |
Pierre Ossman | 46f555f | 2007-05-27 12:57:15 +0200 | [diff] [blame] | 359 | * sdio_readb - read a single byte from a SDIO function |
| 360 | * @func: SDIO function to access |
| 361 | * @addr: address to read |
| 362 | * @err_ret: optional status value from transfer |
| 363 | * |
| 364 | * Reads a single byte from the address space of a given SDIO |
| 365 | * function. If there is a problem reading the address, 0xff |
| 366 | * is returned and @err_ret will contain the error code. |
| 367 | */ |
Tomas Winkler | 6d37333 | 2008-06-30 10:50:24 +0300 | [diff] [blame] | 368 | 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] | 369 | { |
| 370 | int ret; |
Tomas Winkler | 6d37333 | 2008-06-30 10:50:24 +0300 | [diff] [blame] | 371 | u8 val; |
Pierre Ossman | 46f555f | 2007-05-27 12:57:15 +0200 | [diff] [blame] | 372 | |
Shawn Lin | 923dff8 | 2016-08-26 08:49:56 +0800 | [diff] [blame] | 373 | if (!func) { |
| 374 | *err_ret = -EINVAL; |
| 375 | return 0xFF; |
| 376 | } |
Pierre Ossman | 46f555f | 2007-05-27 12:57:15 +0200 | [diff] [blame] | 377 | |
| 378 | if (err_ret) |
| 379 | *err_ret = 0; |
| 380 | |
| 381 | ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val); |
| 382 | if (ret) { |
| 383 | if (err_ret) |
| 384 | *err_ret = ret; |
| 385 | return 0xFF; |
| 386 | } |
| 387 | |
| 388 | return val; |
| 389 | } |
| 390 | EXPORT_SYMBOL_GPL(sdio_readb); |
| 391 | |
| 392 | /** |
Dmitry Shmidt | 56d0106 | 2008-11-11 11:22:38 -0800 | [diff] [blame] | 393 | * sdio_readb_ext - read a single byte from a SDIO function |
| 394 | * @func: SDIO function to access |
| 395 | * @addr: address to read |
| 396 | * @err_ret: optional status value from transfer |
| 397 | * @in: value to add to argument |
| 398 | * |
| 399 | * Reads a single byte from the address space of a given SDIO |
| 400 | * function. If there is a problem reading the address, 0xff |
| 401 | * is returned and @err_ret will contain the error code. |
| 402 | */ |
| 403 | unsigned char sdio_readb_ext(struct sdio_func *func, unsigned int addr, |
| 404 | int *err_ret, unsigned in) |
| 405 | { |
| 406 | int ret; |
| 407 | unsigned char val; |
| 408 | |
| 409 | BUG_ON(!func); |
| 410 | |
| 411 | if (err_ret) |
| 412 | *err_ret = 0; |
| 413 | |
| 414 | ret = mmc_io_rw_direct(func->card, 0, func->num, addr, (u8)in, &val); |
| 415 | if (ret) { |
| 416 | if (err_ret) |
| 417 | *err_ret = ret; |
| 418 | return 0xFF; |
| 419 | } |
| 420 | |
| 421 | return val; |
| 422 | } |
| 423 | EXPORT_SYMBOL_GPL(sdio_readb_ext); |
| 424 | |
| 425 | /** |
Pierre Ossman | 46f555f | 2007-05-27 12:57:15 +0200 | [diff] [blame] | 426 | * sdio_writeb - write a single byte to a SDIO function |
| 427 | * @func: SDIO function to access |
| 428 | * @b: byte to write |
| 429 | * @addr: address to write to |
| 430 | * @err_ret: optional status value from transfer |
| 431 | * |
| 432 | * Writes a single byte to the address space of a given SDIO |
| 433 | * function. @err_ret will contain the status of the actual |
| 434 | * transfer. |
| 435 | */ |
Tomas Winkler | 6d37333 | 2008-06-30 10:50:24 +0300 | [diff] [blame] | 436 | 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] | 437 | { |
| 438 | int ret; |
| 439 | |
Shawn Lin | 923dff8 | 2016-08-26 08:49:56 +0800 | [diff] [blame] | 440 | if (!func) { |
| 441 | *err_ret = -EINVAL; |
| 442 | return; |
| 443 | } |
Pierre Ossman | 46f555f | 2007-05-27 12:57:15 +0200 | [diff] [blame] | 444 | |
| 445 | ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL); |
| 446 | if (err_ret) |
| 447 | *err_ret = ret; |
| 448 | } |
| 449 | EXPORT_SYMBOL_GPL(sdio_writeb); |
| 450 | |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 451 | /** |
Grazvydas Ignotas | 6c1f716 | 2010-05-26 14:42:09 -0700 | [diff] [blame] | 452 | * sdio_writeb_readb - write and read a byte from SDIO function |
| 453 | * @func: SDIO function to access |
| 454 | * @write_byte: byte to write |
| 455 | * @addr: address to write to |
| 456 | * @err_ret: optional status value from transfer |
| 457 | * |
| 458 | * Performs a RAW (Read after Write) operation as defined by SDIO spec - |
| 459 | * single byte is written to address space of a given SDIO function and |
| 460 | * response is read back from the same address, both using single request. |
| 461 | * If there is a problem with the operation, 0xff is returned and |
| 462 | * @err_ret will contain the error code. |
| 463 | */ |
| 464 | u8 sdio_writeb_readb(struct sdio_func *func, u8 write_byte, |
| 465 | unsigned int addr, int *err_ret) |
| 466 | { |
| 467 | int ret; |
| 468 | u8 val; |
| 469 | |
| 470 | ret = mmc_io_rw_direct(func->card, 1, func->num, addr, |
| 471 | write_byte, &val); |
| 472 | if (err_ret) |
| 473 | *err_ret = ret; |
| 474 | if (ret) |
| 475 | val = 0xff; |
| 476 | |
| 477 | return val; |
| 478 | } |
| 479 | EXPORT_SYMBOL_GPL(sdio_writeb_readb); |
| 480 | |
| 481 | /** |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 482 | * sdio_memcpy_fromio - read a chunk of memory from a SDIO function |
| 483 | * @func: SDIO function to access |
| 484 | * @dst: buffer to store the data |
| 485 | * @addr: address to begin reading from |
| 486 | * @count: number of bytes to read |
| 487 | * |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 488 | * Reads from the address space of a given SDIO function. Return |
| 489 | * value indicates if the transfer succeeded or not. |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 490 | */ |
| 491 | int sdio_memcpy_fromio(struct sdio_func *func, void *dst, |
| 492 | unsigned int addr, int count) |
| 493 | { |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 494 | return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count); |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 495 | } |
| 496 | EXPORT_SYMBOL_GPL(sdio_memcpy_fromio); |
| 497 | |
| 498 | /** |
| 499 | * sdio_memcpy_toio - write a chunk of memory to a SDIO function |
| 500 | * @func: SDIO function to access |
| 501 | * @addr: address to start writing to |
| 502 | * @src: buffer that contains the data to write |
| 503 | * @count: number of bytes to write |
| 504 | * |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 505 | * Writes to the address space of a given SDIO function. Return |
| 506 | * value indicates if the transfer succeeded or not. |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 507 | */ |
| 508 | int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr, |
| 509 | void *src, int count) |
| 510 | { |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 511 | return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count); |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 512 | } |
| 513 | EXPORT_SYMBOL_GPL(sdio_memcpy_toio); |
| 514 | |
| 515 | /** |
| 516 | * sdio_readsb - read from a FIFO on a SDIO function |
| 517 | * @func: SDIO function to access |
| 518 | * @dst: buffer to store the data |
| 519 | * @addr: address of (single byte) FIFO |
| 520 | * @count: number of bytes to read |
| 521 | * |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 522 | * Reads from the specified FIFO of a given SDIO function. Return |
| 523 | * value indicates if the transfer succeeded or not. |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 524 | */ |
| 525 | int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr, |
| 526 | int count) |
| 527 | { |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 528 | return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count); |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 529 | } |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 530 | EXPORT_SYMBOL_GPL(sdio_readsb); |
| 531 | |
| 532 | /** |
| 533 | * sdio_writesb - write to a FIFO of a SDIO function |
| 534 | * @func: SDIO function to access |
| 535 | * @addr: address of (single byte) FIFO |
| 536 | * @src: buffer that contains the data to write |
| 537 | * @count: number of bytes to write |
| 538 | * |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 539 | * Writes to the specified FIFO of a given SDIO function. Return |
| 540 | * value indicates if the transfer succeeded or not. |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 541 | */ |
| 542 | int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src, |
| 543 | int count) |
| 544 | { |
David Vrabel | eb65946 | 2007-08-08 14:24:21 +0100 | [diff] [blame] | 545 | return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count); |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 546 | } |
| 547 | EXPORT_SYMBOL_GPL(sdio_writesb); |
| 548 | |
| 549 | /** |
| 550 | * sdio_readw - read a 16 bit integer from a SDIO function |
| 551 | * @func: SDIO function to access |
| 552 | * @addr: address to read |
| 553 | * @err_ret: optional status value from transfer |
| 554 | * |
| 555 | * Reads a 16 bit integer from the address space of a given SDIO |
| 556 | * function. If there is a problem reading the address, 0xffff |
| 557 | * is returned and @err_ret will contain the error code. |
| 558 | */ |
Tomas Winkler | 6d37333 | 2008-06-30 10:50:24 +0300 | [diff] [blame] | 559 | 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] | 560 | { |
| 561 | int ret; |
| 562 | |
| 563 | if (err_ret) |
| 564 | *err_ret = 0; |
| 565 | |
| 566 | ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2); |
| 567 | if (ret) { |
| 568 | if (err_ret) |
| 569 | *err_ret = ret; |
| 570 | return 0xFFFF; |
| 571 | } |
| 572 | |
Tomas Winkler | 6d37333 | 2008-06-30 10:50:24 +0300 | [diff] [blame] | 573 | return le16_to_cpup((__le16 *)func->tmpbuf); |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 574 | } |
| 575 | EXPORT_SYMBOL_GPL(sdio_readw); |
| 576 | |
| 577 | /** |
| 578 | * sdio_writew - write a 16 bit integer to a SDIO function |
| 579 | * @func: SDIO function to access |
| 580 | * @b: integer to write |
| 581 | * @addr: address to write to |
| 582 | * @err_ret: optional status value from transfer |
| 583 | * |
| 584 | * Writes a 16 bit integer to the address space of a given SDIO |
| 585 | * function. @err_ret will contain the status of the actual |
| 586 | * transfer. |
| 587 | */ |
Tomas Winkler | 6d37333 | 2008-06-30 10:50:24 +0300 | [diff] [blame] | 588 | 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] | 589 | { |
| 590 | int ret; |
| 591 | |
Tomas Winkler | 6d37333 | 2008-06-30 10:50:24 +0300 | [diff] [blame] | 592 | *(__le16 *)func->tmpbuf = cpu_to_le16(b); |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 593 | |
| 594 | ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2); |
| 595 | if (err_ret) |
| 596 | *err_ret = ret; |
| 597 | } |
| 598 | EXPORT_SYMBOL_GPL(sdio_writew); |
| 599 | |
| 600 | /** |
| 601 | * sdio_readl - read a 32 bit integer from a SDIO function |
| 602 | * @func: SDIO function to access |
| 603 | * @addr: address to read |
| 604 | * @err_ret: optional status value from transfer |
| 605 | * |
| 606 | * Reads a 32 bit integer from the address space of a given SDIO |
| 607 | * function. If there is a problem reading the address, |
| 608 | * 0xffffffff is returned and @err_ret will contain the error |
| 609 | * code. |
| 610 | */ |
Tomas Winkler | 6d37333 | 2008-06-30 10:50:24 +0300 | [diff] [blame] | 611 | 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] | 612 | { |
| 613 | int ret; |
| 614 | |
| 615 | if (err_ret) |
| 616 | *err_ret = 0; |
| 617 | |
| 618 | ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4); |
| 619 | if (ret) { |
| 620 | if (err_ret) |
| 621 | *err_ret = ret; |
| 622 | return 0xFFFFFFFF; |
| 623 | } |
| 624 | |
Tomas Winkler | 6d37333 | 2008-06-30 10:50:24 +0300 | [diff] [blame] | 625 | return le32_to_cpup((__le32 *)func->tmpbuf); |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 626 | } |
| 627 | EXPORT_SYMBOL_GPL(sdio_readl); |
| 628 | |
| 629 | /** |
| 630 | * sdio_writel - write a 32 bit integer to a SDIO function |
| 631 | * @func: SDIO function to access |
| 632 | * @b: integer to write |
| 633 | * @addr: address to write to |
| 634 | * @err_ret: optional status value from transfer |
| 635 | * |
| 636 | * Writes a 32 bit integer to the address space of a given SDIO |
| 637 | * function. @err_ret will contain the status of the actual |
| 638 | * transfer. |
| 639 | */ |
Tomas Winkler | 6d37333 | 2008-06-30 10:50:24 +0300 | [diff] [blame] | 640 | 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] | 641 | { |
| 642 | int ret; |
| 643 | |
Tomas Winkler | 6d37333 | 2008-06-30 10:50:24 +0300 | [diff] [blame] | 644 | *(__le32 *)func->tmpbuf = cpu_to_le32(b); |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 645 | |
| 646 | ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4); |
| 647 | if (err_ret) |
| 648 | *err_ret = ret; |
| 649 | } |
| 650 | EXPORT_SYMBOL_GPL(sdio_writel); |
| 651 | |
David Vrabel | 7806cdb | 2007-08-10 13:29:46 +0100 | [diff] [blame] | 652 | /** |
| 653 | * sdio_f0_readb - read a single byte from SDIO function 0 |
| 654 | * @func: an SDIO function of the card |
| 655 | * @addr: address to read |
| 656 | * @err_ret: optional status value from transfer |
| 657 | * |
| 658 | * Reads a single byte from the address space of SDIO function 0. |
| 659 | * If there is a problem reading the address, 0xff is returned |
| 660 | * and @err_ret will contain the error code. |
| 661 | */ |
| 662 | unsigned char sdio_f0_readb(struct sdio_func *func, unsigned int addr, |
| 663 | int *err_ret) |
| 664 | { |
| 665 | int ret; |
| 666 | unsigned char val; |
| 667 | |
Shawn Lin | 923dff8 | 2016-08-26 08:49:56 +0800 | [diff] [blame] | 668 | if (!func) { |
| 669 | *err_ret = -EINVAL; |
| 670 | return 0xFF; |
| 671 | } |
David Vrabel | 7806cdb | 2007-08-10 13:29:46 +0100 | [diff] [blame] | 672 | |
| 673 | if (err_ret) |
| 674 | *err_ret = 0; |
| 675 | |
| 676 | ret = mmc_io_rw_direct(func->card, 0, 0, addr, 0, &val); |
| 677 | if (ret) { |
| 678 | if (err_ret) |
| 679 | *err_ret = ret; |
| 680 | return 0xFF; |
| 681 | } |
| 682 | |
| 683 | return val; |
| 684 | } |
| 685 | EXPORT_SYMBOL_GPL(sdio_f0_readb); |
| 686 | |
| 687 | /** |
| 688 | * sdio_f0_writeb - write a single byte to SDIO function 0 |
| 689 | * @func: an SDIO function of the card |
| 690 | * @b: byte to write |
| 691 | * @addr: address to write to |
| 692 | * @err_ret: optional status value from transfer |
| 693 | * |
| 694 | * Writes a single byte to the address space of SDIO function 0. |
| 695 | * @err_ret will contain the status of the actual transfer. |
| 696 | * |
| 697 | * Only writes to the vendor specific CCCR registers (0xF0 - |
| 698 | * 0xFF) are permiited; @err_ret will be set to -EINVAL for * |
| 699 | * writes outside this range. |
| 700 | */ |
| 701 | void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr, |
| 702 | int *err_ret) |
| 703 | { |
| 704 | int ret; |
| 705 | |
Shawn Lin | 923dff8 | 2016-08-26 08:49:56 +0800 | [diff] [blame] | 706 | if (!func) { |
| 707 | *err_ret = -EINVAL; |
| 708 | return; |
| 709 | } |
David Vrabel | 7806cdb | 2007-08-10 13:29:46 +0100 | [diff] [blame] | 710 | |
Ohad Ben-Cohen | 7c979ec | 2009-09-22 16:45:18 -0700 | [diff] [blame] | 711 | if ((addr < 0xF0 || addr > 0xFF) && (!mmc_card_lenient_fn0(func->card))) { |
David Vrabel | 7806cdb | 2007-08-10 13:29:46 +0100 | [diff] [blame] | 712 | if (err_ret) |
| 713 | *err_ret = -EINVAL; |
| 714 | return; |
| 715 | } |
| 716 | |
| 717 | ret = mmc_io_rw_direct(func->card, 1, 0, addr, b, NULL); |
| 718 | if (err_ret) |
| 719 | *err_ret = ret; |
| 720 | } |
| 721 | EXPORT_SYMBOL_GPL(sdio_f0_writeb); |
Nicolas Pitre | da68c4e | 2010-03-05 13:43:31 -0800 | [diff] [blame] | 722 | |
| 723 | /** |
| 724 | * sdio_get_host_pm_caps - get host power management capabilities |
| 725 | * @func: SDIO function attached to host |
| 726 | * |
| 727 | * Returns a capability bitmask corresponding to power management |
| 728 | * features supported by the host controller that the card function |
| 729 | * might rely upon during a system suspend. The host doesn't need |
| 730 | * to be claimed, nor the function active, for this information to be |
| 731 | * obtained. |
| 732 | */ |
| 733 | mmc_pm_flag_t sdio_get_host_pm_caps(struct sdio_func *func) |
| 734 | { |
Shawn Lin | 923dff8 | 2016-08-26 08:49:56 +0800 | [diff] [blame] | 735 | if (!func) |
| 736 | return 0; |
Nicolas Pitre | da68c4e | 2010-03-05 13:43:31 -0800 | [diff] [blame] | 737 | |
| 738 | return func->card->host->pm_caps; |
| 739 | } |
| 740 | EXPORT_SYMBOL_GPL(sdio_get_host_pm_caps); |
| 741 | |
| 742 | /** |
| 743 | * sdio_set_host_pm_flags - set wanted host power management capabilities |
| 744 | * @func: SDIO function attached to host |
| 745 | * |
| 746 | * Set a capability bitmask corresponding to wanted host controller |
| 747 | * power management features for the upcoming suspend state. |
| 748 | * This must be called, if needed, each time the suspend method of |
| 749 | * the function driver is called, and must contain only bits that |
| 750 | * were returned by sdio_get_host_pm_caps(). |
| 751 | * The host doesn't need to be claimed, nor the function active, |
| 752 | * for this information to be set. |
| 753 | */ |
| 754 | int sdio_set_host_pm_flags(struct sdio_func *func, mmc_pm_flag_t flags) |
| 755 | { |
| 756 | struct mmc_host *host; |
| 757 | |
Shawn Lin | 923dff8 | 2016-08-26 08:49:56 +0800 | [diff] [blame] | 758 | if (!func) |
| 759 | return -EINVAL; |
Nicolas Pitre | da68c4e | 2010-03-05 13:43:31 -0800 | [diff] [blame] | 760 | |
| 761 | host = func->card->host; |
| 762 | |
| 763 | if (flags & ~host->pm_caps) |
| 764 | return -EINVAL; |
| 765 | |
| 766 | /* function suspend methods are serialized, hence no lock needed */ |
| 767 | host->pm_flags |= flags; |
| 768 | return 0; |
| 769 | } |
| 770 | EXPORT_SYMBOL_GPL(sdio_set_host_pm_flags); |