Loading...
1/*
2 * linux/drivers/mmc/core/sdio_io.c
3 *
4 * Copyright 2007-2008 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/export.h>
13#include <linux/mmc/host.h>
14#include <linux/mmc/card.h>
15#include <linux/mmc/sdio.h>
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 */
27void 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}
34EXPORT_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 */
43void 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}
50EXPORT_SYMBOL_GPL(sdio_release_host);
51
52/**
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 */
59int 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
80 timeout = jiffies + msecs_to_jiffies(func->enable_timeout);
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
97err:
98 pr_debug("SDIO: Failed to enable device %s\n", sdio_func_id(func));
99 return ret;
100}
101EXPORT_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 */
110int 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
134err:
135 pr_debug("SDIO: Failed to disable device %s\n", sdio_func_id(func));
136 return -EIO;
137}
138EXPORT_SYMBOL_GPL(sdio_disable_func);
139
140/**
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 */
159int 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) {
167 blksz = min(func->max_blksize, func->card->host->max_blk_size);
168 blksz = min(blksz, 512u);
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}
184EXPORT_SYMBOL_GPL(sdio_set_block_size);
185
186/*
187 * Calculate the maximum byte mode transfer size
188 */
189static inline unsigned int sdio_max_byte_size(struct sdio_func *func)
190{
191 unsigned mval = min(func->card->host->max_seg_size,
192 func->card->host->max_blk_size);
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
199 if (mmc_card_broken_byte_mode_512(func->card))
200 return min(mval, 511u);
201
202 return min(mval, 512u); /* maximum size for byte mode */
203}
204
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 */
219unsigned 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 */
238 if (sz <= sdio_max_byte_size(func))
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);
269 if (byte_sz <= sdio_max_byte_size(func)) {
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,
279 sdio_max_byte_size(func));
280 if (chunk_sz == sdio_max_byte_size(func)) {
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}
300EXPORT_SYMBOL_GPL(sdio_align_size);
301
302/* Split an arbitrarily sized data transfer into several
303 * IO_RW_EXTENDED commands. */
304static 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
311 /* Do the bulk of the transfer using block mode (if supported). */
312 if (func->card->cccr.multi_block && (size > sdio_max_byte_size(func))) {
313 /* Blocks per command is limited by host count, host transfer
314 * size (we only use a single sg entry) and the maximum for
315 * IO_RW_EXTENDED of 511 blocks. */
316 max_blocks = min(func->card->host->max_blk_count,
317 func->card->host->max_seg_size / func->cur_blksize);
318 max_blocks = min(max_blocks, 511u);
319
320 while (remainder >= func->cur_blksize) {
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) {
343 size = min(remainder, sdio_max_byte_size(func));
344
345 /* Indicate byte mode by setting "blocks" = 0 */
346 ret = mmc_io_rw_extended(func->card, write, func->num, addr,
347 incr_addr, buf, 0, size);
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
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 */
369u8 sdio_readb(struct sdio_func *func, unsigned int addr, int *err_ret)
370{
371 int ret;
372 u8 val;
373
374 BUG_ON(!func);
375
376 if (err_ret)
377 *err_ret = 0;
378
379 ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val);
380 if (ret) {
381 if (err_ret)
382 *err_ret = ret;
383 return 0xFF;
384 }
385
386 return val;
387}
388EXPORT_SYMBOL_GPL(sdio_readb);
389
390/**
391 * sdio_writeb - write a single byte to a SDIO function
392 * @func: SDIO function to access
393 * @b: byte to write
394 * @addr: address to write to
395 * @err_ret: optional status value from transfer
396 *
397 * Writes a single byte to the address space of a given SDIO
398 * function. @err_ret will contain the status of the actual
399 * transfer.
400 */
401void sdio_writeb(struct sdio_func *func, u8 b, unsigned int addr, int *err_ret)
402{
403 int ret;
404
405 BUG_ON(!func);
406
407 ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL);
408 if (err_ret)
409 *err_ret = ret;
410}
411EXPORT_SYMBOL_GPL(sdio_writeb);
412
413/**
414 * sdio_writeb_readb - write and read a byte from SDIO function
415 * @func: SDIO function to access
416 * @write_byte: byte to write
417 * @addr: address to write to
418 * @err_ret: optional status value from transfer
419 *
420 * Performs a RAW (Read after Write) operation as defined by SDIO spec -
421 * single byte is written to address space of a given SDIO function and
422 * response is read back from the same address, both using single request.
423 * If there is a problem with the operation, 0xff is returned and
424 * @err_ret will contain the error code.
425 */
426u8 sdio_writeb_readb(struct sdio_func *func, u8 write_byte,
427 unsigned int addr, int *err_ret)
428{
429 int ret;
430 u8 val;
431
432 ret = mmc_io_rw_direct(func->card, 1, func->num, addr,
433 write_byte, &val);
434 if (err_ret)
435 *err_ret = ret;
436 if (ret)
437 val = 0xff;
438
439 return val;
440}
441EXPORT_SYMBOL_GPL(sdio_writeb_readb);
442
443/**
444 * sdio_memcpy_fromio - read a chunk of memory from a SDIO function
445 * @func: SDIO function to access
446 * @dst: buffer to store the data
447 * @addr: address to begin reading from
448 * @count: number of bytes to read
449 *
450 * Reads from the address space of a given SDIO function. Return
451 * value indicates if the transfer succeeded or not.
452 */
453int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
454 unsigned int addr, int count)
455{
456 return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count);
457}
458EXPORT_SYMBOL_GPL(sdio_memcpy_fromio);
459
460/**
461 * sdio_memcpy_toio - write a chunk of memory to a SDIO function
462 * @func: SDIO function to access
463 * @addr: address to start writing to
464 * @src: buffer that contains the data to write
465 * @count: number of bytes to write
466 *
467 * Writes to the address space of a given SDIO function. Return
468 * value indicates if the transfer succeeded or not.
469 */
470int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
471 void *src, int count)
472{
473 return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count);
474}
475EXPORT_SYMBOL_GPL(sdio_memcpy_toio);
476
477/**
478 * sdio_readsb - read from a FIFO on a SDIO function
479 * @func: SDIO function to access
480 * @dst: buffer to store the data
481 * @addr: address of (single byte) FIFO
482 * @count: number of bytes to read
483 *
484 * Reads from the specified FIFO of a given SDIO function. Return
485 * value indicates if the transfer succeeded or not.
486 */
487int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
488 int count)
489{
490 return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count);
491}
492EXPORT_SYMBOL_GPL(sdio_readsb);
493
494/**
495 * sdio_writesb - write to a FIFO of a SDIO function
496 * @func: SDIO function to access
497 * @addr: address of (single byte) FIFO
498 * @src: buffer that contains the data to write
499 * @count: number of bytes to write
500 *
501 * Writes to the specified FIFO of a given SDIO function. Return
502 * value indicates if the transfer succeeded or not.
503 */
504int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,
505 int count)
506{
507 return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count);
508}
509EXPORT_SYMBOL_GPL(sdio_writesb);
510
511/**
512 * sdio_readw - read a 16 bit integer from a SDIO function
513 * @func: SDIO function to access
514 * @addr: address to read
515 * @err_ret: optional status value from transfer
516 *
517 * Reads a 16 bit integer from the address space of a given SDIO
518 * function. If there is a problem reading the address, 0xffff
519 * is returned and @err_ret will contain the error code.
520 */
521u16 sdio_readw(struct sdio_func *func, unsigned int addr, int *err_ret)
522{
523 int ret;
524
525 if (err_ret)
526 *err_ret = 0;
527
528 ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);
529 if (ret) {
530 if (err_ret)
531 *err_ret = ret;
532 return 0xFFFF;
533 }
534
535 return le16_to_cpup((__le16 *)func->tmpbuf);
536}
537EXPORT_SYMBOL_GPL(sdio_readw);
538
539/**
540 * sdio_writew - write a 16 bit integer to a SDIO function
541 * @func: SDIO function to access
542 * @b: integer to write
543 * @addr: address to write to
544 * @err_ret: optional status value from transfer
545 *
546 * Writes a 16 bit integer to the address space of a given SDIO
547 * function. @err_ret will contain the status of the actual
548 * transfer.
549 */
550void sdio_writew(struct sdio_func *func, u16 b, unsigned int addr, int *err_ret)
551{
552 int ret;
553
554 *(__le16 *)func->tmpbuf = cpu_to_le16(b);
555
556 ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
557 if (err_ret)
558 *err_ret = ret;
559}
560EXPORT_SYMBOL_GPL(sdio_writew);
561
562/**
563 * sdio_readl - read a 32 bit integer from a SDIO function
564 * @func: SDIO function to access
565 * @addr: address to read
566 * @err_ret: optional status value from transfer
567 *
568 * Reads a 32 bit integer from the address space of a given SDIO
569 * function. If there is a problem reading the address,
570 * 0xffffffff is returned and @err_ret will contain the error
571 * code.
572 */
573u32 sdio_readl(struct sdio_func *func, unsigned int addr, int *err_ret)
574{
575 int ret;
576
577 if (err_ret)
578 *err_ret = 0;
579
580 ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4);
581 if (ret) {
582 if (err_ret)
583 *err_ret = ret;
584 return 0xFFFFFFFF;
585 }
586
587 return le32_to_cpup((__le32 *)func->tmpbuf);
588}
589EXPORT_SYMBOL_GPL(sdio_readl);
590
591/**
592 * sdio_writel - write a 32 bit integer to a SDIO function
593 * @func: SDIO function to access
594 * @b: integer to write
595 * @addr: address to write to
596 * @err_ret: optional status value from transfer
597 *
598 * Writes a 32 bit integer to the address space of a given SDIO
599 * function. @err_ret will contain the status of the actual
600 * transfer.
601 */
602void sdio_writel(struct sdio_func *func, u32 b, unsigned int addr, int *err_ret)
603{
604 int ret;
605
606 *(__le32 *)func->tmpbuf = cpu_to_le32(b);
607
608 ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
609 if (err_ret)
610 *err_ret = ret;
611}
612EXPORT_SYMBOL_GPL(sdio_writel);
613
614/**
615 * sdio_f0_readb - read a single byte from SDIO function 0
616 * @func: an SDIO function of the card
617 * @addr: address to read
618 * @err_ret: optional status value from transfer
619 *
620 * Reads a single byte from the address space of SDIO function 0.
621 * If there is a problem reading the address, 0xff is returned
622 * and @err_ret will contain the error code.
623 */
624unsigned char sdio_f0_readb(struct sdio_func *func, unsigned int addr,
625 int *err_ret)
626{
627 int ret;
628 unsigned char val;
629
630 BUG_ON(!func);
631
632 if (err_ret)
633 *err_ret = 0;
634
635 ret = mmc_io_rw_direct(func->card, 0, 0, addr, 0, &val);
636 if (ret) {
637 if (err_ret)
638 *err_ret = ret;
639 return 0xFF;
640 }
641
642 return val;
643}
644EXPORT_SYMBOL_GPL(sdio_f0_readb);
645
646/**
647 * sdio_f0_writeb - write a single byte to SDIO function 0
648 * @func: an SDIO function of the card
649 * @b: byte to write
650 * @addr: address to write to
651 * @err_ret: optional status value from transfer
652 *
653 * Writes a single byte to the address space of SDIO function 0.
654 * @err_ret will contain the status of the actual transfer.
655 *
656 * Only writes to the vendor specific CCCR registers (0xF0 -
657 * 0xFF) are permiited; @err_ret will be set to -EINVAL for *
658 * writes outside this range.
659 */
660void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
661 int *err_ret)
662{
663 int ret;
664
665 BUG_ON(!func);
666
667 if ((addr < 0xF0 || addr > 0xFF) && (!mmc_card_lenient_fn0(func->card))) {
668 if (err_ret)
669 *err_ret = -EINVAL;
670 return;
671 }
672
673 ret = mmc_io_rw_direct(func->card, 1, 0, addr, b, NULL);
674 if (err_ret)
675 *err_ret = ret;
676}
677EXPORT_SYMBOL_GPL(sdio_f0_writeb);
678
679/**
680 * sdio_get_host_pm_caps - get host power management capabilities
681 * @func: SDIO function attached to host
682 *
683 * Returns a capability bitmask corresponding to power management
684 * features supported by the host controller that the card function
685 * might rely upon during a system suspend. The host doesn't need
686 * to be claimed, nor the function active, for this information to be
687 * obtained.
688 */
689mmc_pm_flag_t sdio_get_host_pm_caps(struct sdio_func *func)
690{
691 BUG_ON(!func);
692 BUG_ON(!func->card);
693
694 return func->card->host->pm_caps;
695}
696EXPORT_SYMBOL_GPL(sdio_get_host_pm_caps);
697
698/**
699 * sdio_set_host_pm_flags - set wanted host power management capabilities
700 * @func: SDIO function attached to host
701 *
702 * Set a capability bitmask corresponding to wanted host controller
703 * power management features for the upcoming suspend state.
704 * This must be called, if needed, each time the suspend method of
705 * the function driver is called, and must contain only bits that
706 * were returned by sdio_get_host_pm_caps().
707 * The host doesn't need to be claimed, nor the function active,
708 * for this information to be set.
709 */
710int sdio_set_host_pm_flags(struct sdio_func *func, mmc_pm_flag_t flags)
711{
712 struct mmc_host *host;
713
714 BUG_ON(!func);
715 BUG_ON(!func->card);
716
717 host = func->card->host;
718
719 if (flags & ~host->pm_caps)
720 return -EINVAL;
721
722 /* function suspend methods are serialized, hence no lock needed */
723 host->pm_flags |= flags;
724 return 0;
725}
726EXPORT_SYMBOL_GPL(sdio_set_host_pm_flags);
1/*
2 * linux/drivers/mmc/core/sdio_io.c
3 *
4 * Copyright 2007-2008 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/export.h>
13#include <linux/mmc/host.h>
14#include <linux/mmc/card.h>
15#include <linux/mmc/sdio.h>
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 */
27void sdio_claim_host(struct sdio_func *func)
28{
29 if (WARN_ON(!func))
30 return;
31
32 mmc_claim_host(func->card->host);
33}
34EXPORT_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 */
43void sdio_release_host(struct sdio_func *func)
44{
45 if (WARN_ON(!func))
46 return;
47
48 mmc_release_host(func->card->host);
49}
50EXPORT_SYMBOL_GPL(sdio_release_host);
51
52/**
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 */
59int sdio_enable_func(struct sdio_func *func)
60{
61 int ret;
62 unsigned char reg;
63 unsigned long timeout;
64
65 if (!func)
66 return -EINVAL;
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
80 timeout = jiffies + msecs_to_jiffies(func->enable_timeout);
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
97err:
98 pr_debug("SDIO: Failed to enable device %s\n", sdio_func_id(func));
99 return ret;
100}
101EXPORT_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 */
110int sdio_disable_func(struct sdio_func *func)
111{
112 int ret;
113 unsigned char reg;
114
115 if (!func)
116 return -EINVAL;
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
134err:
135 pr_debug("SDIO: Failed to disable device %s\n", sdio_func_id(func));
136 return -EIO;
137}
138EXPORT_SYMBOL_GPL(sdio_disable_func);
139
140/**
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 */
159int 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) {
167 blksz = min(func->max_blksize, func->card->host->max_blk_size);
168 blksz = min(blksz, 512u);
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}
184EXPORT_SYMBOL_GPL(sdio_set_block_size);
185
186/*
187 * Calculate the maximum byte mode transfer size
188 */
189static inline unsigned int sdio_max_byte_size(struct sdio_func *func)
190{
191 unsigned mval = func->card->host->max_blk_size;
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
198 if (mmc_card_broken_byte_mode_512(func->card))
199 return min(mval, 511u);
200
201 return min(mval, 512u); /* maximum size for byte mode */
202}
203
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 */
218unsigned 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 */
237 if (sz <= sdio_max_byte_size(func))
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);
268 if (byte_sz <= sdio_max_byte_size(func)) {
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,
278 sdio_max_byte_size(func));
279 if (chunk_sz == sdio_max_byte_size(func)) {
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}
299EXPORT_SYMBOL_GPL(sdio_align_size);
300
301/* Split an arbitrarily sized data transfer into several
302 * IO_RW_EXTENDED commands. */
303static 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
310 if (!func || (func->num > 7))
311 return -EINVAL;
312
313 /* Do the bulk of the transfer using block mode (if supported). */
314 if (func->card->cccr.multi_block && (size > sdio_max_byte_size(func))) {
315 /* Blocks per command is limited by host count, host transfer
316 * size and the maximum for IO_RW_EXTENDED of 511 blocks. */
317 max_blocks = min(func->card->host->max_blk_count, 511u);
318
319 while (remainder >= func->cur_blksize) {
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) {
342 size = min(remainder, sdio_max_byte_size(func));
343
344 /* Indicate byte mode by setting "blocks" = 0 */
345 ret = mmc_io_rw_extended(func->card, write, func->num, addr,
346 incr_addr, buf, 0, size);
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
358/**
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 */
368u8 sdio_readb(struct sdio_func *func, unsigned int addr, int *err_ret)
369{
370 int ret;
371 u8 val;
372
373 if (!func) {
374 *err_ret = -EINVAL;
375 return 0xFF;
376 }
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}
390EXPORT_SYMBOL_GPL(sdio_readb);
391
392/**
393 * sdio_writeb - write a single byte to a SDIO function
394 * @func: SDIO function to access
395 * @b: byte to write
396 * @addr: address to write to
397 * @err_ret: optional status value from transfer
398 *
399 * Writes a single byte to the address space of a given SDIO
400 * function. @err_ret will contain the status of the actual
401 * transfer.
402 */
403void sdio_writeb(struct sdio_func *func, u8 b, unsigned int addr, int *err_ret)
404{
405 int ret;
406
407 if (!func) {
408 *err_ret = -EINVAL;
409 return;
410 }
411
412 ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL);
413 if (err_ret)
414 *err_ret = ret;
415}
416EXPORT_SYMBOL_GPL(sdio_writeb);
417
418/**
419 * sdio_writeb_readb - write and read a byte from SDIO function
420 * @func: SDIO function to access
421 * @write_byte: byte to write
422 * @addr: address to write to
423 * @err_ret: optional status value from transfer
424 *
425 * Performs a RAW (Read after Write) operation as defined by SDIO spec -
426 * single byte is written to address space of a given SDIO function and
427 * response is read back from the same address, both using single request.
428 * If there is a problem with the operation, 0xff is returned and
429 * @err_ret will contain the error code.
430 */
431u8 sdio_writeb_readb(struct sdio_func *func, u8 write_byte,
432 unsigned int addr, int *err_ret)
433{
434 int ret;
435 u8 val;
436
437 ret = mmc_io_rw_direct(func->card, 1, func->num, addr,
438 write_byte, &val);
439 if (err_ret)
440 *err_ret = ret;
441 if (ret)
442 val = 0xff;
443
444 return val;
445}
446EXPORT_SYMBOL_GPL(sdio_writeb_readb);
447
448/**
449 * sdio_memcpy_fromio - read a chunk of memory from a SDIO function
450 * @func: SDIO function to access
451 * @dst: buffer to store the data
452 * @addr: address to begin reading from
453 * @count: number of bytes to read
454 *
455 * Reads from the address space of a given SDIO function. Return
456 * value indicates if the transfer succeeded or not.
457 */
458int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
459 unsigned int addr, int count)
460{
461 return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count);
462}
463EXPORT_SYMBOL_GPL(sdio_memcpy_fromio);
464
465/**
466 * sdio_memcpy_toio - write a chunk of memory to a SDIO function
467 * @func: SDIO function to access
468 * @addr: address to start writing to
469 * @src: buffer that contains the data to write
470 * @count: number of bytes to write
471 *
472 * Writes to the address space of a given SDIO function. Return
473 * value indicates if the transfer succeeded or not.
474 */
475int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
476 void *src, int count)
477{
478 return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count);
479}
480EXPORT_SYMBOL_GPL(sdio_memcpy_toio);
481
482/**
483 * sdio_readsb - read from a FIFO on a SDIO function
484 * @func: SDIO function to access
485 * @dst: buffer to store the data
486 * @addr: address of (single byte) FIFO
487 * @count: number of bytes to read
488 *
489 * Reads from the specified FIFO of a given SDIO function. Return
490 * value indicates if the transfer succeeded or not.
491 */
492int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
493 int count)
494{
495 return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count);
496}
497EXPORT_SYMBOL_GPL(sdio_readsb);
498
499/**
500 * sdio_writesb - write to a FIFO of a SDIO function
501 * @func: SDIO function to access
502 * @addr: address of (single byte) FIFO
503 * @src: buffer that contains the data to write
504 * @count: number of bytes to write
505 *
506 * Writes to the specified FIFO of a given SDIO function. Return
507 * value indicates if the transfer succeeded or not.
508 */
509int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,
510 int count)
511{
512 return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count);
513}
514EXPORT_SYMBOL_GPL(sdio_writesb);
515
516/**
517 * sdio_readw - read a 16 bit integer from a SDIO function
518 * @func: SDIO function to access
519 * @addr: address to read
520 * @err_ret: optional status value from transfer
521 *
522 * Reads a 16 bit integer from the address space of a given SDIO
523 * function. If there is a problem reading the address, 0xffff
524 * is returned and @err_ret will contain the error code.
525 */
526u16 sdio_readw(struct sdio_func *func, unsigned int addr, int *err_ret)
527{
528 int ret;
529
530 if (err_ret)
531 *err_ret = 0;
532
533 ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);
534 if (ret) {
535 if (err_ret)
536 *err_ret = ret;
537 return 0xFFFF;
538 }
539
540 return le16_to_cpup((__le16 *)func->tmpbuf);
541}
542EXPORT_SYMBOL_GPL(sdio_readw);
543
544/**
545 * sdio_writew - write a 16 bit integer to a SDIO function
546 * @func: SDIO function to access
547 * @b: integer to write
548 * @addr: address to write to
549 * @err_ret: optional status value from transfer
550 *
551 * Writes a 16 bit integer to the address space of a given SDIO
552 * function. @err_ret will contain the status of the actual
553 * transfer.
554 */
555void sdio_writew(struct sdio_func *func, u16 b, unsigned int addr, int *err_ret)
556{
557 int ret;
558
559 *(__le16 *)func->tmpbuf = cpu_to_le16(b);
560
561 ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
562 if (err_ret)
563 *err_ret = ret;
564}
565EXPORT_SYMBOL_GPL(sdio_writew);
566
567/**
568 * sdio_readl - read a 32 bit integer from a SDIO function
569 * @func: SDIO function to access
570 * @addr: address to read
571 * @err_ret: optional status value from transfer
572 *
573 * Reads a 32 bit integer from the address space of a given SDIO
574 * function. If there is a problem reading the address,
575 * 0xffffffff is returned and @err_ret will contain the error
576 * code.
577 */
578u32 sdio_readl(struct sdio_func *func, unsigned int addr, int *err_ret)
579{
580 int ret;
581
582 if (err_ret)
583 *err_ret = 0;
584
585 ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4);
586 if (ret) {
587 if (err_ret)
588 *err_ret = ret;
589 return 0xFFFFFFFF;
590 }
591
592 return le32_to_cpup((__le32 *)func->tmpbuf);
593}
594EXPORT_SYMBOL_GPL(sdio_readl);
595
596/**
597 * sdio_writel - write a 32 bit integer to a SDIO function
598 * @func: SDIO function to access
599 * @b: integer to write
600 * @addr: address to write to
601 * @err_ret: optional status value from transfer
602 *
603 * Writes a 32 bit integer to the address space of a given SDIO
604 * function. @err_ret will contain the status of the actual
605 * transfer.
606 */
607void sdio_writel(struct sdio_func *func, u32 b, unsigned int addr, int *err_ret)
608{
609 int ret;
610
611 *(__le32 *)func->tmpbuf = cpu_to_le32(b);
612
613 ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
614 if (err_ret)
615 *err_ret = ret;
616}
617EXPORT_SYMBOL_GPL(sdio_writel);
618
619/**
620 * sdio_f0_readb - read a single byte from SDIO function 0
621 * @func: an SDIO function of the card
622 * @addr: address to read
623 * @err_ret: optional status value from transfer
624 *
625 * Reads a single byte from the address space of SDIO function 0.
626 * If there is a problem reading the address, 0xff is returned
627 * and @err_ret will contain the error code.
628 */
629unsigned char sdio_f0_readb(struct sdio_func *func, unsigned int addr,
630 int *err_ret)
631{
632 int ret;
633 unsigned char val;
634
635 if (!func) {
636 *err_ret = -EINVAL;
637 return 0xFF;
638 }
639
640 if (err_ret)
641 *err_ret = 0;
642
643 ret = mmc_io_rw_direct(func->card, 0, 0, addr, 0, &val);
644 if (ret) {
645 if (err_ret)
646 *err_ret = ret;
647 return 0xFF;
648 }
649
650 return val;
651}
652EXPORT_SYMBOL_GPL(sdio_f0_readb);
653
654/**
655 * sdio_f0_writeb - write a single byte to SDIO function 0
656 * @func: an SDIO function of the card
657 * @b: byte to write
658 * @addr: address to write to
659 * @err_ret: optional status value from transfer
660 *
661 * Writes a single byte to the address space of SDIO function 0.
662 * @err_ret will contain the status of the actual transfer.
663 *
664 * Only writes to the vendor specific CCCR registers (0xF0 -
665 * 0xFF) are permiited; @err_ret will be set to -EINVAL for *
666 * writes outside this range.
667 */
668void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
669 int *err_ret)
670{
671 int ret;
672
673 if (!func) {
674 *err_ret = -EINVAL;
675 return;
676 }
677
678 if ((addr < 0xF0 || addr > 0xFF) && (!mmc_card_lenient_fn0(func->card))) {
679 if (err_ret)
680 *err_ret = -EINVAL;
681 return;
682 }
683
684 ret = mmc_io_rw_direct(func->card, 1, 0, addr, b, NULL);
685 if (err_ret)
686 *err_ret = ret;
687}
688EXPORT_SYMBOL_GPL(sdio_f0_writeb);
689
690/**
691 * sdio_get_host_pm_caps - get host power management capabilities
692 * @func: SDIO function attached to host
693 *
694 * Returns a capability bitmask corresponding to power management
695 * features supported by the host controller that the card function
696 * might rely upon during a system suspend. The host doesn't need
697 * to be claimed, nor the function active, for this information to be
698 * obtained.
699 */
700mmc_pm_flag_t sdio_get_host_pm_caps(struct sdio_func *func)
701{
702 if (!func)
703 return 0;
704
705 return func->card->host->pm_caps;
706}
707EXPORT_SYMBOL_GPL(sdio_get_host_pm_caps);
708
709/**
710 * sdio_set_host_pm_flags - set wanted host power management capabilities
711 * @func: SDIO function attached to host
712 *
713 * Set a capability bitmask corresponding to wanted host controller
714 * power management features for the upcoming suspend state.
715 * This must be called, if needed, each time the suspend method of
716 * the function driver is called, and must contain only bits that
717 * were returned by sdio_get_host_pm_caps().
718 * The host doesn't need to be claimed, nor the function active,
719 * for this information to be set.
720 */
721int sdio_set_host_pm_flags(struct sdio_func *func, mmc_pm_flag_t flags)
722{
723 struct mmc_host *host;
724
725 if (!func)
726 return -EINVAL;
727
728 host = func->card->host;
729
730 if (flags & ~host->pm_caps)
731 return -EINVAL;
732
733 /* function suspend methods are serialized, hence no lock needed */
734 host->pm_flags |= flags;
735 return 0;
736}
737EXPORT_SYMBOL_GPL(sdio_set_host_pm_flags);