sdio_ops.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * linux/drivers/mmc/sdio_ops.c
  3. *
  4. * Copyright 2006-2007 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. #include <linux/scatterlist.h>
  12. #include <linux/mmc/host.h>
  13. #include <linux/mmc/card.h>
  14. #include <linux/mmc/mmc.h>
  15. #include <linux/mmc/sdio.h>
  16. #include "core.h"
  17. #include "sdio_ops.h"
  18. int mmc_send_io_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
  19. {
  20. struct mmc_command cmd = {0};
  21. int i, err = 0;
  22. BUG_ON(!host);
  23. cmd.opcode = SD_IO_SEND_OP_COND;
  24. cmd.arg = ocr;
  25. cmd.flags = MMC_RSP_SPI_R4 | MMC_RSP_R4 | MMC_CMD_BCR;
  26. for (i = 100; i; i--) {
  27. err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
  28. if (err)
  29. break;
  30. /* if we're just probing, do a single pass */
  31. if (ocr == 0)
  32. break;
  33. /* otherwise wait until reset completes */
  34. if (mmc_host_is_spi(host)) {
  35. /*
  36. * Both R1_SPI_IDLE and MMC_CARD_BUSY indicate
  37. * an initialized card under SPI, but some cards
  38. * (Marvell's) only behave when looking at this
  39. * one.
  40. */
  41. if (cmd.resp[1] & MMC_CARD_BUSY)
  42. break;
  43. } else {
  44. if (cmd.resp[0] & MMC_CARD_BUSY)
  45. break;
  46. }
  47. err = -ETIMEDOUT;
  48. mmc_delay(10);
  49. }
  50. if (rocr)
  51. *rocr = cmd.resp[mmc_host_is_spi(host) ? 1 : 0];
  52. return err;
  53. }
  54. static int mmc_io_rw_direct_host(struct mmc_host *host, int write, unsigned fn,
  55. unsigned addr, u8 in, u8 *out)
  56. {
  57. struct mmc_command cmd = {0};
  58. int err;
  59. BUG_ON(!host);
  60. BUG_ON(fn > 7);
  61. /* sanity check */
  62. if (addr & ~0x1FFFF)
  63. return -EINVAL;
  64. cmd.opcode = SD_IO_RW_DIRECT;
  65. cmd.arg = write ? 0x80000000 : 0x00000000;
  66. cmd.arg |= fn << 28;
  67. cmd.arg |= (write && out) ? 0x08000000 : 0x00000000;
  68. cmd.arg |= addr << 9;
  69. cmd.arg |= in;
  70. cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
  71. err = mmc_wait_for_cmd(host, &cmd, 0);
  72. if (err)
  73. return err;
  74. if (mmc_host_is_spi(host)) {
  75. /* host driver already reported errors */
  76. } else {
  77. if (cmd.resp[0] & R5_ERROR)
  78. return -EIO;
  79. if (cmd.resp[0] & R5_FUNCTION_NUMBER)
  80. return -EINVAL;
  81. if (cmd.resp[0] & R5_OUT_OF_RANGE)
  82. return -ERANGE;
  83. }
  84. if (out) {
  85. if (mmc_host_is_spi(host))
  86. *out = (cmd.resp[0] >> 8) & 0xFF;
  87. else
  88. *out = cmd.resp[0] & 0xFF;
  89. }
  90. return 0;
  91. }
  92. int mmc_io_rw_direct(struct mmc_card *card, int write, unsigned fn,
  93. unsigned addr, u8 in, u8 *out)
  94. {
  95. BUG_ON(!card);
  96. return mmc_io_rw_direct_host(card->host, write, fn, addr, in, out);
  97. }
  98. int mmc_io_rw_extended(struct mmc_card *card, int write, unsigned fn,
  99. unsigned addr, int incr_addr, u8 *buf, unsigned blocks, unsigned blksz)
  100. {
  101. struct mmc_request mrq = {NULL};
  102. struct mmc_command cmd = {0};
  103. struct mmc_data data = {0};
  104. struct scatterlist sg, *sg_ptr;
  105. struct sg_table sgtable;
  106. unsigned int nents, left_size, i;
  107. unsigned int seg_size = card->host->max_seg_size;
  108. BUG_ON(!card);
  109. BUG_ON(fn > 7);
  110. WARN_ON(blksz == 0);
  111. /* sanity check */
  112. if (addr & ~0x1FFFF)
  113. return -EINVAL;
  114. mrq.cmd = &cmd;
  115. mrq.data = &data;
  116. cmd.opcode = SD_IO_RW_EXTENDED;
  117. cmd.arg = write ? 0x80000000 : 0x00000000;
  118. cmd.arg |= fn << 28;
  119. cmd.arg |= incr_addr ? 0x04000000 : 0x00000000;
  120. cmd.arg |= addr << 9;
  121. if (blocks == 0)
  122. cmd.arg |= (blksz == 512) ? 0 : blksz; /* byte mode */
  123. else
  124. cmd.arg |= 0x08000000 | blocks; /* block mode */
  125. cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
  126. data.blksz = blksz;
  127. /* Code in host drivers/fwk assumes that "blocks" always is >=1 */
  128. data.blocks = blocks ? blocks : 1;
  129. data.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
  130. left_size = data.blksz * data.blocks;
  131. nents = (left_size - 1) / seg_size + 1;
  132. if (nents > 1) {
  133. if (sg_alloc_table(&sgtable, nents, GFP_KERNEL))
  134. return -ENOMEM;
  135. data.sg = sgtable.sgl;
  136. data.sg_len = nents;
  137. for_each_sg(data.sg, sg_ptr, data.sg_len, i) {
  138. sg_set_page(sg_ptr, virt_to_page(buf + (i * seg_size)),
  139. min(seg_size, left_size),
  140. offset_in_page(buf + (i * seg_size)));
  141. left_size = left_size - seg_size;
  142. }
  143. } else {
  144. data.sg = &sg;
  145. data.sg_len = 1;
  146. sg_init_one(&sg, buf, left_size);
  147. }
  148. mmc_set_data_timeout(&data, card);
  149. mmc_wait_for_req(card->host, &mrq);
  150. if (nents > 1)
  151. sg_free_table(&sgtable);
  152. if (cmd.error)
  153. return cmd.error;
  154. if (data.error)
  155. return data.error;
  156. if (mmc_host_is_spi(card->host)) {
  157. /* host driver already reported errors */
  158. } else {
  159. if (cmd.resp[0] & R5_ERROR)
  160. return -EIO;
  161. if (cmd.resp[0] & R5_FUNCTION_NUMBER)
  162. return -EINVAL;
  163. if (cmd.resp[0] & R5_OUT_OF_RANGE)
  164. return -ERANGE;
  165. }
  166. return 0;
  167. }
  168. int sdio_reset(struct mmc_host *host)
  169. {
  170. int ret;
  171. u8 abort;
  172. /* SDIO Simplified Specification V2.0, 4.4 Reset for SDIO */
  173. ret = mmc_io_rw_direct_host(host, 0, 0, SDIO_CCCR_ABORT, 0, &abort);
  174. if (ret)
  175. abort = 0x08;
  176. else
  177. abort |= 0x08;
  178. ret = mmc_io_rw_direct_host(host, 1, 0, SDIO_CCCR_ABORT, abort, NULL);
  179. return ret;
  180. }