sdio_ops.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 = {0};
  102. struct mmc_command cmd = {0};
  103. struct mmc_data data = {0};
  104. struct scatterlist sg;
  105. BUG_ON(!card);
  106. BUG_ON(fn > 7);
  107. BUG_ON(blocks == 1 && blksz > 512);
  108. WARN_ON(blocks == 0);
  109. WARN_ON(blksz == 0);
  110. /* sanity check */
  111. if (addr & ~0x1FFFF)
  112. return -EINVAL;
  113. mrq.cmd = &cmd;
  114. mrq.data = &data;
  115. cmd.opcode = SD_IO_RW_EXTENDED;
  116. cmd.arg = write ? 0x80000000 : 0x00000000;
  117. cmd.arg |= fn << 28;
  118. cmd.arg |= incr_addr ? 0x04000000 : 0x00000000;
  119. cmd.arg |= addr << 9;
  120. if (blocks == 1 && blksz <= 512)
  121. cmd.arg |= (blksz == 512) ? 0 : blksz; /* byte mode */
  122. else
  123. cmd.arg |= 0x08000000 | blocks; /* block mode */
  124. cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
  125. data.blksz = blksz;
  126. data.blocks = blocks;
  127. data.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
  128. data.sg = &sg;
  129. data.sg_len = 1;
  130. sg_init_one(&sg, buf, blksz * blocks);
  131. mmc_set_data_timeout(&data, card);
  132. mmc_wait_for_req(card->host, &mrq);
  133. if (cmd.error)
  134. return cmd.error;
  135. if (data.error)
  136. return data.error;
  137. if (mmc_host_is_spi(card->host)) {
  138. /* host driver already reported errors */
  139. } else {
  140. if (cmd.resp[0] & R5_ERROR)
  141. return -EIO;
  142. if (cmd.resp[0] & R5_FUNCTION_NUMBER)
  143. return -EINVAL;
  144. if (cmd.resp[0] & R5_OUT_OF_RANGE)
  145. return -ERANGE;
  146. }
  147. return 0;
  148. }
  149. int sdio_reset(struct mmc_host *host)
  150. {
  151. int ret;
  152. u8 abort;
  153. /* SDIO Simplified Specification V2.0, 4.4 Reset for SDIO */
  154. ret = mmc_io_rw_direct_host(host, 0, 0, SDIO_CCCR_ABORT, 0, &abort);
  155. if (ret)
  156. abort = 0x08;
  157. else
  158. abort |= 0x08;
  159. ret = mmc_io_rw_direct_host(host, 1, 0, SDIO_CCCR_ABORT, abort, NULL);
  160. return ret;
  161. }