Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sdio_io.c
Go to the documentation of this file.
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 
28 {
29  BUG_ON(!func);
30  BUG_ON(!func->card);
31 
32  mmc_claim_host(func->card->host);
33 }
35 
44 {
45  BUG_ON(!func);
46  BUG_ON(!func->card);
47 
48  mmc_release_host(func->card->host);
49 }
51 
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 }
102 
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 }
139 
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,
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 }
185 
186 /*
187  * Calculate the maximum byte mode transfer size
188  */
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 
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 }
301 
302 /* Split an arbitrarily sized data transfer into several
303  * IO_RW_EXTENDED commands. */
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 
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 }
389 
401 void sdio_writeb(struct sdio_func *func, u8 b, unsigned int addr, 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 }
412 
426 u8 sdio_writeb_readb(struct sdio_func *func, u8 write_byte,
427  unsigned int addr, int *err_ret)
428 {
429  int ret;
430  u8 val;
431 
432  ret = mmc_io_rw_direct(func->card, 1, func->num, addr,
433  write_byte, &val);
434  if (err_ret)
435  *err_ret = ret;
436  if (ret)
437  val = 0xff;
438 
439  return val;
440 }
442 
453 int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
454  unsigned int addr, int count)
455 {
456  return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count);
457 }
459 
470 int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
471  void *src, int count)
472 {
473  return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count);
474 }
476 
487 int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
488  int count)
489 {
490  return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count);
491 }
493 
504 int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,
505  int count)
506 {
507  return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count);
508 }
510 
521 u16 sdio_readw(struct sdio_func *func, unsigned int addr, int *err_ret)
522 {
523  int ret;
524 
525  if (err_ret)
526  *err_ret = 0;
527 
528  ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);
529  if (ret) {
530  if (err_ret)
531  *err_ret = ret;
532  return 0xFFFF;
533  }
534 
535  return le16_to_cpup((__le16 *)func->tmpbuf);
536 }
538 
550 void sdio_writew(struct sdio_func *func, u16 b, unsigned int addr, int *err_ret)
551 {
552  int ret;
553 
554  *(__le16 *)func->tmpbuf = cpu_to_le16(b);
555 
556  ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
557  if (err_ret)
558  *err_ret = ret;
559 }
561 
573 u32 sdio_readl(struct sdio_func *func, unsigned int addr, int *err_ret)
574 {
575  int ret;
576 
577  if (err_ret)
578  *err_ret = 0;
579 
580  ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4);
581  if (ret) {
582  if (err_ret)
583  *err_ret = ret;
584  return 0xFFFFFFFF;
585  }
586 
587  return le32_to_cpup((__le32 *)func->tmpbuf);
588 }
590 
602 void sdio_writel(struct sdio_func *func, u32 b, unsigned int addr, int *err_ret)
603 {
604  int ret;
605 
606  *(__le32 *)func->tmpbuf = cpu_to_le32(b);
607 
608  ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
609  if (err_ret)
610  *err_ret = ret;
611 }
613 
624 unsigned char sdio_f0_readb(struct sdio_func *func, unsigned int addr,
625  int *err_ret)
626 {
627  int ret;
628  unsigned char val;
629 
630  BUG_ON(!func);
631 
632  if (err_ret)
633  *err_ret = 0;
634 
635  ret = mmc_io_rw_direct(func->card, 0, 0, addr, 0, &val);
636  if (ret) {
637  if (err_ret)
638  *err_ret = ret;
639  return 0xFF;
640  }
641 
642  return val;
643 }
645 
660 void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
661  int *err_ret)
662 {
663  int ret;
664 
665  BUG_ON(!func);
666 
667  if ((addr < 0xF0 || addr > 0xFF) && (!mmc_card_lenient_fn0(func->card))) {
668  if (err_ret)
669  *err_ret = -EINVAL;
670  return;
671  }
672 
673  ret = mmc_io_rw_direct(func->card, 1, 0, addr, b, NULL);
674  if (err_ret)
675  *err_ret = ret;
676 }
678 
690 {
691  BUG_ON(!func);
692  BUG_ON(!func->card);
693 
694  return func->card->host->pm_caps;
695 }
697 
711 {
712  struct mmc_host *host;
713 
714  BUG_ON(!func);
715  BUG_ON(!func->card);
716 
717  host = func->card->host;
718 
719  if (flags & ~host->pm_caps)
720  return -EINVAL;
721 
722  /* function suspend methods are serialized, hence no lock needed */
723  host->pm_flags |= flags;
724  return 0;
725 }