1 /* 2 * linux/drivers/mmc/core/mmc_ops.h 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/types.h> 13 #include <asm/scatterlist.h> 14 #include <linux/scatterlist.h> 15 16 #include <linux/mmc/host.h> 17 #include <linux/mmc/card.h> 18 #include <linux/mmc/mmc.h> 19 20 #include "core.h" 21 #include "mmc_ops.h" 22 23 static int _mmc_select_card(struct mmc_host *host, struct mmc_card *card) 24 { 25 int err; 26 struct mmc_command cmd; 27 28 BUG_ON(!host); 29 30 memset(&cmd, 0, sizeof(struct mmc_command)); 31 32 cmd.opcode = MMC_SELECT_CARD; 33 34 if (card) { 35 cmd.arg = card->rca << 16; 36 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 37 } else { 38 cmd.arg = 0; 39 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC; 40 } 41 42 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES); 43 if (err) 44 return err; 45 46 return 0; 47 } 48 49 int mmc_select_card(struct mmc_card *card) 50 { 51 BUG_ON(!card); 52 53 return _mmc_select_card(card->host, card); 54 } 55 56 int mmc_deselect_cards(struct mmc_host *host) 57 { 58 return _mmc_select_card(host, NULL); 59 } 60 61 int mmc_go_idle(struct mmc_host *host) 62 { 63 int err; 64 struct mmc_command cmd; 65 66 /* 67 * Non-SPI hosts need to prevent chipselect going active during 68 * GO_IDLE; that would put chips into SPI mode. Remind them of 69 * that in case of hardware that won't pull up DAT3/nCS otherwise. 70 * 71 * SPI hosts ignore ios.chip_select; it's managed according to 72 * rules that must accomodate non-MMC slaves which this layer 73 * won't even know about. 74 */ 75 if (!mmc_host_is_spi(host)) { 76 mmc_set_chip_select(host, MMC_CS_HIGH); 77 mmc_delay(1); 78 } 79 80 memset(&cmd, 0, sizeof(struct mmc_command)); 81 82 cmd.opcode = MMC_GO_IDLE_STATE; 83 cmd.arg = 0; 84 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC; 85 86 err = mmc_wait_for_cmd(host, &cmd, 0); 87 88 mmc_delay(1); 89 90 if (!mmc_host_is_spi(host)) { 91 mmc_set_chip_select(host, MMC_CS_DONTCARE); 92 mmc_delay(1); 93 } 94 95 host->use_spi_crc = 0; 96 97 return err; 98 } 99 100 int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr) 101 { 102 struct mmc_command cmd; 103 int i, err = 0; 104 105 BUG_ON(!host); 106 107 memset(&cmd, 0, sizeof(struct mmc_command)); 108 109 cmd.opcode = MMC_SEND_OP_COND; 110 cmd.arg = mmc_host_is_spi(host) ? 0 : ocr; 111 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR; 112 113 for (i = 100; i; i--) { 114 err = mmc_wait_for_cmd(host, &cmd, 0); 115 if (err) 116 break; 117 118 /* if we're just probing, do a single pass */ 119 if (ocr == 0) 120 break; 121 122 /* otherwise wait until reset completes */ 123 if (mmc_host_is_spi(host)) { 124 if (!(cmd.resp[0] & R1_SPI_IDLE)) 125 break; 126 } else { 127 if (cmd.resp[0] & MMC_CARD_BUSY) 128 break; 129 } 130 131 err = -ETIMEDOUT; 132 133 mmc_delay(10); 134 } 135 136 if (rocr && !mmc_host_is_spi(host)) 137 *rocr = cmd.resp[0]; 138 139 return err; 140 } 141 142 int mmc_all_send_cid(struct mmc_host *host, u32 *cid) 143 { 144 int err; 145 struct mmc_command cmd; 146 147 BUG_ON(!host); 148 BUG_ON(!cid); 149 150 memset(&cmd, 0, sizeof(struct mmc_command)); 151 152 cmd.opcode = MMC_ALL_SEND_CID; 153 cmd.arg = 0; 154 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; 155 156 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES); 157 if (err) 158 return err; 159 160 memcpy(cid, cmd.resp, sizeof(u32) * 4); 161 162 return 0; 163 } 164 165 int mmc_set_relative_addr(struct mmc_card *card) 166 { 167 int err; 168 struct mmc_command cmd; 169 170 BUG_ON(!card); 171 BUG_ON(!card->host); 172 173 memset(&cmd, 0, sizeof(struct mmc_command)); 174 175 cmd.opcode = MMC_SET_RELATIVE_ADDR; 176 cmd.arg = card->rca << 16; 177 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 178 179 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES); 180 if (err) 181 return err; 182 183 return 0; 184 } 185 186 static int 187 mmc_send_cxd_native(struct mmc_host *host, u32 arg, u32 *cxd, int opcode) 188 { 189 int err; 190 struct mmc_command cmd; 191 192 BUG_ON(!host); 193 BUG_ON(!cxd); 194 195 memset(&cmd, 0, sizeof(struct mmc_command)); 196 197 cmd.opcode = opcode; 198 cmd.arg = arg; 199 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC; 200 201 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES); 202 if (err) 203 return err; 204 205 memcpy(cxd, cmd.resp, sizeof(u32) * 4); 206 207 return 0; 208 } 209 210 static int 211 mmc_send_cxd_data(struct mmc_card *card, struct mmc_host *host, 212 u32 opcode, void *buf, unsigned len) 213 { 214 struct mmc_request mrq; 215 struct mmc_command cmd; 216 struct mmc_data data; 217 struct scatterlist sg; 218 void *data_buf; 219 220 /* dma onto stack is unsafe/nonportable, but callers to this 221 * routine normally provide temporary on-stack buffers ... 222 */ 223 data_buf = kmalloc(len, GFP_KERNEL); 224 if (data_buf == NULL) 225 return -ENOMEM; 226 227 memset(&mrq, 0, sizeof(struct mmc_request)); 228 memset(&cmd, 0, sizeof(struct mmc_command)); 229 memset(&data, 0, sizeof(struct mmc_data)); 230 231 mrq.cmd = &cmd; 232 mrq.data = &data; 233 234 cmd.opcode = opcode; 235 cmd.arg = 0; 236 237 /* NOTE HACK: the MMC_RSP_SPI_R1 is always correct here, but we 238 * rely on callers to never use this with "native" calls for reading 239 * CSD or CID. Native versions of those commands use the R2 type, 240 * not R1 plus a data block. 241 */ 242 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; 243 244 data.blksz = len; 245 data.blocks = 1; 246 data.flags = MMC_DATA_READ; 247 data.sg = &sg; 248 data.sg_len = 1; 249 250 sg_init_one(&sg, data_buf, len); 251 252 if (card) 253 mmc_set_data_timeout(&data, card); 254 255 mmc_wait_for_req(host, &mrq); 256 257 memcpy(buf, data_buf, len); 258 kfree(data_buf); 259 260 if (cmd.error) 261 return cmd.error; 262 if (data.error) 263 return data.error; 264 265 return 0; 266 } 267 268 int mmc_send_csd(struct mmc_card *card, u32 *csd) 269 { 270 if (!mmc_host_is_spi(card->host)) 271 return mmc_send_cxd_native(card->host, card->rca << 16, 272 csd, MMC_SEND_CSD); 273 274 return mmc_send_cxd_data(card, card->host, MMC_SEND_CSD, csd, 16); 275 } 276 277 int mmc_send_cid(struct mmc_host *host, u32 *cid) 278 { 279 if (!mmc_host_is_spi(host)) { 280 if (!host->card) 281 return -EINVAL; 282 return mmc_send_cxd_native(host, host->card->rca << 16, 283 cid, MMC_SEND_CID); 284 } 285 286 return mmc_send_cxd_data(NULL, host, MMC_SEND_CID, cid, 16); 287 } 288 289 int mmc_send_ext_csd(struct mmc_card *card, u8 *ext_csd) 290 { 291 return mmc_send_cxd_data(card, card->host, MMC_SEND_EXT_CSD, 292 ext_csd, 512); 293 } 294 295 int mmc_spi_read_ocr(struct mmc_host *host, int highcap, u32 *ocrp) 296 { 297 struct mmc_command cmd; 298 int err; 299 300 memset(&cmd, 0, sizeof(struct mmc_command)); 301 302 cmd.opcode = MMC_SPI_READ_OCR; 303 cmd.arg = highcap ? (1 << 30) : 0; 304 cmd.flags = MMC_RSP_SPI_R3; 305 306 err = mmc_wait_for_cmd(host, &cmd, 0); 307 308 *ocrp = cmd.resp[1]; 309 return err; 310 } 311 312 int mmc_spi_set_crc(struct mmc_host *host, int use_crc) 313 { 314 struct mmc_command cmd; 315 int err; 316 317 memset(&cmd, 0, sizeof(struct mmc_command)); 318 319 cmd.opcode = MMC_SPI_CRC_ON_OFF; 320 cmd.flags = MMC_RSP_SPI_R1; 321 cmd.arg = use_crc; 322 323 err = mmc_wait_for_cmd(host, &cmd, 0); 324 if (!err) 325 host->use_spi_crc = use_crc; 326 return err; 327 } 328 329 int mmc_switch(struct mmc_card *card, u8 set, u8 index, u8 value) 330 { 331 int err; 332 struct mmc_command cmd; 333 334 BUG_ON(!card); 335 BUG_ON(!card->host); 336 337 memset(&cmd, 0, sizeof(struct mmc_command)); 338 339 cmd.opcode = MMC_SWITCH; 340 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | 341 (index << 16) | 342 (value << 8) | 343 set; 344 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; 345 346 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES); 347 if (err) 348 return err; 349 350 return 0; 351 } 352 353 int mmc_send_status(struct mmc_card *card, u32 *status) 354 { 355 int err; 356 struct mmc_command cmd; 357 358 BUG_ON(!card); 359 BUG_ON(!card->host); 360 361 memset(&cmd, 0, sizeof(struct mmc_command)); 362 363 cmd.opcode = MMC_SEND_STATUS; 364 if (!mmc_host_is_spi(card->host)) 365 cmd.arg = card->rca << 16; 366 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC; 367 368 err = mmc_wait_for_cmd(card->host, &cmd, MMC_CMD_RETRIES); 369 if (err) 370 return err; 371 372 /* NOTE: callers are required to understand the difference 373 * between "native" and SPI format status words! 374 */ 375 if (status) 376 *status = cmd.resp[0]; 377 378 return 0; 379 } 380 381