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