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