Linux Audio

Check our new training course

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