• 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 
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  */
sdio_claim_host(struct sdio_func * func)27 void 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 }
34 EXPORT_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  */
sdio_release_host(struct sdio_func * func)43 void 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 }
50 EXPORT_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  */
sdio_enable_func(struct sdio_func * func)59 int 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, &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 
97 err:
98 	pr_debug("SDIO: Failed to enable device %s\n", sdio_func_id(func));
99 	return ret;
100 }
101 EXPORT_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  */
sdio_disable_func(struct sdio_func * func)110 int 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, &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 
134 err:
135 	pr_debug("SDIO: Failed to disable device %s\n", sdio_func_id(func));
136 	return -EIO;
137 }
138 EXPORT_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  */
sdio_set_block_size(struct sdio_func * func,unsigned blksz)159 int 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 }
184 EXPORT_SYMBOL_GPL(sdio_set_block_size);
185 
186 /*
187  * Calculate the maximum byte mode transfer size
188  */
sdio_max_byte_size(struct sdio_func * func)189 static 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  */
sdio_align_size(struct sdio_func * func,unsigned int sz)219 unsigned int sdio_align_size(struct sdio_func *func, unsigned int sz)
220 {
221 	unsigned int orig_sz;
222 	unsigned int blk_sz, byte_sz;
223 	unsigned chunk_sz;
224 
225 	orig_sz = sz;
226 
227 	/*
228 	 * Do a first check with the controller, in case it
229 	 * wants to increase the size up to a point where it
230 	 * might need more than one block.
231 	 */
232 	sz = mmc_align_data_size(func->card, sz);
233 
234 	/*
235 	 * If we can still do this with just a byte transfer, then
236 	 * we're done.
237 	 */
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 }
300 EXPORT_SYMBOL_GPL(sdio_align_size);
301 
302 /* Split an arbitrarily sized data transfer into several
303  * IO_RW_EXTENDED commands. */
sdio_io_rw_ext_helper(struct sdio_func * func,int write,unsigned addr,int incr_addr,u8 * buf,unsigned size)304 static int sdio_io_rw_ext_helper(struct sdio_func *func, int write,
305 	unsigned addr, int incr_addr, u8 *buf, unsigned size)
306 {
307 	unsigned remainder = size;
308 	unsigned max_blocks;
309 	int ret;
310 
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  */
sdio_readb(struct sdio_func * func,unsigned int addr,int * err_ret)369 u8 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 }
388 EXPORT_SYMBOL_GPL(sdio_readb);
389 
390 /**
391  *	sdio_readb_ext - read a single byte from a SDIO function
392  *	@func: SDIO function to access
393  *	@addr: address to read
394  *	@err_ret: optional status value from transfer
395  *	@in: value to add to argument
396  *
397  *	Reads a single byte from the address space of a given SDIO
398  *	function. If there is a problem reading the address, 0xff
399  *	is returned and @err_ret will contain the error code.
400  */
sdio_readb_ext(struct sdio_func * func,unsigned int addr,int * err_ret,unsigned in)401 unsigned char sdio_readb_ext(struct sdio_func *func, unsigned int addr,
402 	int *err_ret, unsigned in)
403 {
404 	int ret;
405 	unsigned char val;
406 
407 	BUG_ON(!func);
408 
409 	if (err_ret)
410 		*err_ret = 0;
411 
412 	ret = mmc_io_rw_direct(func->card, 0, func->num, addr, (u8)in, &val);
413 	if (ret) {
414 		if (err_ret)
415 			*err_ret = ret;
416 		return 0xFF;
417 	}
418 
419 	return val;
420 }
421 EXPORT_SYMBOL_GPL(sdio_readb_ext);
422 
423 /**
424  *	sdio_writeb - write a single byte to a SDIO function
425  *	@func: SDIO function to access
426  *	@b: byte to write
427  *	@addr: address to write to
428  *	@err_ret: optional status value from transfer
429  *
430  *	Writes a single byte to the address space of a given SDIO
431  *	function. @err_ret will contain the status of the actual
432  *	transfer.
433  */
sdio_writeb(struct sdio_func * func,u8 b,unsigned int addr,int * err_ret)434 void sdio_writeb(struct sdio_func *func, u8 b, unsigned int addr, int *err_ret)
435 {
436 	int ret;
437 
438 	BUG_ON(!func);
439 
440 	ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL);
441 	if (err_ret)
442 		*err_ret = ret;
443 }
444 EXPORT_SYMBOL_GPL(sdio_writeb);
445 
446 /**
447  *	sdio_writeb_readb - write and read a byte from SDIO function
448  *	@func: SDIO function to access
449  *	@write_byte: byte to write
450  *	@addr: address to write to
451  *	@err_ret: optional status value from transfer
452  *
453  *	Performs a RAW (Read after Write) operation as defined by SDIO spec -
454  *	single byte is written to address space of a given SDIO function and
455  *	response is read back from the same address, both using single request.
456  *	If there is a problem with the operation, 0xff is returned and
457  *	@err_ret will contain the error code.
458  */
sdio_writeb_readb(struct sdio_func * func,u8 write_byte,unsigned int addr,int * err_ret)459 u8 sdio_writeb_readb(struct sdio_func *func, u8 write_byte,
460 	unsigned int addr, int *err_ret)
461 {
462 	int ret;
463 	u8 val;
464 
465 	ret = mmc_io_rw_direct(func->card, 1, func->num, addr,
466 			write_byte, &val);
467 	if (err_ret)
468 		*err_ret = ret;
469 	if (ret)
470 		val = 0xff;
471 
472 	return val;
473 }
474 EXPORT_SYMBOL_GPL(sdio_writeb_readb);
475 
476 /**
477  *	sdio_memcpy_fromio - read a chunk of memory from a SDIO function
478  *	@func: SDIO function to access
479  *	@dst: buffer to store the data
480  *	@addr: address to begin reading from
481  *	@count: number of bytes to read
482  *
483  *	Reads from the address space of a given SDIO function. Return
484  *	value indicates if the transfer succeeded or not.
485  */
sdio_memcpy_fromio(struct sdio_func * func,void * dst,unsigned int addr,int count)486 int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
487 	unsigned int addr, int count)
488 {
489 	return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count);
490 }
491 EXPORT_SYMBOL_GPL(sdio_memcpy_fromio);
492 
493 /**
494  *	sdio_memcpy_toio - write a chunk of memory to a SDIO function
495  *	@func: SDIO function to access
496  *	@addr: address to start writing to
497  *	@src: buffer that contains the data to write
498  *	@count: number of bytes to write
499  *
500  *	Writes to the address space of a given SDIO function. Return
501  *	value indicates if the transfer succeeded or not.
502  */
sdio_memcpy_toio(struct sdio_func * func,unsigned int addr,void * src,int count)503 int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
504 	void *src, int count)
505 {
506 	return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count);
507 }
508 EXPORT_SYMBOL_GPL(sdio_memcpy_toio);
509 
510 /**
511  *	sdio_readsb - read from a FIFO on a SDIO function
512  *	@func: SDIO function to access
513  *	@dst: buffer to store the data
514  *	@addr: address of (single byte) FIFO
515  *	@count: number of bytes to read
516  *
517  *	Reads from the specified FIFO of a given SDIO function. Return
518  *	value indicates if the transfer succeeded or not.
519  */
sdio_readsb(struct sdio_func * func,void * dst,unsigned int addr,int count)520 int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
521 	int count)
522 {
523 	return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count);
524 }
525 EXPORT_SYMBOL_GPL(sdio_readsb);
526 
527 /**
528  *	sdio_writesb - write to a FIFO of a SDIO function
529  *	@func: SDIO function to access
530  *	@addr: address of (single byte) FIFO
531  *	@src: buffer that contains the data to write
532  *	@count: number of bytes to write
533  *
534  *	Writes to the specified FIFO of a given SDIO function. Return
535  *	value indicates if the transfer succeeded or not.
536  */
sdio_writesb(struct sdio_func * func,unsigned int addr,void * src,int count)537 int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,
538 	int count)
539 {
540 	return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count);
541 }
542 EXPORT_SYMBOL_GPL(sdio_writesb);
543 
544 /**
545  *	sdio_readw - read a 16 bit integer from a SDIO function
546  *	@func: SDIO function to access
547  *	@addr: address to read
548  *	@err_ret: optional status value from transfer
549  *
550  *	Reads a 16 bit integer from the address space of a given SDIO
551  *	function. If there is a problem reading the address, 0xffff
552  *	is returned and @err_ret will contain the error code.
553  */
sdio_readw(struct sdio_func * func,unsigned int addr,int * err_ret)554 u16 sdio_readw(struct sdio_func *func, unsigned int addr, int *err_ret)
555 {
556 	int ret;
557 
558 	if (err_ret)
559 		*err_ret = 0;
560 
561 	ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);
562 	if (ret) {
563 		if (err_ret)
564 			*err_ret = ret;
565 		return 0xFFFF;
566 	}
567 
568 	return le16_to_cpup((__le16 *)func->tmpbuf);
569 }
570 EXPORT_SYMBOL_GPL(sdio_readw);
571 
572 /**
573  *	sdio_writew - write a 16 bit integer to a SDIO function
574  *	@func: SDIO function to access
575  *	@b: integer to write
576  *	@addr: address to write to
577  *	@err_ret: optional status value from transfer
578  *
579  *	Writes a 16 bit integer to the address space of a given SDIO
580  *	function. @err_ret will contain the status of the actual
581  *	transfer.
582  */
sdio_writew(struct sdio_func * func,u16 b,unsigned int addr,int * err_ret)583 void sdio_writew(struct sdio_func *func, u16 b, unsigned int addr, int *err_ret)
584 {
585 	int ret;
586 
587 	*(__le16 *)func->tmpbuf = cpu_to_le16(b);
588 
589 	ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
590 	if (err_ret)
591 		*err_ret = ret;
592 }
593 EXPORT_SYMBOL_GPL(sdio_writew);
594 
595 /**
596  *	sdio_readl - read a 32 bit integer from a SDIO function
597  *	@func: SDIO function to access
598  *	@addr: address to read
599  *	@err_ret: optional status value from transfer
600  *
601  *	Reads a 32 bit integer from the address space of a given SDIO
602  *	function. If there is a problem reading the address,
603  *	0xffffffff is returned and @err_ret will contain the error
604  *	code.
605  */
sdio_readl(struct sdio_func * func,unsigned int addr,int * err_ret)606 u32 sdio_readl(struct sdio_func *func, unsigned int addr, int *err_ret)
607 {
608 	int ret;
609 
610 	if (err_ret)
611 		*err_ret = 0;
612 
613 	ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4);
614 	if (ret) {
615 		if (err_ret)
616 			*err_ret = ret;
617 		return 0xFFFFFFFF;
618 	}
619 
620 	return le32_to_cpup((__le32 *)func->tmpbuf);
621 }
622 EXPORT_SYMBOL_GPL(sdio_readl);
623 
624 /**
625  *	sdio_writel - write a 32 bit integer to a SDIO function
626  *	@func: SDIO function to access
627  *	@b: integer to write
628  *	@addr: address to write to
629  *	@err_ret: optional status value from transfer
630  *
631  *	Writes a 32 bit integer to the address space of a given SDIO
632  *	function. @err_ret will contain the status of the actual
633  *	transfer.
634  */
sdio_writel(struct sdio_func * func,u32 b,unsigned int addr,int * err_ret)635 void sdio_writel(struct sdio_func *func, u32 b, unsigned int addr, int *err_ret)
636 {
637 	int ret;
638 
639 	*(__le32 *)func->tmpbuf = cpu_to_le32(b);
640 
641 	ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
642 	if (err_ret)
643 		*err_ret = ret;
644 }
645 EXPORT_SYMBOL_GPL(sdio_writel);
646 
647 /**
648  *	sdio_f0_readb - read a single byte from SDIO function 0
649  *	@func: an SDIO function of the card
650  *	@addr: address to read
651  *	@err_ret: optional status value from transfer
652  *
653  *	Reads a single byte from the address space of SDIO function 0.
654  *	If there is a problem reading the address, 0xff is returned
655  *	and @err_ret will contain the error code.
656  */
sdio_f0_readb(struct sdio_func * func,unsigned int addr,int * err_ret)657 unsigned char sdio_f0_readb(struct sdio_func *func, unsigned int addr,
658 	int *err_ret)
659 {
660 	int ret;
661 	unsigned char val;
662 
663 	BUG_ON(!func);
664 
665 	if (err_ret)
666 		*err_ret = 0;
667 
668 	ret = mmc_io_rw_direct(func->card, 0, 0, addr, 0, &val);
669 	if (ret) {
670 		if (err_ret)
671 			*err_ret = ret;
672 		return 0xFF;
673 	}
674 
675 	return val;
676 }
677 EXPORT_SYMBOL_GPL(sdio_f0_readb);
678 
679 /**
680  *	sdio_f0_writeb - write a single byte to SDIO function 0
681  *	@func: an SDIO function of the card
682  *	@b: byte to write
683  *	@addr: address to write to
684  *	@err_ret: optional status value from transfer
685  *
686  *	Writes a single byte to the address space of SDIO function 0.
687  *	@err_ret will contain the status of the actual transfer.
688  *
689  *	Only writes to the vendor specific CCCR registers (0xF0 -
690  *	0xFF) are permiited; @err_ret will be set to -EINVAL for *
691  *	writes outside this range.
692  */
sdio_f0_writeb(struct sdio_func * func,unsigned char b,unsigned int addr,int * err_ret)693 void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
694 	int *err_ret)
695 {
696 	int ret;
697 
698 	BUG_ON(!func);
699 
700 	if ((addr < 0xF0 || addr > 0xFF) && (!mmc_card_lenient_fn0(func->card))) {
701 		if (err_ret)
702 			*err_ret = -EINVAL;
703 		return;
704 	}
705 
706 	ret = mmc_io_rw_direct(func->card, 1, 0, addr, b, NULL);
707 	if (err_ret)
708 		*err_ret = ret;
709 }
710 EXPORT_SYMBOL_GPL(sdio_f0_writeb);
711 
712 /**
713  *	sdio_get_host_pm_caps - get host power management capabilities
714  *	@func: SDIO function attached to host
715  *
716  *	Returns a capability bitmask corresponding to power management
717  *	features supported by the host controller that the card function
718  *	might rely upon during a system suspend.  The host doesn't need
719  *	to be claimed, nor the function active, for this information to be
720  *	obtained.
721  */
sdio_get_host_pm_caps(struct sdio_func * func)722 mmc_pm_flag_t sdio_get_host_pm_caps(struct sdio_func *func)
723 {
724 	BUG_ON(!func);
725 	BUG_ON(!func->card);
726 
727 	return func->card->host->pm_caps;
728 }
729 EXPORT_SYMBOL_GPL(sdio_get_host_pm_caps);
730 
731 /**
732  *	sdio_set_host_pm_flags - set wanted host power management capabilities
733  *	@func: SDIO function attached to host
734  *
735  *	Set a capability bitmask corresponding to wanted host controller
736  *	power management features for the upcoming suspend state.
737  *	This must be called, if needed, each time the suspend method of
738  *	the function driver is called, and must contain only bits that
739  *	were returned by sdio_get_host_pm_caps().
740  *	The host doesn't need to be claimed, nor the function active,
741  *	for this information to be set.
742  */
sdio_set_host_pm_flags(struct sdio_func * func,mmc_pm_flag_t flags)743 int sdio_set_host_pm_flags(struct sdio_func *func, mmc_pm_flag_t flags)
744 {
745 	struct mmc_host *host;
746 
747 	BUG_ON(!func);
748 	BUG_ON(!func->card);
749 
750 	host = func->card->host;
751 
752 	if (flags & ~host->pm_caps)
753 		return -EINVAL;
754 
755 	/* function suspend methods are serialized, hence no lock needed */
756 	host->pm_flags |= flags;
757 	return 0;
758 }
759 EXPORT_SYMBOL_GPL(sdio_set_host_pm_flags);
760