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