Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/mmc/sd.c |
| 3 | * |
| 4 | * Copyright (C) 2003-2004 Russell King, All Rights Reserved. |
| 5 | * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved. |
| 6 | * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/err.h> |
| 14 | |
| 15 | #include <linux/mmc/host.h> |
| 16 | #include <linux/mmc/card.h> |
| 17 | #include <linux/mmc/mmc.h> |
Pierre Ossman | 3373c0a | 2007-05-31 22:25:11 +0200 | [diff] [blame] | 18 | #include <linux/mmc/sd.h> |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 19 | |
| 20 | #include "core.h" |
| 21 | #include "sysfs.h" |
| 22 | #include "mmc_ops.h" |
| 23 | #include "sd_ops.h" |
| 24 | |
| 25 | #include "core.h" |
| 26 | |
| 27 | static const unsigned int tran_exp[] = { |
| 28 | 10000, 100000, 1000000, 10000000, |
| 29 | 0, 0, 0, 0 |
| 30 | }; |
| 31 | |
| 32 | static const unsigned char tran_mant[] = { |
| 33 | 0, 10, 12, 13, 15, 20, 25, 30, |
| 34 | 35, 40, 45, 50, 55, 60, 70, 80, |
| 35 | }; |
| 36 | |
| 37 | static const unsigned int tacc_exp[] = { |
| 38 | 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, |
| 39 | }; |
| 40 | |
| 41 | static const unsigned int tacc_mant[] = { |
| 42 | 0, 10, 12, 13, 15, 20, 25, 30, |
| 43 | 35, 40, 45, 50, 55, 60, 70, 80, |
| 44 | }; |
| 45 | |
| 46 | #define UNSTUFF_BITS(resp,start,size) \ |
| 47 | ({ \ |
| 48 | const int __size = size; \ |
| 49 | const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \ |
| 50 | const int __off = 3 - ((start) / 32); \ |
| 51 | const int __shft = (start) & 31; \ |
| 52 | u32 __res; \ |
| 53 | \ |
| 54 | __res = resp[__off] >> __shft; \ |
| 55 | if (__size + __shft > 32) \ |
| 56 | __res |= resp[__off-1] << ((32 - __shft) % 32); \ |
| 57 | __res & __mask; \ |
| 58 | }) |
| 59 | |
| 60 | /* |
| 61 | * Given the decoded CSD structure, decode the raw CID to our CID structure. |
| 62 | */ |
| 63 | static void mmc_decode_cid(struct mmc_card *card) |
| 64 | { |
| 65 | u32 *resp = card->raw_cid; |
| 66 | |
| 67 | memset(&card->cid, 0, sizeof(struct mmc_cid)); |
| 68 | |
| 69 | /* |
| 70 | * SD doesn't currently have a version field so we will |
| 71 | * have to assume we can parse this. |
| 72 | */ |
| 73 | card->cid.manfid = UNSTUFF_BITS(resp, 120, 8); |
| 74 | card->cid.oemid = UNSTUFF_BITS(resp, 104, 16); |
| 75 | card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); |
| 76 | card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); |
| 77 | card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); |
| 78 | card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); |
| 79 | card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); |
| 80 | card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4); |
| 81 | card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4); |
| 82 | card->cid.serial = UNSTUFF_BITS(resp, 24, 32); |
| 83 | card->cid.year = UNSTUFF_BITS(resp, 12, 8); |
| 84 | card->cid.month = UNSTUFF_BITS(resp, 8, 4); |
| 85 | |
| 86 | card->cid.year += 2000; /* SD cards year offset */ |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * Given a 128-bit response, decode to our card CSD structure. |
| 91 | */ |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 92 | static int mmc_decode_csd(struct mmc_card *card) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 93 | { |
| 94 | struct mmc_csd *csd = &card->csd; |
| 95 | unsigned int e, m, csd_struct; |
| 96 | u32 *resp = card->raw_csd; |
| 97 | |
| 98 | csd_struct = UNSTUFF_BITS(resp, 126, 2); |
| 99 | |
| 100 | switch (csd_struct) { |
| 101 | case 0: |
| 102 | m = UNSTUFF_BITS(resp, 115, 4); |
| 103 | e = UNSTUFF_BITS(resp, 112, 3); |
| 104 | csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10; |
| 105 | csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100; |
| 106 | |
| 107 | m = UNSTUFF_BITS(resp, 99, 4); |
| 108 | e = UNSTUFF_BITS(resp, 96, 3); |
| 109 | csd->max_dtr = tran_exp[e] * tran_mant[m]; |
| 110 | csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); |
| 111 | |
| 112 | e = UNSTUFF_BITS(resp, 47, 3); |
| 113 | m = UNSTUFF_BITS(resp, 62, 12); |
| 114 | csd->capacity = (1 + m) << (e + 2); |
| 115 | |
| 116 | csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); |
| 117 | csd->read_partial = UNSTUFF_BITS(resp, 79, 1); |
| 118 | csd->write_misalign = UNSTUFF_BITS(resp, 78, 1); |
| 119 | csd->read_misalign = UNSTUFF_BITS(resp, 77, 1); |
| 120 | csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3); |
| 121 | csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); |
| 122 | csd->write_partial = UNSTUFF_BITS(resp, 21, 1); |
| 123 | break; |
| 124 | case 1: |
| 125 | /* |
| 126 | * This is a block-addressed SDHC card. Most |
| 127 | * interesting fields are unused and have fixed |
| 128 | * values. To avoid getting tripped by buggy cards, |
| 129 | * we assume those fixed values ourselves. |
| 130 | */ |
| 131 | mmc_card_set_blockaddr(card); |
| 132 | |
| 133 | csd->tacc_ns = 0; /* Unused */ |
| 134 | csd->tacc_clks = 0; /* Unused */ |
| 135 | |
| 136 | m = UNSTUFF_BITS(resp, 99, 4); |
| 137 | e = UNSTUFF_BITS(resp, 96, 3); |
| 138 | csd->max_dtr = tran_exp[e] * tran_mant[m]; |
| 139 | csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); |
| 140 | |
| 141 | m = UNSTUFF_BITS(resp, 48, 22); |
| 142 | csd->capacity = (1 + m) << 10; |
| 143 | |
| 144 | csd->read_blkbits = 9; |
| 145 | csd->read_partial = 0; |
| 146 | csd->write_misalign = 0; |
| 147 | csd->read_misalign = 0; |
| 148 | csd->r2w_factor = 4; /* Unused */ |
| 149 | csd->write_blkbits = 9; |
| 150 | csd->write_partial = 0; |
| 151 | break; |
| 152 | default: |
| 153 | printk("%s: unrecognised CSD structure version %d\n", |
| 154 | mmc_hostname(card->host), csd_struct); |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 155 | return -EINVAL; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 156 | } |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 157 | |
| 158 | return 0; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /* |
| 162 | * Given a 64-bit response, decode to our card SCR structure. |
| 163 | */ |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 164 | static int mmc_decode_scr(struct mmc_card *card) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 165 | { |
| 166 | struct sd_scr *scr = &card->scr; |
| 167 | unsigned int scr_struct; |
| 168 | u32 resp[4]; |
| 169 | |
| 170 | BUG_ON(!mmc_card_sd(card)); |
| 171 | |
| 172 | resp[3] = card->raw_scr[1]; |
| 173 | resp[2] = card->raw_scr[0]; |
| 174 | |
| 175 | scr_struct = UNSTUFF_BITS(resp, 60, 4); |
| 176 | if (scr_struct != 0) { |
| 177 | printk("%s: unrecognised SCR structure version %d\n", |
| 178 | mmc_hostname(card->host), scr_struct); |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 179 | return -EINVAL; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4); |
| 183 | scr->bus_widths = UNSTUFF_BITS(resp, 48, 4); |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 184 | |
| 185 | return 0; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /* |
Pierre Ossman | 1addfcd | 2007-05-01 14:46:08 +0200 | [diff] [blame] | 189 | * Fetches and decodes switch information |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 190 | */ |
Pierre Ossman | 1addfcd | 2007-05-01 14:46:08 +0200 | [diff] [blame] | 191 | static int mmc_read_switch(struct mmc_card *card) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 192 | { |
| 193 | int err; |
| 194 | u8 *status; |
| 195 | |
Pierre Ossman | 3373c0a | 2007-05-31 22:25:11 +0200 | [diff] [blame] | 196 | if (card->scr.sda_vsn < SCR_SPEC_VER_1) |
| 197 | return MMC_ERR_NONE; |
| 198 | |
| 199 | if (!(card->csd.cmdclass & CCC_SWITCH)) { |
| 200 | printk(KERN_WARNING "%s: card lacks mandatory switch " |
| 201 | "function, performance might suffer.\n", |
| 202 | mmc_hostname(card->host)); |
| 203 | return MMC_ERR_NONE; |
| 204 | } |
| 205 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 206 | err = MMC_ERR_FAILED; |
| 207 | |
| 208 | status = kmalloc(64, GFP_KERNEL); |
| 209 | if (!status) { |
| 210 | printk("%s: could not allocate a buffer for switch " |
| 211 | "capabilities.\n", |
| 212 | mmc_hostname(card->host)); |
| 213 | return err; |
| 214 | } |
| 215 | |
| 216 | err = mmc_sd_switch(card, 0, 0, 1, status); |
| 217 | if (err != MMC_ERR_NONE) { |
Pierre Ossman | 3373c0a | 2007-05-31 22:25:11 +0200 | [diff] [blame] | 218 | printk(KERN_WARNING "%s: problem reading switch " |
| 219 | "capabilities, performance might suffer.\n", |
| 220 | mmc_hostname(card->host)); |
Pierre Ossman | 1addfcd | 2007-05-01 14:46:08 +0200 | [diff] [blame] | 221 | err = MMC_ERR_NONE; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 222 | goto out; |
| 223 | } |
| 224 | |
| 225 | if (status[13] & 0x02) |
| 226 | card->sw_caps.hs_max_dtr = 50000000; |
| 227 | |
Pierre Ossman | 1addfcd | 2007-05-01 14:46:08 +0200 | [diff] [blame] | 228 | out: |
| 229 | kfree(status); |
| 230 | |
| 231 | return err; |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | * Test if the card supports high-speed mode and, if so, switch to it. |
| 236 | */ |
| 237 | static int mmc_switch_hs(struct mmc_card *card) |
| 238 | { |
| 239 | int err; |
| 240 | u8 *status; |
| 241 | |
Pierre Ossman | 3373c0a | 2007-05-31 22:25:11 +0200 | [diff] [blame] | 242 | if (card->scr.sda_vsn < SCR_SPEC_VER_1) |
| 243 | return MMC_ERR_NONE; |
| 244 | |
| 245 | if (!(card->csd.cmdclass & CCC_SWITCH)) |
| 246 | return MMC_ERR_NONE; |
| 247 | |
Pierre Ossman | 1addfcd | 2007-05-01 14:46:08 +0200 | [diff] [blame] | 248 | if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED)) |
| 249 | return MMC_ERR_NONE; |
| 250 | |
| 251 | if (card->sw_caps.hs_max_dtr == 0) |
| 252 | return MMC_ERR_NONE; |
| 253 | |
| 254 | err = MMC_ERR_FAILED; |
| 255 | |
| 256 | status = kmalloc(64, GFP_KERNEL); |
| 257 | if (!status) { |
| 258 | printk("%s: could not allocate a buffer for switch " |
| 259 | "capabilities.\n", |
| 260 | mmc_hostname(card->host)); |
| 261 | return err; |
| 262 | } |
| 263 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 264 | err = mmc_sd_switch(card, 1, 0, 1, status); |
| 265 | if (err != MMC_ERR_NONE) |
| 266 | goto out; |
| 267 | |
| 268 | if ((status[16] & 0xF) != 1) { |
| 269 | printk(KERN_WARNING "%s: Problem switching card " |
| 270 | "into high-speed mode!\n", |
| 271 | mmc_hostname(card->host)); |
| 272 | } else { |
| 273 | mmc_card_set_highspeed(card); |
| 274 | mmc_set_timing(card->host, MMC_TIMING_SD_HS); |
| 275 | } |
| 276 | |
| 277 | out: |
| 278 | kfree(status); |
| 279 | |
| 280 | return err; |
| 281 | } |
| 282 | |
| 283 | /* |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 284 | * Handle the detection and initialisation of a card. |
| 285 | * |
| 286 | * In the case of a resume, "curcard" will contain the card |
| 287 | * we're trying to reinitialise. |
| 288 | */ |
| 289 | static int mmc_sd_init_card(struct mmc_host *host, u32 ocr, |
| 290 | struct mmc_card *oldcard) |
| 291 | { |
| 292 | struct mmc_card *card; |
| 293 | int err; |
| 294 | u32 cid[4]; |
| 295 | unsigned int max_dtr; |
| 296 | |
| 297 | BUG_ON(!host); |
| 298 | BUG_ON(!host->claimed); |
| 299 | |
| 300 | /* |
| 301 | * Since we're changing the OCR value, we seem to |
| 302 | * need to tell some cards to go back to the idle |
| 303 | * state. We wait 1ms to give cards time to |
| 304 | * respond. |
| 305 | */ |
| 306 | mmc_go_idle(host); |
| 307 | |
| 308 | /* |
| 309 | * If SD_SEND_IF_COND indicates an SD 2.0 |
| 310 | * compliant card and we should set bit 30 |
| 311 | * of the ocr to indicate that we can handle |
| 312 | * block-addressed SDHC cards. |
| 313 | */ |
| 314 | err = mmc_send_if_cond(host, ocr); |
| 315 | if (err == MMC_ERR_NONE) |
| 316 | ocr |= 1 << 30; |
| 317 | |
| 318 | err = mmc_send_app_op_cond(host, ocr, NULL); |
| 319 | if (err != MMC_ERR_NONE) |
| 320 | goto err; |
| 321 | |
| 322 | /* |
| 323 | * Fetch CID from card. |
| 324 | */ |
| 325 | err = mmc_all_send_cid(host, cid); |
| 326 | if (err != MMC_ERR_NONE) |
| 327 | goto err; |
| 328 | |
| 329 | if (oldcard) { |
| 330 | if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) |
| 331 | goto err; |
| 332 | |
| 333 | card = oldcard; |
| 334 | } else { |
| 335 | /* |
| 336 | * Allocate card structure. |
| 337 | */ |
| 338 | card = mmc_alloc_card(host); |
| 339 | if (IS_ERR(card)) |
| 340 | goto err; |
| 341 | |
| 342 | card->type = MMC_TYPE_SD; |
| 343 | memcpy(card->raw_cid, cid, sizeof(card->raw_cid)); |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * Set card RCA. |
| 348 | */ |
| 349 | err = mmc_send_relative_addr(host, &card->rca); |
| 350 | if (err != MMC_ERR_NONE) |
| 351 | goto free_card; |
| 352 | |
| 353 | mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); |
| 354 | |
| 355 | if (!oldcard) { |
| 356 | /* |
| 357 | * Fetch CSD from card. |
| 358 | */ |
| 359 | err = mmc_send_csd(card, card->raw_csd); |
| 360 | if (err != MMC_ERR_NONE) |
| 361 | goto free_card; |
| 362 | |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 363 | err = mmc_decode_csd(card); |
| 364 | if (err < 0) |
| 365 | goto free_card; |
| 366 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 367 | mmc_decode_cid(card); |
| 368 | } |
| 369 | |
| 370 | /* |
| 371 | * Select card, as all following commands rely on that. |
| 372 | */ |
| 373 | err = mmc_select_card(card); |
| 374 | if (err != MMC_ERR_NONE) |
| 375 | goto free_card; |
| 376 | |
| 377 | if (!oldcard) { |
| 378 | /* |
| 379 | * Fetch SCR from card. |
| 380 | */ |
| 381 | err = mmc_app_send_scr(card, card->raw_scr); |
| 382 | if (err != MMC_ERR_NONE) |
| 383 | goto free_card; |
| 384 | |
Pierre Ossman | bd76631 | 2007-05-01 16:11:57 +0200 | [diff] [blame] | 385 | err = mmc_decode_scr(card); |
| 386 | if (err < 0) |
| 387 | goto free_card; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 388 | |
| 389 | /* |
| 390 | * Fetch switch information from card. |
| 391 | */ |
| 392 | err = mmc_read_switch(card); |
| 393 | if (err != MMC_ERR_NONE) |
| 394 | goto free_card; |
| 395 | } |
| 396 | |
| 397 | /* |
| 398 | * Attempt to change to high-speed (if supported) |
| 399 | */ |
| 400 | err = mmc_switch_hs(card); |
| 401 | if (err != MMC_ERR_NONE) |
| 402 | goto free_card; |
| 403 | |
| 404 | /* |
| 405 | * Compute bus speed. |
| 406 | */ |
| 407 | max_dtr = (unsigned int)-1; |
| 408 | |
| 409 | if (mmc_card_highspeed(card)) { |
| 410 | if (max_dtr > card->sw_caps.hs_max_dtr) |
| 411 | max_dtr = card->sw_caps.hs_max_dtr; |
| 412 | } else if (max_dtr > card->csd.max_dtr) { |
| 413 | max_dtr = card->csd.max_dtr; |
| 414 | } |
| 415 | |
| 416 | mmc_set_clock(host, max_dtr); |
| 417 | |
| 418 | /* |
| 419 | * Switch to wider bus (if supported). |
| 420 | */ |
Pierre Ossman | 7165129 | 2007-06-06 20:23:25 +0200 | [diff] [blame] | 421 | if ((host->caps & MMC_CAP_4_BIT_DATA) && |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 422 | (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) { |
| 423 | err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4); |
| 424 | if (err != MMC_ERR_NONE) |
| 425 | goto free_card; |
| 426 | |
| 427 | mmc_set_bus_width(host, MMC_BUS_WIDTH_4); |
| 428 | } |
| 429 | |
| 430 | if (!oldcard) |
| 431 | host->card = card; |
| 432 | |
| 433 | return MMC_ERR_NONE; |
| 434 | |
| 435 | free_card: |
| 436 | if (!oldcard) |
| 437 | mmc_remove_card(card); |
| 438 | err: |
| 439 | |
| 440 | return MMC_ERR_FAILED; |
| 441 | } |
| 442 | |
| 443 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 444 | * Host is being removed. Free up the current card. |
| 445 | */ |
| 446 | static void mmc_sd_remove(struct mmc_host *host) |
| 447 | { |
| 448 | BUG_ON(!host); |
| 449 | BUG_ON(!host->card); |
| 450 | |
| 451 | mmc_remove_card(host->card); |
| 452 | host->card = NULL; |
| 453 | } |
| 454 | |
| 455 | /* |
| 456 | * Card detection callback from host. |
| 457 | */ |
| 458 | static void mmc_sd_detect(struct mmc_host *host) |
| 459 | { |
| 460 | int err; |
| 461 | |
| 462 | BUG_ON(!host); |
| 463 | BUG_ON(!host->card); |
| 464 | |
| 465 | mmc_claim_host(host); |
| 466 | |
| 467 | /* |
| 468 | * Just check if our card has been removed. |
| 469 | */ |
| 470 | err = mmc_send_status(host->card, NULL); |
| 471 | |
| 472 | mmc_release_host(host); |
| 473 | |
| 474 | if (err != MMC_ERR_NONE) { |
| 475 | mmc_remove_card(host->card); |
| 476 | host->card = NULL; |
| 477 | |
| 478 | mmc_claim_host(host); |
| 479 | mmc_detach_bus(host); |
| 480 | mmc_release_host(host); |
| 481 | } |
| 482 | } |
| 483 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 484 | #ifdef CONFIG_MMC_UNSAFE_RESUME |
| 485 | |
| 486 | /* |
| 487 | * Suspend callback from host. |
| 488 | */ |
| 489 | static void mmc_sd_suspend(struct mmc_host *host) |
| 490 | { |
| 491 | BUG_ON(!host); |
| 492 | BUG_ON(!host->card); |
| 493 | |
| 494 | mmc_claim_host(host); |
| 495 | mmc_deselect_cards(host); |
| 496 | host->card->state &= ~MMC_STATE_HIGHSPEED; |
| 497 | mmc_release_host(host); |
| 498 | } |
| 499 | |
| 500 | /* |
| 501 | * Resume callback from host. |
| 502 | * |
| 503 | * This function tries to determine if the same card is still present |
| 504 | * and, if so, restore all state to it. |
| 505 | */ |
| 506 | static void mmc_sd_resume(struct mmc_host *host) |
| 507 | { |
| 508 | int err; |
| 509 | |
| 510 | BUG_ON(!host); |
| 511 | BUG_ON(!host->card); |
| 512 | |
| 513 | mmc_claim_host(host); |
| 514 | |
| 515 | err = mmc_sd_init_card(host, host->ocr, host->card); |
| 516 | if (err != MMC_ERR_NONE) { |
| 517 | mmc_remove_card(host->card); |
| 518 | host->card = NULL; |
| 519 | |
| 520 | mmc_detach_bus(host); |
| 521 | } |
| 522 | |
| 523 | mmc_release_host(host); |
| 524 | } |
| 525 | |
| 526 | #else |
| 527 | |
| 528 | #define mmc_sd_suspend NULL |
| 529 | #define mmc_sd_resume NULL |
| 530 | |
| 531 | #endif |
| 532 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 533 | static const struct mmc_bus_ops mmc_sd_ops = { |
| 534 | .remove = mmc_sd_remove, |
| 535 | .detect = mmc_sd_detect, |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 536 | .suspend = mmc_sd_suspend, |
| 537 | .resume = mmc_sd_resume, |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 538 | }; |
| 539 | |
| 540 | /* |
| 541 | * Starting point for SD card init. |
| 542 | */ |
| 543 | int mmc_attach_sd(struct mmc_host *host, u32 ocr) |
| 544 | { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 545 | int err; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 546 | |
| 547 | BUG_ON(!host); |
| 548 | BUG_ON(!host->claimed); |
| 549 | |
| 550 | mmc_attach_bus(host, &mmc_sd_ops); |
| 551 | |
Philip Langdale | 55556da | 2007-03-16 19:39:00 -0700 | [diff] [blame] | 552 | /* |
| 553 | * Sanity check the voltages that the card claims to |
| 554 | * support. |
| 555 | */ |
| 556 | if (ocr & 0x7F) { |
| 557 | printk(KERN_WARNING "%s: card claims to support voltages " |
| 558 | "below the defined range. These will be ignored.\n", |
| 559 | mmc_hostname(host)); |
| 560 | ocr &= ~0x7F; |
| 561 | } |
| 562 | |
| 563 | if (ocr & MMC_VDD_165_195) { |
| 564 | printk(KERN_WARNING "%s: SD card claims to support the " |
| 565 | "incompletely defined 'low voltage range'. This " |
| 566 | "will be ignored.\n", mmc_hostname(host)); |
| 567 | ocr &= ~MMC_VDD_165_195; |
| 568 | } |
| 569 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 570 | host->ocr = mmc_select_voltage(host, ocr); |
| 571 | |
| 572 | /* |
| 573 | * Can we support the voltage(s) of the card(s)? |
| 574 | */ |
| 575 | if (!host->ocr) |
| 576 | goto err; |
| 577 | |
| 578 | /* |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 579 | * Detect and init the card. |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 580 | */ |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 581 | err = mmc_sd_init_card(host, host->ocr, NULL); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 582 | if (err != MMC_ERR_NONE) |
| 583 | goto err; |
| 584 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 585 | mmc_release_host(host); |
| 586 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 587 | err = mmc_register_card(host->card); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 588 | if (err) |
| 589 | goto reclaim_host; |
| 590 | |
| 591 | return 0; |
| 592 | |
| 593 | reclaim_host: |
| 594 | mmc_claim_host(host); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 595 | mmc_remove_card(host->card); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 596 | host->card = NULL; |
| 597 | err: |
| 598 | mmc_detach_bus(host); |
| 599 | mmc_release_host(host); |
| 600 | |
| 601 | return 0; |
| 602 | } |
| 603 | |