blob: 6ee7861fceae96b49ac8ffbbeda28c4bc3d65c45 [file] [log] [blame]
Pierre Ossman46f555f2007-05-27 12:57:15 +02001/*
2 * linux/drivers/mmc/core/sdio_io.c
3 *
Pierre Ossmanad3868b2008-06-28 12:52:45 +02004 * Copyright 2007-2008 Pierre Ossman
Pierre Ossman46f555f2007-05-27 12:57:15 +02005 *
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>
Pierre Ossmanfa64efa2007-05-27 14:22:37 +020014#include <linux/mmc/sdio.h>
Pierre Ossman46f555f2007-05-27 12:57:15 +020015#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 */
26void 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}
33EXPORT_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 */
42void 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}
49EXPORT_SYMBOL_GPL(sdio_release_host);
50
51/**
Pierre Ossmanfa64efa2007-05-27 14:22:37 +020052 * 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 */
58int 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 /*
80 * FIXME: This should timeout based on information in the CIS,
81 * but we don't have card to parse that yet.
82 */
83 timeout = jiffies + HZ;
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
100err:
101 pr_debug("SDIO: Failed to enable device %s\n", sdio_func_id(func));
102 return ret;
103}
104EXPORT_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 */
113int sdio_disable_func(struct sdio_func *func)
114{
115 int ret;
116 unsigned char reg;
117
118 BUG_ON(!func);
119 BUG_ON(!func->card);
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
137err:
138 pr_debug("SDIO: Failed to disable device %s\n", sdio_func_id(func));
139 return -EIO;
140}
141EXPORT_SYMBOL_GPL(sdio_disable_func);
142
143/**
David Vrabel9a08f822007-08-08 14:23:48 +0100144 * 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 */
162int 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(min(
171 func->max_blksize,
172 func->card->host->max_blk_size),
173 512u);
174 }
175
176 ret = mmc_io_rw_direct(func->card, 1, 0,
177 SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE,
178 blksz & 0xff, NULL);
179 if (ret)
180 return ret;
181 ret = mmc_io_rw_direct(func->card, 1, 0,
182 SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE + 1,
183 (blksz >> 8) & 0xff, NULL);
184 if (ret)
185 return ret;
186 func->cur_blksize = blksz;
187 return 0;
188}
189
190EXPORT_SYMBOL_GPL(sdio_set_block_size);
191
Pierre Ossmanad3868b2008-06-28 12:52:45 +0200192/**
193 * sdio_align_size - pads a transfer size to a more optimal value
194 * @func: SDIO function
195 * @sz: original transfer size
196 *
197 * Pads the original data size with a number of extra bytes in
198 * order to avoid controller bugs and/or performance hits
199 * (e.g. some controllers revert to PIO for certain sizes).
200 *
201 * If possible, it will also adjust the size so that it can be
202 * handled in just a single request.
203 *
204 * Returns the improved size, which might be unmodified.
205 */
206unsigned int sdio_align_size(struct sdio_func *func, unsigned int sz)
207{
208 unsigned int orig_sz;
209 unsigned int blk_sz, byte_sz;
210 unsigned chunk_sz;
211
212 orig_sz = sz;
213
214 /*
215 * Do a first check with the controller, in case it
216 * wants to increase the size up to a point where it
217 * might need more than one block.
218 */
219 sz = mmc_align_data_size(func->card, sz);
220
221 /*
222 * If we can still do this with just a byte transfer, then
223 * we're done.
224 */
225 if ((sz <= func->cur_blksize) && (sz <= 512))
226 return sz;
227
228 if (func->card->cccr.multi_block) {
229 /*
230 * Check if the transfer is already block aligned
231 */
232 if ((sz % func->cur_blksize) == 0)
233 return sz;
234
235 /*
236 * Realign it so that it can be done with one request,
237 * and recheck if the controller still likes it.
238 */
239 blk_sz = ((sz + func->cur_blksize - 1) /
240 func->cur_blksize) * func->cur_blksize;
241 blk_sz = mmc_align_data_size(func->card, blk_sz);
242
243 /*
244 * This value is only good if it is still just
245 * one request.
246 */
247 if ((blk_sz % func->cur_blksize) == 0)
248 return blk_sz;
249
250 /*
251 * We failed to do one request, but at least try to
252 * pad the remainder properly.
253 */
254 byte_sz = mmc_align_data_size(func->card,
255 sz % func->cur_blksize);
256 if ((byte_sz <= func->cur_blksize) && (byte_sz <= 512)) {
257 blk_sz = sz / func->cur_blksize;
258 return blk_sz * func->cur_blksize + byte_sz;
259 }
260 } else {
261 /*
262 * We need multiple requests, so first check that the
263 * controller can handle the chunk size;
264 */
265 chunk_sz = mmc_align_data_size(func->card,
266 min(func->cur_blksize, 512u));
267 if (chunk_sz == min(func->cur_blksize, 512u)) {
268 /*
269 * Fix up the size of the remainder (if any)
270 */
271 byte_sz = orig_sz % chunk_sz;
272 if (byte_sz) {
273 byte_sz = mmc_align_data_size(func->card,
274 byte_sz);
275 }
276
277 return (orig_sz / chunk_sz) * chunk_sz + byte_sz;
278 }
279 }
280
281 /*
282 * The controller is simply incapable of transferring the size
283 * we want in decent manner, so just return the original size.
284 */
285 return orig_sz;
286}
287EXPORT_SYMBOL_GPL(sdio_align_size);
288
David Vrabeleb659462007-08-08 14:24:21 +0100289/* Split an arbitrarily sized data transfer into several
290 * IO_RW_EXTENDED commands. */
291static int sdio_io_rw_ext_helper(struct sdio_func *func, int write,
292 unsigned addr, int incr_addr, u8 *buf, unsigned size)
293{
294 unsigned remainder = size;
295 unsigned max_blocks;
296 int ret;
297
298 /* Do the bulk of the transfer using block mode (if supported). */
299 if (func->card->cccr.multi_block) {
300 /* Blocks per command is limited by host count, host transfer
301 * size (we only use a single sg entry) and the maximum for
302 * IO_RW_EXTENDED of 511 blocks. */
303 max_blocks = min(min(
304 func->card->host->max_blk_count,
305 func->card->host->max_seg_size / func->cur_blksize),
306 511u);
307
308 while (remainder > func->cur_blksize) {
309 unsigned blocks;
310
311 blocks = remainder / func->cur_blksize;
312 if (blocks > max_blocks)
313 blocks = max_blocks;
314 size = blocks * func->cur_blksize;
315
316 ret = mmc_io_rw_extended(func->card, write,
317 func->num, addr, incr_addr, buf,
318 blocks, func->cur_blksize);
319 if (ret)
320 return ret;
321
322 remainder -= size;
323 buf += size;
324 if (incr_addr)
325 addr += size;
326 }
327 }
328
329 /* Write the remainder using byte mode. */
330 while (remainder > 0) {
331 size = remainder;
332 if (size > func->cur_blksize)
333 size = func->cur_blksize;
334 if (size > 512)
335 size = 512; /* maximum size for byte mode */
336
337 ret = mmc_io_rw_extended(func->card, write, func->num, addr,
338 incr_addr, buf, 1, size);
339 if (ret)
340 return ret;
341
342 remainder -= size;
343 buf += size;
344 if (incr_addr)
345 addr += size;
346 }
347 return 0;
348}
349
David Vrabel9a08f822007-08-08 14:23:48 +0100350/**
Pierre Ossman46f555f2007-05-27 12:57:15 +0200351 * sdio_readb - read a single byte from a SDIO function
352 * @func: SDIO function to access
353 * @addr: address to read
354 * @err_ret: optional status value from transfer
355 *
356 * Reads a single byte from the address space of a given SDIO
357 * function. If there is a problem reading the address, 0xff
358 * is returned and @err_ret will contain the error code.
359 */
360unsigned char sdio_readb(struct sdio_func *func, unsigned int addr,
361 int *err_ret)
362{
363 int ret;
364 unsigned char val;
365
366 BUG_ON(!func);
367
368 if (err_ret)
369 *err_ret = 0;
370
371 ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val);
372 if (ret) {
373 if (err_ret)
374 *err_ret = ret;
375 return 0xFF;
376 }
377
378 return val;
379}
380EXPORT_SYMBOL_GPL(sdio_readb);
381
382/**
383 * sdio_writeb - write a single byte to a SDIO function
384 * @func: SDIO function to access
385 * @b: byte to write
386 * @addr: address to write to
387 * @err_ret: optional status value from transfer
388 *
389 * Writes a single byte to the address space of a given SDIO
390 * function. @err_ret will contain the status of the actual
391 * transfer.
392 */
393void sdio_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
394 int *err_ret)
395{
396 int ret;
397
398 BUG_ON(!func);
399
400 ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL);
401 if (err_ret)
402 *err_ret = ret;
403}
404EXPORT_SYMBOL_GPL(sdio_writeb);
405
Pierre Ossman112c9db2007-07-06 13:35:01 +0200406/**
407 * sdio_memcpy_fromio - read a chunk of memory from a SDIO function
408 * @func: SDIO function to access
409 * @dst: buffer to store the data
410 * @addr: address to begin reading from
411 * @count: number of bytes to read
412 *
David Vrabeleb659462007-08-08 14:24:21 +0100413 * Reads from the address space of a given SDIO function. Return
414 * value indicates if the transfer succeeded or not.
Pierre Ossman112c9db2007-07-06 13:35:01 +0200415 */
416int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
417 unsigned int addr, int count)
418{
David Vrabeleb659462007-08-08 14:24:21 +0100419 return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count);
Pierre Ossman112c9db2007-07-06 13:35:01 +0200420}
421EXPORT_SYMBOL_GPL(sdio_memcpy_fromio);
422
423/**
424 * sdio_memcpy_toio - write a chunk of memory to a SDIO function
425 * @func: SDIO function to access
426 * @addr: address to start writing to
427 * @src: buffer that contains the data to write
428 * @count: number of bytes to write
429 *
David Vrabeleb659462007-08-08 14:24:21 +0100430 * Writes to the address space of a given SDIO function. Return
431 * value indicates if the transfer succeeded or not.
Pierre Ossman112c9db2007-07-06 13:35:01 +0200432 */
433int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
434 void *src, int count)
435{
David Vrabeleb659462007-08-08 14:24:21 +0100436 return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count);
Pierre Ossman112c9db2007-07-06 13:35:01 +0200437}
438EXPORT_SYMBOL_GPL(sdio_memcpy_toio);
439
440/**
441 * sdio_readsb - read from a FIFO on a SDIO function
442 * @func: SDIO function to access
443 * @dst: buffer to store the data
444 * @addr: address of (single byte) FIFO
445 * @count: number of bytes to read
446 *
David Vrabeleb659462007-08-08 14:24:21 +0100447 * Reads from the specified FIFO of a given SDIO function. Return
448 * value indicates if the transfer succeeded or not.
Pierre Ossman112c9db2007-07-06 13:35:01 +0200449 */
450int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
451 int count)
452{
David Vrabeleb659462007-08-08 14:24:21 +0100453 return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count);
Pierre Ossman112c9db2007-07-06 13:35:01 +0200454}
455
456EXPORT_SYMBOL_GPL(sdio_readsb);
457
458/**
459 * sdio_writesb - write to a FIFO of a SDIO function
460 * @func: SDIO function to access
461 * @addr: address of (single byte) FIFO
462 * @src: buffer that contains the data to write
463 * @count: number of bytes to write
464 *
David Vrabeleb659462007-08-08 14:24:21 +0100465 * Writes to the specified FIFO of a given SDIO function. Return
466 * value indicates if the transfer succeeded or not.
Pierre Ossman112c9db2007-07-06 13:35:01 +0200467 */
468int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,
469 int count)
470{
David Vrabeleb659462007-08-08 14:24:21 +0100471 return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count);
Pierre Ossman112c9db2007-07-06 13:35:01 +0200472}
473EXPORT_SYMBOL_GPL(sdio_writesb);
474
475/**
476 * sdio_readw - read a 16 bit integer from a SDIO function
477 * @func: SDIO function to access
478 * @addr: address to read
479 * @err_ret: optional status value from transfer
480 *
481 * Reads a 16 bit integer from the address space of a given SDIO
482 * function. If there is a problem reading the address, 0xffff
483 * is returned and @err_ret will contain the error code.
484 */
485unsigned short sdio_readw(struct sdio_func *func, unsigned int addr,
486 int *err_ret)
487{
488 int ret;
489
490 if (err_ret)
491 *err_ret = 0;
492
493 ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);
494 if (ret) {
495 if (err_ret)
496 *err_ret = ret;
497 return 0xFFFF;
498 }
499
500 return le16_to_cpu(*(u16*)func->tmpbuf);
501}
502EXPORT_SYMBOL_GPL(sdio_readw);
503
504/**
505 * sdio_writew - write a 16 bit integer to a SDIO function
506 * @func: SDIO function to access
507 * @b: integer to write
508 * @addr: address to write to
509 * @err_ret: optional status value from transfer
510 *
511 * Writes a 16 bit integer to the address space of a given SDIO
512 * function. @err_ret will contain the status of the actual
513 * transfer.
514 */
515void sdio_writew(struct sdio_func *func, unsigned short b, unsigned int addr,
516 int *err_ret)
517{
518 int ret;
519
520 *(u16*)func->tmpbuf = cpu_to_le16(b);
521
522 ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
523 if (err_ret)
524 *err_ret = ret;
525}
526EXPORT_SYMBOL_GPL(sdio_writew);
527
528/**
529 * sdio_readl - read a 32 bit integer from a SDIO function
530 * @func: SDIO function to access
531 * @addr: address to read
532 * @err_ret: optional status value from transfer
533 *
534 * Reads a 32 bit integer from the address space of a given SDIO
535 * function. If there is a problem reading the address,
536 * 0xffffffff is returned and @err_ret will contain the error
537 * code.
538 */
539unsigned long sdio_readl(struct sdio_func *func, unsigned int addr,
540 int *err_ret)
541{
542 int ret;
543
544 if (err_ret)
545 *err_ret = 0;
546
547 ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4);
548 if (ret) {
549 if (err_ret)
550 *err_ret = ret;
551 return 0xFFFFFFFF;
552 }
553
554 return le32_to_cpu(*(u32*)func->tmpbuf);
555}
556EXPORT_SYMBOL_GPL(sdio_readl);
557
558/**
559 * sdio_writel - write a 32 bit integer to a SDIO function
560 * @func: SDIO function to access
561 * @b: integer to write
562 * @addr: address to write to
563 * @err_ret: optional status value from transfer
564 *
565 * Writes a 32 bit integer to the address space of a given SDIO
566 * function. @err_ret will contain the status of the actual
567 * transfer.
568 */
569void sdio_writel(struct sdio_func *func, unsigned long b, unsigned int addr,
570 int *err_ret)
571{
572 int ret;
573
574 *(u32*)func->tmpbuf = cpu_to_le32(b);
575
576 ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
577 if (err_ret)
578 *err_ret = ret;
579}
580EXPORT_SYMBOL_GPL(sdio_writel);
581
David Vrabel7806cdb2007-08-10 13:29:46 +0100582/**
583 * sdio_f0_readb - read a single byte from SDIO function 0
584 * @func: an SDIO function of the card
585 * @addr: address to read
586 * @err_ret: optional status value from transfer
587 *
588 * Reads a single byte from the address space of SDIO function 0.
589 * If there is a problem reading the address, 0xff is returned
590 * and @err_ret will contain the error code.
591 */
592unsigned char sdio_f0_readb(struct sdio_func *func, unsigned int addr,
593 int *err_ret)
594{
595 int ret;
596 unsigned char val;
597
598 BUG_ON(!func);
599
600 if (err_ret)
601 *err_ret = 0;
602
603 ret = mmc_io_rw_direct(func->card, 0, 0, addr, 0, &val);
604 if (ret) {
605 if (err_ret)
606 *err_ret = ret;
607 return 0xFF;
608 }
609
610 return val;
611}
612EXPORT_SYMBOL_GPL(sdio_f0_readb);
613
614/**
615 * sdio_f0_writeb - write a single byte to SDIO function 0
616 * @func: an SDIO function of the card
617 * @b: byte to write
618 * @addr: address to write to
619 * @err_ret: optional status value from transfer
620 *
621 * Writes a single byte to the address space of SDIO function 0.
622 * @err_ret will contain the status of the actual transfer.
623 *
624 * Only writes to the vendor specific CCCR registers (0xF0 -
625 * 0xFF) are permiited; @err_ret will be set to -EINVAL for *
626 * writes outside this range.
627 */
628void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
629 int *err_ret)
630{
631 int ret;
632
633 BUG_ON(!func);
634
635 if (addr < 0xF0 || addr > 0xFF) {
636 if (err_ret)
637 *err_ret = -EINVAL;
638 return;
639 }
640
641 ret = mmc_io_rw_direct(func->card, 1, 0, addr, b, NULL);
642 if (err_ret)
643 *err_ret = ret;
644}
645EXPORT_SYMBOL_GPL(sdio_f0_writeb);