sdio: clean up handling of byte mode transfer size
[safe/jmp/linux-2.6] / drivers / mmc / core / sdio_io.c
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  */
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  */
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  */
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         /*
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
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  */
113 int 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
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  */
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(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 EXPORT_SYMBOL_GPL(sdio_set_block_size);
190
191 /*
192  * Calculate the maximum byte mode transfer size
193  */
194 static inline unsigned int sdio_max_byte_size(struct sdio_func *func)
195 {
196         return min(min(min(
197                 func->card->host->max_seg_size,
198                 func->card->host->max_blk_size),
199                 func->max_blksize),
200                 512u); /* maximum size for byte mode */
201 }
202
203 /**
204  *      sdio_align_size - pads a transfer size to a more optimal value
205  *      @func: SDIO function
206  *      @sz: original transfer size
207  *
208  *      Pads the original data size with a number of extra bytes in
209  *      order to avoid controller bugs and/or performance hits
210  *      (e.g. some controllers revert to PIO for certain sizes).
211  *
212  *      If possible, it will also adjust the size so that it can be
213  *      handled in just a single request.
214  *
215  *      Returns the improved size, which might be unmodified.
216  */
217 unsigned int sdio_align_size(struct sdio_func *func, unsigned int sz)
218 {
219         unsigned int orig_sz;
220         unsigned int blk_sz, byte_sz;
221         unsigned chunk_sz;
222
223         orig_sz = sz;
224
225         /*
226          * Do a first check with the controller, in case it
227          * wants to increase the size up to a point where it
228          * might need more than one block.
229          */
230         sz = mmc_align_data_size(func->card, sz);
231
232         /*
233          * If we can still do this with just a byte transfer, then
234          * we're done.
235          */
236         if (sz <= sdio_max_byte_size(func))
237                 return sz;
238
239         if (func->card->cccr.multi_block) {
240                 /*
241                  * Check if the transfer is already block aligned
242                  */
243                 if ((sz % func->cur_blksize) == 0)
244                         return sz;
245
246                 /*
247                  * Realign it so that it can be done with one request,
248                  * and recheck if the controller still likes it.
249                  */
250                 blk_sz = ((sz + func->cur_blksize - 1) /
251                         func->cur_blksize) * func->cur_blksize;
252                 blk_sz = mmc_align_data_size(func->card, blk_sz);
253
254                 /*
255                  * This value is only good if it is still just
256                  * one request.
257                  */
258                 if ((blk_sz % func->cur_blksize) == 0)
259                         return blk_sz;
260
261                 /*
262                  * We failed to do one request, but at least try to
263                  * pad the remainder properly.
264                  */
265                 byte_sz = mmc_align_data_size(func->card,
266                                 sz % func->cur_blksize);
267                 if (byte_sz <= sdio_max_byte_size(func)) {
268                         blk_sz = sz / func->cur_blksize;
269                         return blk_sz * func->cur_blksize + byte_sz;
270                 }
271         } else {
272                 /*
273                  * We need multiple requests, so first check that the
274                  * controller can handle the chunk size;
275                  */
276                 chunk_sz = mmc_align_data_size(func->card,
277                                 sdio_max_byte_size(func));
278                 if (chunk_sz == sdio_max_byte_size(func)) {
279                         /*
280                          * Fix up the size of the remainder (if any)
281                          */
282                         byte_sz = orig_sz % chunk_sz;
283                         if (byte_sz) {
284                                 byte_sz = mmc_align_data_size(func->card,
285                                                 byte_sz);
286                         }
287
288                         return (orig_sz / chunk_sz) * chunk_sz + byte_sz;
289                 }
290         }
291
292         /*
293          * The controller is simply incapable of transferring the size
294          * we want in decent manner, so just return the original size.
295          */
296         return orig_sz;
297 }
298 EXPORT_SYMBOL_GPL(sdio_align_size);
299
300 /* Split an arbitrarily sized data transfer into several
301  * IO_RW_EXTENDED commands. */
302 static int sdio_io_rw_ext_helper(struct sdio_func *func, int write,
303         unsigned addr, int incr_addr, u8 *buf, unsigned size)
304 {
305         unsigned remainder = size;
306         unsigned max_blocks;
307         int ret;
308
309         /* Do the bulk of the transfer using block mode (if supported). */
310         if (func->card->cccr.multi_block && (size > sdio_max_byte_size(func))) {
311                 /* Blocks per command is limited by host count, host transfer
312                  * size (we only use a single sg entry) and the maximum for
313                  * IO_RW_EXTENDED of 511 blocks. */
314                 max_blocks = min(min(
315                         func->card->host->max_blk_count,
316                         func->card->host->max_seg_size / func->cur_blksize),
317                         511u);
318
319                 while (remainder > func->cur_blksize) {
320                         unsigned blocks;
321
322                         blocks = remainder / func->cur_blksize;
323                         if (blocks > max_blocks)
324                                 blocks = max_blocks;
325                         size = blocks * func->cur_blksize;
326
327                         ret = mmc_io_rw_extended(func->card, write,
328                                 func->num, addr, incr_addr, buf,
329                                 blocks, func->cur_blksize);
330                         if (ret)
331                                 return ret;
332
333                         remainder -= size;
334                         buf += size;
335                         if (incr_addr)
336                                 addr += size;
337                 }
338         }
339
340         /* Write the remainder using byte mode. */
341         while (remainder > 0) {
342                 size = min(remainder, sdio_max_byte_size(func));
343
344                 ret = mmc_io_rw_extended(func->card, write, func->num, addr,
345                          incr_addr, buf, 1, size);
346                 if (ret)
347                         return ret;
348
349                 remainder -= size;
350                 buf += size;
351                 if (incr_addr)
352                         addr += size;
353         }
354         return 0;
355 }
356
357 /**
358  *      sdio_readb - read a single byte from a SDIO function
359  *      @func: SDIO function to access
360  *      @addr: address to read
361  *      @err_ret: optional status value from transfer
362  *
363  *      Reads a single byte from the address space of a given SDIO
364  *      function. If there is a problem reading the address, 0xff
365  *      is returned and @err_ret will contain the error code.
366  */
367 unsigned char sdio_readb(struct sdio_func *func, unsigned int addr,
368         int *err_ret)
369 {
370         int ret;
371         unsigned char val;
372
373         BUG_ON(!func);
374
375         if (err_ret)
376                 *err_ret = 0;
377
378         ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val);
379         if (ret) {
380                 if (err_ret)
381                         *err_ret = ret;
382                 return 0xFF;
383         }
384
385         return val;
386 }
387 EXPORT_SYMBOL_GPL(sdio_readb);
388
389 /**
390  *      sdio_writeb - write a single byte to a SDIO function
391  *      @func: SDIO function to access
392  *      @b: byte to write
393  *      @addr: address to write to
394  *      @err_ret: optional status value from transfer
395  *
396  *      Writes a single byte to the address space of a given SDIO
397  *      function. @err_ret will contain the status of the actual
398  *      transfer.
399  */
400 void sdio_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
401         int *err_ret)
402 {
403         int ret;
404
405         BUG_ON(!func);
406
407         ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL);
408         if (err_ret)
409                 *err_ret = ret;
410 }
411 EXPORT_SYMBOL_GPL(sdio_writeb);
412
413 /**
414  *      sdio_memcpy_fromio - read a chunk of memory from a SDIO function
415  *      @func: SDIO function to access
416  *      @dst: buffer to store the data
417  *      @addr: address to begin reading from
418  *      @count: number of bytes to read
419  *
420  *      Reads from the address space of a given SDIO function. Return
421  *      value indicates if the transfer succeeded or not.
422  */
423 int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
424         unsigned int addr, int count)
425 {
426         return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count);
427 }
428 EXPORT_SYMBOL_GPL(sdio_memcpy_fromio);
429
430 /**
431  *      sdio_memcpy_toio - write a chunk of memory to a SDIO function
432  *      @func: SDIO function to access
433  *      @addr: address to start writing to
434  *      @src: buffer that contains the data to write
435  *      @count: number of bytes to write
436  *
437  *      Writes to the address space of a given SDIO function. Return
438  *      value indicates if the transfer succeeded or not.
439  */
440 int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
441         void *src, int count)
442 {
443         return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count);
444 }
445 EXPORT_SYMBOL_GPL(sdio_memcpy_toio);
446
447 /**
448  *      sdio_readsb - read from a FIFO on a SDIO function
449  *      @func: SDIO function to access
450  *      @dst: buffer to store the data
451  *      @addr: address of (single byte) FIFO
452  *      @count: number of bytes to read
453  *
454  *      Reads from the specified FIFO of a given SDIO function. Return
455  *      value indicates if the transfer succeeded or not.
456  */
457 int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
458         int count)
459 {
460         return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count);
461 }
462
463 EXPORT_SYMBOL_GPL(sdio_readsb);
464
465 /**
466  *      sdio_writesb - write to a FIFO of a SDIO function
467  *      @func: SDIO function to access
468  *      @addr: address of (single byte) FIFO
469  *      @src: buffer that contains the data to write
470  *      @count: number of bytes to write
471  *
472  *      Writes to the specified FIFO of a given SDIO function. Return
473  *      value indicates if the transfer succeeded or not.
474  */
475 int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,
476         int count)
477 {
478         return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count);
479 }
480 EXPORT_SYMBOL_GPL(sdio_writesb);
481
482 /**
483  *      sdio_readw - read a 16 bit integer from a SDIO function
484  *      @func: SDIO function to access
485  *      @addr: address to read
486  *      @err_ret: optional status value from transfer
487  *
488  *      Reads a 16 bit integer from the address space of a given SDIO
489  *      function. If there is a problem reading the address, 0xffff
490  *      is returned and @err_ret will contain the error code.
491  */
492 unsigned short sdio_readw(struct sdio_func *func, unsigned int addr,
493         int *err_ret)
494 {
495         int ret;
496
497         if (err_ret)
498                 *err_ret = 0;
499
500         ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);
501         if (ret) {
502                 if (err_ret)
503                         *err_ret = ret;
504                 return 0xFFFF;
505         }
506
507         return le16_to_cpu(*(u16*)func->tmpbuf);
508 }
509 EXPORT_SYMBOL_GPL(sdio_readw);
510
511 /**
512  *      sdio_writew - write a 16 bit integer to a SDIO function
513  *      @func: SDIO function to access
514  *      @b: integer to write
515  *      @addr: address to write to
516  *      @err_ret: optional status value from transfer
517  *
518  *      Writes a 16 bit integer to the address space of a given SDIO
519  *      function. @err_ret will contain the status of the actual
520  *      transfer.
521  */
522 void sdio_writew(struct sdio_func *func, unsigned short b, unsigned int addr,
523         int *err_ret)
524 {
525         int ret;
526
527         *(u16*)func->tmpbuf = cpu_to_le16(b);
528
529         ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
530         if (err_ret)
531                 *err_ret = ret;
532 }
533 EXPORT_SYMBOL_GPL(sdio_writew);
534
535 /**
536  *      sdio_readl - read a 32 bit integer from a SDIO function
537  *      @func: SDIO function to access
538  *      @addr: address to read
539  *      @err_ret: optional status value from transfer
540  *
541  *      Reads a 32 bit integer from the address space of a given SDIO
542  *      function. If there is a problem reading the address,
543  *      0xffffffff is returned and @err_ret will contain the error
544  *      code.
545  */
546 unsigned long sdio_readl(struct sdio_func *func, unsigned int addr,
547         int *err_ret)
548 {
549         int ret;
550
551         if (err_ret)
552                 *err_ret = 0;
553
554         ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4);
555         if (ret) {
556                 if (err_ret)
557                         *err_ret = ret;
558                 return 0xFFFFFFFF;
559         }
560
561         return le32_to_cpu(*(u32*)func->tmpbuf);
562 }
563 EXPORT_SYMBOL_GPL(sdio_readl);
564
565 /**
566  *      sdio_writel - write a 32 bit integer to a SDIO function
567  *      @func: SDIO function to access
568  *      @b: integer to write
569  *      @addr: address to write to
570  *      @err_ret: optional status value from transfer
571  *
572  *      Writes a 32 bit integer to the address space of a given SDIO
573  *      function. @err_ret will contain the status of the actual
574  *      transfer.
575  */
576 void sdio_writel(struct sdio_func *func, unsigned long b, unsigned int addr,
577         int *err_ret)
578 {
579         int ret;
580
581         *(u32*)func->tmpbuf = cpu_to_le32(b);
582
583         ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
584         if (err_ret)
585                 *err_ret = ret;
586 }
587 EXPORT_SYMBOL_GPL(sdio_writel);
588
589 /**
590  *      sdio_f0_readb - read a single byte from SDIO function 0
591  *      @func: an SDIO function of the card
592  *      @addr: address to read
593  *      @err_ret: optional status value from transfer
594  *
595  *      Reads a single byte from the address space of SDIO function 0.
596  *      If there is a problem reading the address, 0xff is returned
597  *      and @err_ret will contain the error code.
598  */
599 unsigned char sdio_f0_readb(struct sdio_func *func, unsigned int addr,
600         int *err_ret)
601 {
602         int ret;
603         unsigned char val;
604
605         BUG_ON(!func);
606
607         if (err_ret)
608                 *err_ret = 0;
609
610         ret = mmc_io_rw_direct(func->card, 0, 0, addr, 0, &val);
611         if (ret) {
612                 if (err_ret)
613                         *err_ret = ret;
614                 return 0xFF;
615         }
616
617         return val;
618 }
619 EXPORT_SYMBOL_GPL(sdio_f0_readb);
620
621 /**
622  *      sdio_f0_writeb - write a single byte to SDIO function 0
623  *      @func: an SDIO function of the card
624  *      @b: byte to write
625  *      @addr: address to write to
626  *      @err_ret: optional status value from transfer
627  *
628  *      Writes a single byte to the address space of SDIO function 0.
629  *      @err_ret will contain the status of the actual transfer.
630  *
631  *      Only writes to the vendor specific CCCR registers (0xF0 -
632  *      0xFF) are permiited; @err_ret will be set to -EINVAL for *
633  *      writes outside this range.
634  */
635 void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
636         int *err_ret)
637 {
638         int ret;
639
640         BUG_ON(!func);
641
642         if (addr < 0xF0 || addr > 0xFF) {
643                 if (err_ret)
644                         *err_ret = -EINVAL;
645                 return;
646         }
647
648         ret = mmc_io_rw_direct(func->card, 1, 0, addr, b, NULL);
649         if (err_ret)
650                 *err_ret = ret;
651 }
652 EXPORT_SYMBOL_GPL(sdio_f0_writeb);