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