Pierre Ossman | 46f555f | 2007-05-27 12:57:15 +0200 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/mmc/core/sdio_io.c |
| 3 | * |
| 4 | * Copyright 2007 Pierre Ossman |
| 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 | |
| 12 | #include <linux/mmc/host.h> |
| 13 | #include <linux/mmc/card.h> |
Pierre Ossman | fa64efa | 2007-05-27 14:22:37 +0200 | [diff] [blame] | 14 | #include <linux/mmc/sdio.h> |
Pierre Ossman | 46f555f | 2007-05-27 12:57:15 +0200 | [diff] [blame] | 15 | #include <linux/mmc/sdio_func.h> |
| 16 | |
| 17 | #include "sdio_ops.h" |
| 18 | |
| 19 | /** |
| 20 | * sdio_claim_host - exclusively claim a bus for a certain SDIO function |
| 21 | * @func: SDIO function that will be accessed |
| 22 | * |
| 23 | * Claim a bus for a set of operations. The SDIO function given |
| 24 | * is used to figure out which bus is relevant. |
| 25 | */ |
| 26 | void sdio_claim_host(struct sdio_func *func) |
| 27 | { |
| 28 | BUG_ON(!func); |
| 29 | BUG_ON(!func->card); |
| 30 | |
| 31 | mmc_claim_host(func->card->host); |
| 32 | } |
| 33 | EXPORT_SYMBOL_GPL(sdio_claim_host); |
| 34 | |
| 35 | /** |
| 36 | * sdio_release_host - release a bus for a certain SDIO function |
| 37 | * @func: SDIO function that was accessed |
| 38 | * |
| 39 | * Release a bus, allowing others to claim the bus for their |
| 40 | * operations. |
| 41 | */ |
| 42 | void sdio_release_host(struct sdio_func *func) |
| 43 | { |
| 44 | BUG_ON(!func); |
| 45 | BUG_ON(!func->card); |
| 46 | |
| 47 | mmc_release_host(func->card->host); |
| 48 | } |
| 49 | EXPORT_SYMBOL_GPL(sdio_release_host); |
| 50 | |
| 51 | /** |
Pierre Ossman | fa64efa | 2007-05-27 14:22:37 +0200 | [diff] [blame] | 52 | * sdio_enable_func - enables a SDIO function for usage |
| 53 | * @func: SDIO function to enable |
| 54 | * |
| 55 | * Powers up and activates a SDIO function so that register |
| 56 | * access is possible. |
| 57 | */ |
| 58 | int sdio_enable_func(struct sdio_func *func) |
| 59 | { |
| 60 | int ret; |
| 61 | unsigned char reg; |
| 62 | unsigned long timeout; |
| 63 | |
| 64 | BUG_ON(!func); |
| 65 | BUG_ON(!func->card); |
| 66 | |
| 67 | pr_debug("SDIO: Enabling device %s...\n", sdio_func_id(func)); |
| 68 | |
| 69 | ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, ®); |
| 70 | if (ret) |
| 71 | goto err; |
| 72 | |
| 73 | reg |= 1 << func->num; |
| 74 | |
| 75 | ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL); |
| 76 | if (ret) |
| 77 | goto err; |
| 78 | |
| 79 | /* |
| 80 | * FIXME: This should timeout based on information in the CIS, |
| 81 | * but we don't have card to parse that yet. |
| 82 | */ |
| 83 | timeout = jiffies + HZ; |
| 84 | |
| 85 | while (1) { |
| 86 | ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IORx, 0, ®); |
| 87 | if (ret) |
| 88 | goto err; |
| 89 | if (reg & (1 << func->num)) |
| 90 | break; |
| 91 | ret = -ETIME; |
| 92 | if (time_after(jiffies, timeout)) |
| 93 | goto err; |
| 94 | } |
| 95 | |
| 96 | pr_debug("SDIO: Enabled device %s\n", sdio_func_id(func)); |
| 97 | |
| 98 | return 0; |
| 99 | |
| 100 | err: |
| 101 | pr_debug("SDIO: Failed to enable device %s\n", sdio_func_id(func)); |
| 102 | return ret; |
| 103 | } |
| 104 | EXPORT_SYMBOL_GPL(sdio_enable_func); |
| 105 | |
| 106 | /** |
| 107 | * sdio_disable_func - disable a SDIO function |
| 108 | * @func: SDIO function to disable |
| 109 | * |
| 110 | * Powers down and deactivates a SDIO function. Register access |
| 111 | * to this function will fail until the function is reenabled. |
| 112 | */ |
| 113 | int sdio_disable_func(struct sdio_func *func) |
| 114 | { |
| 115 | int ret; |
| 116 | unsigned char reg; |
| 117 | |
| 118 | BUG_ON(!func); |
| 119 | BUG_ON(!func->card); |
| 120 | |
| 121 | pr_debug("SDIO: Disabling device %s...\n", sdio_func_id(func)); |
| 122 | |
| 123 | ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, ®); |
| 124 | if (ret) |
| 125 | goto err; |
| 126 | |
| 127 | reg &= ~(1 << func->num); |
| 128 | |
| 129 | ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL); |
| 130 | if (ret) |
| 131 | goto err; |
| 132 | |
| 133 | pr_debug("SDIO: Disabled device %s\n", sdio_func_id(func)); |
| 134 | |
| 135 | return 0; |
| 136 | |
| 137 | err: |
| 138 | pr_debug("SDIO: Failed to disable device %s\n", sdio_func_id(func)); |
| 139 | return -EIO; |
| 140 | } |
| 141 | EXPORT_SYMBOL_GPL(sdio_disable_func); |
| 142 | |
| 143 | /** |
David Vrabel | 9a08f82 | 2007-08-08 14:23:48 +0100 | [diff] [blame^] | 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. |
| 147 | * |
| 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. |
| 151 | * |
| 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. |
| 156 | * |
| 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. |
| 160 | * |
| 161 | */ |
| 162 | int sdio_set_block_size(struct sdio_func *func, unsigned blksz) |
| 163 | { |
| 164 | int ret; |
| 165 | |
| 166 | if (blksz > func->card->host->max_blk_size) |
| 167 | return -EINVAL; |
| 168 | |
| 169 | if (blksz == 0) { |
| 170 | blksz = min(min( |
| 171 | func->max_blksize, |
| 172 | func->card->host->max_blk_size), |
| 173 | 512u); |
| 174 | } |
| 175 | |
| 176 | ret = mmc_io_rw_direct(func->card, 1, 0, |
| 177 | SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE, |
| 178 | blksz & 0xff, NULL); |
| 179 | if (ret) |
| 180 | return ret; |
| 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); |
| 184 | if (ret) |
| 185 | return ret; |
| 186 | func->cur_blksize = blksz; |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | EXPORT_SYMBOL_GPL(sdio_set_block_size); |
| 191 | |
| 192 | /** |
Pierre Ossman | 46f555f | 2007-05-27 12:57:15 +0200 | [diff] [blame] | 193 | * sdio_readb - read a single byte from a SDIO function |
| 194 | * @func: SDIO function to access |
| 195 | * @addr: address to read |
| 196 | * @err_ret: optional status value from transfer |
| 197 | * |
| 198 | * Reads a single byte from the address space of a given SDIO |
| 199 | * function. If there is a problem reading the address, 0xff |
| 200 | * is returned and @err_ret will contain the error code. |
| 201 | */ |
| 202 | unsigned char sdio_readb(struct sdio_func *func, unsigned int addr, |
| 203 | int *err_ret) |
| 204 | { |
| 205 | int ret; |
| 206 | unsigned char val; |
| 207 | |
| 208 | BUG_ON(!func); |
| 209 | |
| 210 | if (err_ret) |
| 211 | *err_ret = 0; |
| 212 | |
| 213 | ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val); |
| 214 | if (ret) { |
| 215 | if (err_ret) |
| 216 | *err_ret = ret; |
| 217 | return 0xFF; |
| 218 | } |
| 219 | |
| 220 | return val; |
| 221 | } |
| 222 | EXPORT_SYMBOL_GPL(sdio_readb); |
| 223 | |
| 224 | /** |
| 225 | * sdio_writeb - write a single byte to a SDIO function |
| 226 | * @func: SDIO function to access |
| 227 | * @b: byte to write |
| 228 | * @addr: address to write to |
| 229 | * @err_ret: optional status value from transfer |
| 230 | * |
| 231 | * Writes a single byte to the address space of a given SDIO |
| 232 | * function. @err_ret will contain the status of the actual |
| 233 | * transfer. |
| 234 | */ |
| 235 | void sdio_writeb(struct sdio_func *func, unsigned char b, unsigned int addr, |
| 236 | int *err_ret) |
| 237 | { |
| 238 | int ret; |
| 239 | |
| 240 | BUG_ON(!func); |
| 241 | |
| 242 | ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL); |
| 243 | if (err_ret) |
| 244 | *err_ret = ret; |
| 245 | } |
| 246 | EXPORT_SYMBOL_GPL(sdio_writeb); |
| 247 | |
Pierre Ossman | 112c9db | 2007-07-06 13:35:01 +0200 | [diff] [blame] | 248 | /** |
| 249 | * sdio_memcpy_fromio - read a chunk of memory from a SDIO function |
| 250 | * @func: SDIO function to access |
| 251 | * @dst: buffer to store the data |
| 252 | * @addr: address to begin reading from |
| 253 | * @count: number of bytes to read |
| 254 | * |
| 255 | * Reads up to 512 bytes from the address space of a given SDIO |
| 256 | * function. Return value indicates if the transfer succeeded or |
| 257 | * not. |
| 258 | */ |
| 259 | int sdio_memcpy_fromio(struct sdio_func *func, void *dst, |
| 260 | unsigned int addr, int count) |
| 261 | { |
| 262 | return mmc_io_rw_extended(func->card, 0, func->num, addr, 0, dst, |
| 263 | count); |
| 264 | } |
| 265 | EXPORT_SYMBOL_GPL(sdio_memcpy_fromio); |
| 266 | |
| 267 | /** |
| 268 | * sdio_memcpy_toio - write a chunk of memory to a SDIO function |
| 269 | * @func: SDIO function to access |
| 270 | * @addr: address to start writing to |
| 271 | * @src: buffer that contains the data to write |
| 272 | * @count: number of bytes to write |
| 273 | * |
| 274 | * Writes up to 512 bytes to the address space of a given SDIO |
| 275 | * function. Return value indicates if the transfer succeeded or |
| 276 | * not. |
| 277 | */ |
| 278 | int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr, |
| 279 | void *src, int count) |
| 280 | { |
| 281 | return mmc_io_rw_extended(func->card, 1, func->num, addr, 0, src, |
| 282 | count); |
| 283 | } |
| 284 | EXPORT_SYMBOL_GPL(sdio_memcpy_toio); |
| 285 | |
| 286 | /** |
| 287 | * sdio_readsb - read from a FIFO on a SDIO function |
| 288 | * @func: SDIO function to access |
| 289 | * @dst: buffer to store the data |
| 290 | * @addr: address of (single byte) FIFO |
| 291 | * @count: number of bytes to read |
| 292 | * |
| 293 | * Reads up to 512 bytes from the specified FIFO of a given SDIO |
| 294 | * function. Return value indicates if the transfer succeeded or |
| 295 | * not. |
| 296 | */ |
| 297 | int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr, |
| 298 | int count) |
| 299 | { |
| 300 | return mmc_io_rw_extended(func->card, 0, func->num, addr, 1, dst, |
| 301 | count); |
| 302 | } |
| 303 | |
| 304 | EXPORT_SYMBOL_GPL(sdio_readsb); |
| 305 | |
| 306 | /** |
| 307 | * sdio_writesb - write to a FIFO of a SDIO function |
| 308 | * @func: SDIO function to access |
| 309 | * @addr: address of (single byte) FIFO |
| 310 | * @src: buffer that contains the data to write |
| 311 | * @count: number of bytes to write |
| 312 | * |
| 313 | * Writes up to 512 bytes to the specified FIFO of a given SDIO |
| 314 | * function. Return value indicates if the transfer succeeded or |
| 315 | * not. |
| 316 | */ |
| 317 | int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src, |
| 318 | int count) |
| 319 | { |
| 320 | return mmc_io_rw_extended(func->card, 1, func->num, addr, 1, src, |
| 321 | count); |
| 322 | } |
| 323 | EXPORT_SYMBOL_GPL(sdio_writesb); |
| 324 | |
| 325 | /** |
| 326 | * sdio_readw - read a 16 bit integer from a SDIO function |
| 327 | * @func: SDIO function to access |
| 328 | * @addr: address to read |
| 329 | * @err_ret: optional status value from transfer |
| 330 | * |
| 331 | * Reads a 16 bit integer from the address space of a given SDIO |
| 332 | * function. If there is a problem reading the address, 0xffff |
| 333 | * is returned and @err_ret will contain the error code. |
| 334 | */ |
| 335 | unsigned short sdio_readw(struct sdio_func *func, unsigned int addr, |
| 336 | int *err_ret) |
| 337 | { |
| 338 | int ret; |
| 339 | |
| 340 | if (err_ret) |
| 341 | *err_ret = 0; |
| 342 | |
| 343 | ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2); |
| 344 | if (ret) { |
| 345 | if (err_ret) |
| 346 | *err_ret = ret; |
| 347 | return 0xFFFF; |
| 348 | } |
| 349 | |
| 350 | return le16_to_cpu(*(u16*)func->tmpbuf); |
| 351 | } |
| 352 | EXPORT_SYMBOL_GPL(sdio_readw); |
| 353 | |
| 354 | /** |
| 355 | * sdio_writew - write a 16 bit integer to a SDIO function |
| 356 | * @func: SDIO function to access |
| 357 | * @b: integer to write |
| 358 | * @addr: address to write to |
| 359 | * @err_ret: optional status value from transfer |
| 360 | * |
| 361 | * Writes a 16 bit integer to the address space of a given SDIO |
| 362 | * function. @err_ret will contain the status of the actual |
| 363 | * transfer. |
| 364 | */ |
| 365 | void sdio_writew(struct sdio_func *func, unsigned short b, unsigned int addr, |
| 366 | int *err_ret) |
| 367 | { |
| 368 | int ret; |
| 369 | |
| 370 | *(u16*)func->tmpbuf = cpu_to_le16(b); |
| 371 | |
| 372 | ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2); |
| 373 | if (err_ret) |
| 374 | *err_ret = ret; |
| 375 | } |
| 376 | EXPORT_SYMBOL_GPL(sdio_writew); |
| 377 | |
| 378 | /** |
| 379 | * sdio_readl - read a 32 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 |
| 383 | * |
| 384 | * Reads a 32 bit integer from the address space of a given SDIO |
| 385 | * function. If there is a problem reading the address, |
| 386 | * 0xffffffff is returned and @err_ret will contain the error |
| 387 | * code. |
| 388 | */ |
| 389 | unsigned long sdio_readl(struct sdio_func *func, unsigned int addr, |
| 390 | int *err_ret) |
| 391 | { |
| 392 | int ret; |
| 393 | |
| 394 | if (err_ret) |
| 395 | *err_ret = 0; |
| 396 | |
| 397 | ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4); |
| 398 | if (ret) { |
| 399 | if (err_ret) |
| 400 | *err_ret = ret; |
| 401 | return 0xFFFFFFFF; |
| 402 | } |
| 403 | |
| 404 | return le32_to_cpu(*(u32*)func->tmpbuf); |
| 405 | } |
| 406 | EXPORT_SYMBOL_GPL(sdio_readl); |
| 407 | |
| 408 | /** |
| 409 | * sdio_writel - write a 32 bit integer to a SDIO function |
| 410 | * @func: SDIO function to access |
| 411 | * @b: integer to write |
| 412 | * @addr: address to write to |
| 413 | * @err_ret: optional status value from transfer |
| 414 | * |
| 415 | * Writes a 32 bit integer to the address space of a given SDIO |
| 416 | * function. @err_ret will contain the status of the actual |
| 417 | * transfer. |
| 418 | */ |
| 419 | void sdio_writel(struct sdio_func *func, unsigned long b, unsigned int addr, |
| 420 | int *err_ret) |
| 421 | { |
| 422 | int ret; |
| 423 | |
| 424 | *(u32*)func->tmpbuf = cpu_to_le32(b); |
| 425 | |
| 426 | ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4); |
| 427 | if (err_ret) |
| 428 | *err_ret = ret; |
| 429 | } |
| 430 | EXPORT_SYMBOL_GPL(sdio_writel); |
| 431 | |