Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/mmc/mmc.c |
| 3 | * |
| 4 | * Copyright (C) 2003-2004 Russell King, All Rights Reserved. |
| 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 version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | #include <linux/config.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/completion.h> |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/pagemap.h> |
| 18 | #include <linux/err.h> |
| 19 | |
| 20 | #include <linux/mmc/card.h> |
| 21 | #include <linux/mmc/host.h> |
| 22 | #include <linux/mmc/protocol.h> |
| 23 | |
| 24 | #include "mmc.h" |
| 25 | |
| 26 | #ifdef CONFIG_MMC_DEBUG |
| 27 | #define DBG(x...) printk(KERN_DEBUG x) |
| 28 | #else |
| 29 | #define DBG(x...) do { } while (0) |
| 30 | #endif |
| 31 | |
| 32 | #define CMD_RETRIES 3 |
| 33 | |
| 34 | /* |
| 35 | * OCR Bit positions to 10s of Vdd mV. |
| 36 | */ |
| 37 | static const unsigned short mmc_ocr_bit_to_vdd[] = { |
| 38 | 150, 155, 160, 165, 170, 180, 190, 200, |
| 39 | 210, 220, 230, 240, 250, 260, 270, 280, |
| 40 | 290, 300, 310, 320, 330, 340, 350, 360 |
| 41 | }; |
| 42 | |
| 43 | static const unsigned int tran_exp[] = { |
| 44 | 10000, 100000, 1000000, 10000000, |
| 45 | 0, 0, 0, 0 |
| 46 | }; |
| 47 | |
| 48 | static const unsigned char tran_mant[] = { |
| 49 | 0, 10, 12, 13, 15, 20, 25, 30, |
| 50 | 35, 40, 45, 50, 55, 60, 70, 80, |
| 51 | }; |
| 52 | |
| 53 | static const unsigned int tacc_exp[] = { |
| 54 | 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, |
| 55 | }; |
| 56 | |
| 57 | static const unsigned int tacc_mant[] = { |
| 58 | 0, 10, 12, 13, 15, 20, 25, 30, |
| 59 | 35, 40, 45, 50, 55, 60, 70, 80, |
| 60 | }; |
| 61 | |
| 62 | |
| 63 | /** |
| 64 | * mmc_request_done - finish processing an MMC command |
| 65 | * @host: MMC host which completed command |
| 66 | * @mrq: MMC request which completed |
| 67 | * |
| 68 | * MMC drivers should call this function when they have completed |
| 69 | * their processing of a command. This should be called before the |
| 70 | * data part of the command has completed. |
| 71 | */ |
| 72 | void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq) |
| 73 | { |
| 74 | struct mmc_command *cmd = mrq->cmd; |
| 75 | int err = mrq->cmd->error; |
| 76 | DBG("MMC: req done (%02x): %d: %08x %08x %08x %08x\n", cmd->opcode, |
| 77 | err, cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]); |
| 78 | |
| 79 | if (err && cmd->retries) { |
| 80 | cmd->retries--; |
| 81 | cmd->error = 0; |
| 82 | host->ops->request(host, mrq); |
| 83 | } else if (mrq->done) { |
| 84 | mrq->done(mrq); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | EXPORT_SYMBOL(mmc_request_done); |
| 89 | |
| 90 | /** |
| 91 | * mmc_start_request - start a command on a host |
| 92 | * @host: MMC host to start command on |
| 93 | * @mrq: MMC request to start |
| 94 | * |
| 95 | * Queue a command on the specified host. We expect the |
| 96 | * caller to be holding the host lock with interrupts disabled. |
| 97 | */ |
| 98 | void |
| 99 | mmc_start_request(struct mmc_host *host, struct mmc_request *mrq) |
| 100 | { |
| 101 | DBG("MMC: starting cmd %02x arg %08x flags %08x\n", |
| 102 | mrq->cmd->opcode, mrq->cmd->arg, mrq->cmd->flags); |
| 103 | |
| 104 | WARN_ON(host->card_busy == NULL); |
| 105 | |
| 106 | mrq->cmd->error = 0; |
| 107 | mrq->cmd->mrq = mrq; |
| 108 | if (mrq->data) { |
| 109 | mrq->cmd->data = mrq->data; |
| 110 | mrq->data->error = 0; |
| 111 | mrq->data->mrq = mrq; |
| 112 | if (mrq->stop) { |
| 113 | mrq->data->stop = mrq->stop; |
| 114 | mrq->stop->error = 0; |
| 115 | mrq->stop->mrq = mrq; |
| 116 | } |
| 117 | } |
| 118 | host->ops->request(host, mrq); |
| 119 | } |
| 120 | |
| 121 | EXPORT_SYMBOL(mmc_start_request); |
| 122 | |
| 123 | static void mmc_wait_done(struct mmc_request *mrq) |
| 124 | { |
| 125 | complete(mrq->done_data); |
| 126 | } |
| 127 | |
| 128 | int mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq) |
| 129 | { |
| 130 | DECLARE_COMPLETION(complete); |
| 131 | |
| 132 | mrq->done_data = &complete; |
| 133 | mrq->done = mmc_wait_done; |
| 134 | |
| 135 | mmc_start_request(host, mrq); |
| 136 | |
| 137 | wait_for_completion(&complete); |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | EXPORT_SYMBOL(mmc_wait_for_req); |
| 143 | |
| 144 | /** |
| 145 | * mmc_wait_for_cmd - start a command and wait for completion |
| 146 | * @host: MMC host to start command |
| 147 | * @cmd: MMC command to start |
| 148 | * @retries: maximum number of retries |
| 149 | * |
| 150 | * Start a new MMC command for a host, and wait for the command |
| 151 | * to complete. Return any error that occurred while the command |
| 152 | * was executing. Do not attempt to parse the response. |
| 153 | */ |
| 154 | int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries) |
| 155 | { |
| 156 | struct mmc_request mrq; |
| 157 | |
| 158 | BUG_ON(host->card_busy == NULL); |
| 159 | |
| 160 | memset(&mrq, 0, sizeof(struct mmc_request)); |
| 161 | |
| 162 | memset(cmd->resp, 0, sizeof(cmd->resp)); |
| 163 | cmd->retries = retries; |
| 164 | |
| 165 | mrq.cmd = cmd; |
| 166 | cmd->data = NULL; |
| 167 | |
| 168 | mmc_wait_for_req(host, &mrq); |
| 169 | |
| 170 | return cmd->error; |
| 171 | } |
| 172 | |
| 173 | EXPORT_SYMBOL(mmc_wait_for_cmd); |
| 174 | |
| 175 | |
| 176 | |
| 177 | /** |
| 178 | * __mmc_claim_host - exclusively claim a host |
| 179 | * @host: mmc host to claim |
| 180 | * @card: mmc card to claim host for |
| 181 | * |
| 182 | * Claim a host for a set of operations. If a valid card |
| 183 | * is passed and this wasn't the last card selected, select |
| 184 | * the card before returning. |
| 185 | * |
| 186 | * Note: you should use mmc_card_claim_host or mmc_claim_host. |
| 187 | */ |
| 188 | int __mmc_claim_host(struct mmc_host *host, struct mmc_card *card) |
| 189 | { |
| 190 | DECLARE_WAITQUEUE(wait, current); |
| 191 | unsigned long flags; |
| 192 | int err = 0; |
| 193 | |
| 194 | add_wait_queue(&host->wq, &wait); |
| 195 | spin_lock_irqsave(&host->lock, flags); |
| 196 | while (1) { |
| 197 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 198 | if (host->card_busy == NULL) |
| 199 | break; |
| 200 | spin_unlock_irqrestore(&host->lock, flags); |
| 201 | schedule(); |
| 202 | spin_lock_irqsave(&host->lock, flags); |
| 203 | } |
| 204 | set_current_state(TASK_RUNNING); |
| 205 | host->card_busy = card; |
| 206 | spin_unlock_irqrestore(&host->lock, flags); |
| 207 | remove_wait_queue(&host->wq, &wait); |
| 208 | |
| 209 | if (card != (void *)-1 && host->card_selected != card) { |
| 210 | struct mmc_command cmd; |
| 211 | |
| 212 | host->card_selected = card; |
| 213 | |
| 214 | cmd.opcode = MMC_SELECT_CARD; |
| 215 | cmd.arg = card->rca << 16; |
| 216 | cmd.flags = MMC_RSP_R1; |
| 217 | |
| 218 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); |
| 219 | } |
| 220 | |
| 221 | return err; |
| 222 | } |
| 223 | |
| 224 | EXPORT_SYMBOL(__mmc_claim_host); |
| 225 | |
| 226 | /** |
| 227 | * mmc_release_host - release a host |
| 228 | * @host: mmc host to release |
| 229 | * |
| 230 | * Release a MMC host, allowing others to claim the host |
| 231 | * for their operations. |
| 232 | */ |
| 233 | void mmc_release_host(struct mmc_host *host) |
| 234 | { |
| 235 | unsigned long flags; |
| 236 | |
| 237 | BUG_ON(host->card_busy == NULL); |
| 238 | |
| 239 | spin_lock_irqsave(&host->lock, flags); |
| 240 | host->card_busy = NULL; |
| 241 | spin_unlock_irqrestore(&host->lock, flags); |
| 242 | |
| 243 | wake_up(&host->wq); |
| 244 | } |
| 245 | |
| 246 | EXPORT_SYMBOL(mmc_release_host); |
| 247 | |
| 248 | /* |
| 249 | * Ensure that no card is selected. |
| 250 | */ |
| 251 | static void mmc_deselect_cards(struct mmc_host *host) |
| 252 | { |
| 253 | struct mmc_command cmd; |
| 254 | |
| 255 | if (host->card_selected) { |
| 256 | host->card_selected = NULL; |
| 257 | |
| 258 | cmd.opcode = MMC_SELECT_CARD; |
| 259 | cmd.arg = 0; |
| 260 | cmd.flags = MMC_RSP_NONE; |
| 261 | |
| 262 | mmc_wait_for_cmd(host, &cmd, 0); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | |
| 267 | static inline void mmc_delay(unsigned int ms) |
| 268 | { |
| 269 | if (ms < HZ / 1000) { |
| 270 | yield(); |
| 271 | mdelay(ms); |
| 272 | } else { |
| 273 | msleep_interruptible (ms); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * Mask off any voltages we don't support and select |
| 279 | * the lowest voltage |
| 280 | */ |
| 281 | static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr) |
| 282 | { |
| 283 | int bit; |
| 284 | |
| 285 | ocr &= host->ocr_avail; |
| 286 | |
| 287 | bit = ffs(ocr); |
| 288 | if (bit) { |
| 289 | bit -= 1; |
| 290 | |
| 291 | ocr = 3 << bit; |
| 292 | |
| 293 | host->ios.vdd = bit; |
| 294 | host->ops->set_ios(host, &host->ios); |
| 295 | } else { |
| 296 | ocr = 0; |
| 297 | } |
| 298 | |
| 299 | return ocr; |
| 300 | } |
| 301 | |
| 302 | #define UNSTUFF_BITS(resp,start,size) \ |
| 303 | ({ \ |
| 304 | const int __size = size; \ |
| 305 | const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \ |
| 306 | const int __off = 3 - ((start) / 32); \ |
| 307 | const int __shft = (start) & 31; \ |
| 308 | u32 __res; \ |
| 309 | \ |
| 310 | __res = resp[__off] >> __shft; \ |
| 311 | if (__size + __shft > 32) \ |
| 312 | __res |= resp[__off-1] << ((32 - __shft) % 32); \ |
| 313 | __res & __mask; \ |
| 314 | }) |
| 315 | |
| 316 | /* |
| 317 | * Given the decoded CSD structure, decode the raw CID to our CID structure. |
| 318 | */ |
| 319 | static void mmc_decode_cid(struct mmc_card *card) |
| 320 | { |
| 321 | u32 *resp = card->raw_cid; |
| 322 | |
| 323 | memset(&card->cid, 0, sizeof(struct mmc_cid)); |
| 324 | |
| 325 | /* |
| 326 | * The selection of the format here is guesswork based upon |
| 327 | * information people have sent to date. |
| 328 | */ |
| 329 | switch (card->csd.mmca_vsn) { |
| 330 | case 0: /* MMC v1.? */ |
| 331 | case 1: /* MMC v1.4 */ |
| 332 | card->cid.manfid = UNSTUFF_BITS(resp, 104, 24); |
| 333 | card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); |
| 334 | card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); |
| 335 | card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); |
| 336 | card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); |
| 337 | card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); |
| 338 | card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8); |
| 339 | card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8); |
| 340 | card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4); |
| 341 | card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4); |
| 342 | card->cid.serial = UNSTUFF_BITS(resp, 16, 24); |
| 343 | card->cid.month = UNSTUFF_BITS(resp, 12, 4); |
| 344 | card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997; |
| 345 | break; |
| 346 | |
| 347 | case 2: /* MMC v2.x ? */ |
| 348 | case 3: /* MMC v3.x ? */ |
| 349 | card->cid.manfid = UNSTUFF_BITS(resp, 120, 8); |
| 350 | card->cid.oemid = UNSTUFF_BITS(resp, 104, 16); |
| 351 | card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); |
| 352 | card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); |
| 353 | card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); |
| 354 | card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); |
| 355 | card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); |
| 356 | card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8); |
| 357 | card->cid.serial = UNSTUFF_BITS(resp, 16, 32); |
| 358 | card->cid.month = UNSTUFF_BITS(resp, 12, 4); |
| 359 | card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997; |
| 360 | break; |
| 361 | |
| 362 | default: |
| 363 | printk("%s: card has unknown MMCA version %d\n", |
Russell King | d366b64 | 2005-08-19 09:40:08 +0100 | [diff] [blame] | 364 | mmc_hostname(card->host), card->csd.mmca_vsn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | mmc_card_set_bad(card); |
| 366 | break; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | /* |
| 371 | * Given a 128-bit response, decode to our card CSD structure. |
| 372 | */ |
| 373 | static void mmc_decode_csd(struct mmc_card *card) |
| 374 | { |
| 375 | struct mmc_csd *csd = &card->csd; |
| 376 | unsigned int e, m, csd_struct; |
| 377 | u32 *resp = card->raw_csd; |
| 378 | |
| 379 | /* |
| 380 | * We only understand CSD structure v1.1 and v2. |
| 381 | * v2 has extra information in bits 15, 11 and 10. |
| 382 | */ |
| 383 | csd_struct = UNSTUFF_BITS(resp, 126, 2); |
| 384 | if (csd_struct != 1 && csd_struct != 2) { |
| 385 | printk("%s: unrecognised CSD structure version %d\n", |
Russell King | d366b64 | 2005-08-19 09:40:08 +0100 | [diff] [blame] | 386 | mmc_hostname(card->host), csd_struct); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | mmc_card_set_bad(card); |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4); |
| 392 | m = UNSTUFF_BITS(resp, 115, 4); |
| 393 | e = UNSTUFF_BITS(resp, 112, 3); |
| 394 | csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10; |
| 395 | csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100; |
| 396 | |
| 397 | m = UNSTUFF_BITS(resp, 99, 4); |
| 398 | e = UNSTUFF_BITS(resp, 96, 3); |
| 399 | csd->max_dtr = tran_exp[e] * tran_mant[m]; |
| 400 | csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); |
| 401 | |
| 402 | e = UNSTUFF_BITS(resp, 47, 3); |
| 403 | m = UNSTUFF_BITS(resp, 62, 12); |
| 404 | csd->capacity = (1 + m) << (e + 2); |
| 405 | |
| 406 | csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); |
| 407 | } |
| 408 | |
| 409 | /* |
| 410 | * Locate a MMC card on this MMC host given a raw CID. |
| 411 | */ |
| 412 | static struct mmc_card *mmc_find_card(struct mmc_host *host, u32 *raw_cid) |
| 413 | { |
| 414 | struct mmc_card *card; |
| 415 | |
| 416 | list_for_each_entry(card, &host->cards, node) { |
| 417 | if (memcmp(card->raw_cid, raw_cid, sizeof(card->raw_cid)) == 0) |
| 418 | return card; |
| 419 | } |
| 420 | return NULL; |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | * Allocate a new MMC card, and assign a unique RCA. |
| 425 | */ |
| 426 | static struct mmc_card * |
| 427 | mmc_alloc_card(struct mmc_host *host, u32 *raw_cid, unsigned int *frca) |
| 428 | { |
| 429 | struct mmc_card *card, *c; |
| 430 | unsigned int rca = *frca; |
| 431 | |
| 432 | card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL); |
| 433 | if (!card) |
| 434 | return ERR_PTR(-ENOMEM); |
| 435 | |
| 436 | mmc_init_card(card, host); |
| 437 | memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid)); |
| 438 | |
| 439 | again: |
| 440 | list_for_each_entry(c, &host->cards, node) |
| 441 | if (c->rca == rca) { |
| 442 | rca++; |
| 443 | goto again; |
| 444 | } |
| 445 | |
| 446 | card->rca = rca; |
| 447 | |
| 448 | *frca = rca; |
| 449 | |
| 450 | return card; |
| 451 | } |
| 452 | |
| 453 | /* |
| 454 | * Tell attached cards to go to IDLE state |
| 455 | */ |
| 456 | static void mmc_idle_cards(struct mmc_host *host) |
| 457 | { |
| 458 | struct mmc_command cmd; |
| 459 | |
| 460 | cmd.opcode = MMC_GO_IDLE_STATE; |
| 461 | cmd.arg = 0; |
| 462 | cmd.flags = MMC_RSP_NONE; |
| 463 | |
| 464 | mmc_wait_for_cmd(host, &cmd, 0); |
| 465 | |
| 466 | mmc_delay(1); |
| 467 | } |
| 468 | |
| 469 | /* |
| 470 | * Apply power to the MMC stack. |
| 471 | */ |
| 472 | static void mmc_power_up(struct mmc_host *host) |
| 473 | { |
| 474 | int bit = fls(host->ocr_avail) - 1; |
| 475 | |
| 476 | host->ios.vdd = bit; |
| 477 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; |
| 478 | host->ios.power_mode = MMC_POWER_UP; |
| 479 | host->ops->set_ios(host, &host->ios); |
| 480 | |
| 481 | mmc_delay(1); |
| 482 | |
| 483 | host->ios.clock = host->f_min; |
| 484 | host->ios.power_mode = MMC_POWER_ON; |
| 485 | host->ops->set_ios(host, &host->ios); |
| 486 | |
| 487 | mmc_delay(2); |
| 488 | } |
| 489 | |
| 490 | static void mmc_power_off(struct mmc_host *host) |
| 491 | { |
| 492 | host->ios.clock = 0; |
| 493 | host->ios.vdd = 0; |
| 494 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; |
| 495 | host->ios.power_mode = MMC_POWER_OFF; |
| 496 | host->ops->set_ios(host, &host->ios); |
| 497 | } |
| 498 | |
| 499 | static int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr) |
| 500 | { |
| 501 | struct mmc_command cmd; |
| 502 | int i, err = 0; |
| 503 | |
| 504 | cmd.opcode = MMC_SEND_OP_COND; |
| 505 | cmd.arg = ocr; |
| 506 | cmd.flags = MMC_RSP_R3; |
| 507 | |
| 508 | for (i = 100; i; i--) { |
| 509 | err = mmc_wait_for_cmd(host, &cmd, 0); |
| 510 | if (err != MMC_ERR_NONE) |
| 511 | break; |
| 512 | |
| 513 | if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0) |
| 514 | break; |
| 515 | |
| 516 | err = MMC_ERR_TIMEOUT; |
| 517 | |
| 518 | mmc_delay(10); |
| 519 | } |
| 520 | |
| 521 | if (rocr) |
| 522 | *rocr = cmd.resp[0]; |
| 523 | |
| 524 | return err; |
| 525 | } |
| 526 | |
| 527 | /* |
| 528 | * Discover cards by requesting their CID. If this command |
| 529 | * times out, it is not an error; there are no further cards |
| 530 | * to be discovered. Add new cards to the list. |
| 531 | * |
| 532 | * Create a mmc_card entry for each discovered card, assigning |
| 533 | * it an RCA, and save the raw CID for decoding later. |
| 534 | */ |
| 535 | static void mmc_discover_cards(struct mmc_host *host) |
| 536 | { |
| 537 | struct mmc_card *card; |
| 538 | unsigned int first_rca = 1, err; |
| 539 | |
| 540 | while (1) { |
| 541 | struct mmc_command cmd; |
| 542 | |
| 543 | cmd.opcode = MMC_ALL_SEND_CID; |
| 544 | cmd.arg = 0; |
| 545 | cmd.flags = MMC_RSP_R2; |
| 546 | |
| 547 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); |
| 548 | if (err == MMC_ERR_TIMEOUT) { |
| 549 | err = MMC_ERR_NONE; |
| 550 | break; |
| 551 | } |
| 552 | if (err != MMC_ERR_NONE) { |
| 553 | printk(KERN_ERR "%s: error requesting CID: %d\n", |
Russell King | d366b64 | 2005-08-19 09:40:08 +0100 | [diff] [blame] | 554 | mmc_hostname(host), err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | break; |
| 556 | } |
| 557 | |
| 558 | card = mmc_find_card(host, cmd.resp); |
| 559 | if (!card) { |
| 560 | card = mmc_alloc_card(host, cmd.resp, &first_rca); |
| 561 | if (IS_ERR(card)) { |
| 562 | err = PTR_ERR(card); |
| 563 | break; |
| 564 | } |
| 565 | list_add(&card->node, &host->cards); |
| 566 | } |
| 567 | |
| 568 | card->state &= ~MMC_STATE_DEAD; |
| 569 | |
| 570 | cmd.opcode = MMC_SET_RELATIVE_ADDR; |
| 571 | cmd.arg = card->rca << 16; |
| 572 | cmd.flags = MMC_RSP_R1; |
| 573 | |
| 574 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); |
| 575 | if (err != MMC_ERR_NONE) |
| 576 | mmc_card_set_dead(card); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | static void mmc_read_csds(struct mmc_host *host) |
| 581 | { |
| 582 | struct mmc_card *card; |
| 583 | |
| 584 | list_for_each_entry(card, &host->cards, node) { |
| 585 | struct mmc_command cmd; |
| 586 | int err; |
| 587 | |
| 588 | if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT)) |
| 589 | continue; |
| 590 | |
| 591 | cmd.opcode = MMC_SEND_CSD; |
| 592 | cmd.arg = card->rca << 16; |
| 593 | cmd.flags = MMC_RSP_R2; |
| 594 | |
| 595 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); |
| 596 | if (err != MMC_ERR_NONE) { |
| 597 | mmc_card_set_dead(card); |
| 598 | continue; |
| 599 | } |
| 600 | |
| 601 | memcpy(card->raw_csd, cmd.resp, sizeof(card->raw_csd)); |
| 602 | |
| 603 | mmc_decode_csd(card); |
| 604 | mmc_decode_cid(card); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | static unsigned int mmc_calculate_clock(struct mmc_host *host) |
| 609 | { |
| 610 | struct mmc_card *card; |
| 611 | unsigned int max_dtr = host->f_max; |
| 612 | |
| 613 | list_for_each_entry(card, &host->cards, node) |
| 614 | if (!mmc_card_dead(card) && max_dtr > card->csd.max_dtr) |
| 615 | max_dtr = card->csd.max_dtr; |
| 616 | |
| 617 | DBG("MMC: selected %d.%03dMHz transfer rate\n", |
| 618 | max_dtr / 1000000, (max_dtr / 1000) % 1000); |
| 619 | |
| 620 | return max_dtr; |
| 621 | } |
| 622 | |
| 623 | /* |
| 624 | * Check whether cards we already know about are still present. |
| 625 | * We do this by requesting status, and checking whether a card |
| 626 | * responds. |
| 627 | * |
| 628 | * A request for status does not cause a state change in data |
| 629 | * transfer mode. |
| 630 | */ |
| 631 | static void mmc_check_cards(struct mmc_host *host) |
| 632 | { |
| 633 | struct list_head *l, *n; |
| 634 | |
| 635 | mmc_deselect_cards(host); |
| 636 | |
| 637 | list_for_each_safe(l, n, &host->cards) { |
| 638 | struct mmc_card *card = mmc_list_to_card(l); |
| 639 | struct mmc_command cmd; |
| 640 | int err; |
| 641 | |
| 642 | cmd.opcode = MMC_SEND_STATUS; |
| 643 | cmd.arg = card->rca << 16; |
| 644 | cmd.flags = MMC_RSP_R1; |
| 645 | |
| 646 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); |
| 647 | if (err == MMC_ERR_NONE) |
| 648 | continue; |
| 649 | |
| 650 | mmc_card_set_dead(card); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | static void mmc_setup(struct mmc_host *host) |
| 655 | { |
| 656 | if (host->ios.power_mode != MMC_POWER_ON) { |
| 657 | int err; |
| 658 | u32 ocr; |
| 659 | |
| 660 | mmc_power_up(host); |
| 661 | mmc_idle_cards(host); |
| 662 | |
| 663 | err = mmc_send_op_cond(host, 0, &ocr); |
| 664 | if (err != MMC_ERR_NONE) |
| 665 | return; |
| 666 | |
| 667 | host->ocr = mmc_select_voltage(host, ocr); |
| 668 | |
| 669 | /* |
| 670 | * Since we're changing the OCR value, we seem to |
| 671 | * need to tell some cards to go back to the idle |
| 672 | * state. We wait 1ms to give cards time to |
| 673 | * respond. |
| 674 | */ |
| 675 | if (host->ocr) |
| 676 | mmc_idle_cards(host); |
| 677 | } else { |
| 678 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; |
| 679 | host->ios.clock = host->f_min; |
| 680 | host->ops->set_ios(host, &host->ios); |
| 681 | |
| 682 | /* |
| 683 | * We should remember the OCR mask from the existing |
| 684 | * cards, and detect the new cards OCR mask, combine |
| 685 | * the two and re-select the VDD. However, if we do |
| 686 | * change VDD, we should do an idle, and then do a |
| 687 | * full re-initialisation. We would need to notify |
| 688 | * drivers so that they can re-setup the cards as |
| 689 | * well, while keeping their queues at bay. |
| 690 | * |
| 691 | * For the moment, we take the easy way out - if the |
| 692 | * new cards don't like our currently selected VDD, |
| 693 | * they drop off the bus. |
| 694 | */ |
| 695 | } |
| 696 | |
| 697 | if (host->ocr == 0) |
| 698 | return; |
| 699 | |
| 700 | /* |
| 701 | * Send the selected OCR multiple times... until the cards |
| 702 | * all get the idea that they should be ready for CMD2. |
| 703 | * (My SanDisk card seems to need this.) |
| 704 | */ |
| 705 | mmc_send_op_cond(host, host->ocr, NULL); |
| 706 | |
| 707 | mmc_discover_cards(host); |
| 708 | |
| 709 | /* |
| 710 | * Ok, now switch to push-pull mode. |
| 711 | */ |
| 712 | host->ios.bus_mode = MMC_BUSMODE_PUSHPULL; |
| 713 | host->ops->set_ios(host, &host->ios); |
| 714 | |
| 715 | mmc_read_csds(host); |
| 716 | } |
| 717 | |
| 718 | |
| 719 | /** |
| 720 | * mmc_detect_change - process change of state on a MMC socket |
| 721 | * @host: host which changed state. |
| 722 | * |
| 723 | * All we know is that card(s) have been inserted or removed |
| 724 | * from the socket(s). We don't know which socket or cards. |
| 725 | */ |
| 726 | void mmc_detect_change(struct mmc_host *host) |
| 727 | { |
| 728 | schedule_work(&host->detect); |
| 729 | } |
| 730 | |
| 731 | EXPORT_SYMBOL(mmc_detect_change); |
| 732 | |
| 733 | |
| 734 | static void mmc_rescan(void *data) |
| 735 | { |
| 736 | struct mmc_host *host = data; |
| 737 | struct list_head *l, *n; |
| 738 | |
| 739 | mmc_claim_host(host); |
| 740 | |
| 741 | if (host->ios.power_mode == MMC_POWER_ON) |
| 742 | mmc_check_cards(host); |
| 743 | |
| 744 | mmc_setup(host); |
| 745 | |
| 746 | if (!list_empty(&host->cards)) { |
| 747 | /* |
| 748 | * (Re-)calculate the fastest clock rate which the |
| 749 | * attached cards and the host support. |
| 750 | */ |
| 751 | host->ios.clock = mmc_calculate_clock(host); |
| 752 | host->ops->set_ios(host, &host->ios); |
| 753 | } |
| 754 | |
| 755 | mmc_release_host(host); |
| 756 | |
| 757 | list_for_each_safe(l, n, &host->cards) { |
| 758 | struct mmc_card *card = mmc_list_to_card(l); |
| 759 | |
| 760 | /* |
| 761 | * If this is a new and good card, register it. |
| 762 | */ |
| 763 | if (!mmc_card_present(card) && !mmc_card_dead(card)) { |
| 764 | if (mmc_register_card(card)) |
| 765 | mmc_card_set_dead(card); |
| 766 | else |
| 767 | mmc_card_set_present(card); |
| 768 | } |
| 769 | |
| 770 | /* |
| 771 | * If this card is dead, destroy it. |
| 772 | */ |
| 773 | if (mmc_card_dead(card)) { |
| 774 | list_del(&card->node); |
| 775 | mmc_remove_card(card); |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | /* |
| 780 | * If we discover that there are no cards on the |
| 781 | * bus, turn off the clock and power down. |
| 782 | */ |
| 783 | if (list_empty(&host->cards)) |
| 784 | mmc_power_off(host); |
| 785 | } |
| 786 | |
| 787 | |
| 788 | /** |
| 789 | * mmc_alloc_host - initialise the per-host structure. |
| 790 | * @extra: sizeof private data structure |
| 791 | * @dev: pointer to host device model structure |
| 792 | * |
| 793 | * Initialise the per-host structure. |
| 794 | */ |
| 795 | struct mmc_host *mmc_alloc_host(int extra, struct device *dev) |
| 796 | { |
| 797 | struct mmc_host *host; |
| 798 | |
Russell King | 00b137c | 2005-08-19 09:41:24 +0100 | [diff] [blame] | 799 | host = mmc_alloc_host_sysfs(extra, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | if (host) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | spin_lock_init(&host->lock); |
| 802 | init_waitqueue_head(&host->wq); |
| 803 | INIT_LIST_HEAD(&host->cards); |
| 804 | INIT_WORK(&host->detect, mmc_rescan, host); |
| 805 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | /* |
| 807 | * By default, hosts do not support SGIO or large requests. |
| 808 | * They have to set these according to their abilities. |
| 809 | */ |
| 810 | host->max_hw_segs = 1; |
| 811 | host->max_phys_segs = 1; |
| 812 | host->max_sectors = 1 << (PAGE_CACHE_SHIFT - 9); |
| 813 | host->max_seg_size = PAGE_CACHE_SIZE; |
| 814 | } |
| 815 | |
| 816 | return host; |
| 817 | } |
| 818 | |
| 819 | EXPORT_SYMBOL(mmc_alloc_host); |
| 820 | |
| 821 | /** |
| 822 | * mmc_add_host - initialise host hardware |
| 823 | * @host: mmc host |
| 824 | */ |
| 825 | int mmc_add_host(struct mmc_host *host) |
| 826 | { |
Russell King | 00b137c | 2005-08-19 09:41:24 +0100 | [diff] [blame] | 827 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | |
Russell King | 00b137c | 2005-08-19 09:41:24 +0100 | [diff] [blame] | 829 | ret = mmc_add_host_sysfs(host); |
| 830 | if (ret == 0) { |
| 831 | mmc_power_off(host); |
| 832 | mmc_detect_change(host); |
| 833 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | |
Russell King | 00b137c | 2005-08-19 09:41:24 +0100 | [diff] [blame] | 835 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | EXPORT_SYMBOL(mmc_add_host); |
| 839 | |
| 840 | /** |
| 841 | * mmc_remove_host - remove host hardware |
| 842 | * @host: mmc host |
| 843 | * |
| 844 | * Unregister and remove all cards associated with this host, |
| 845 | * and power down the MMC bus. |
| 846 | */ |
| 847 | void mmc_remove_host(struct mmc_host *host) |
| 848 | { |
| 849 | struct list_head *l, *n; |
| 850 | |
| 851 | list_for_each_safe(l, n, &host->cards) { |
| 852 | struct mmc_card *card = mmc_list_to_card(l); |
| 853 | |
| 854 | mmc_remove_card(card); |
| 855 | } |
| 856 | |
| 857 | mmc_power_off(host); |
Russell King | 00b137c | 2005-08-19 09:41:24 +0100 | [diff] [blame] | 858 | mmc_remove_host_sysfs(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | EXPORT_SYMBOL(mmc_remove_host); |
| 862 | |
| 863 | /** |
| 864 | * mmc_free_host - free the host structure |
| 865 | * @host: mmc host |
| 866 | * |
| 867 | * Free the host once all references to it have been dropped. |
| 868 | */ |
| 869 | void mmc_free_host(struct mmc_host *host) |
| 870 | { |
| 871 | flush_scheduled_work(); |
Russell King | 00b137c | 2005-08-19 09:41:24 +0100 | [diff] [blame] | 872 | mmc_free_host_sysfs(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | EXPORT_SYMBOL(mmc_free_host); |
| 876 | |
| 877 | #ifdef CONFIG_PM |
| 878 | |
| 879 | /** |
| 880 | * mmc_suspend_host - suspend a host |
| 881 | * @host: mmc host |
| 882 | * @state: suspend mode (PM_SUSPEND_xxx) |
| 883 | */ |
Pavel Machek | e5378ca | 2005-04-16 15:25:29 -0700 | [diff] [blame] | 884 | int mmc_suspend_host(struct mmc_host *host, pm_message_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | { |
| 886 | mmc_claim_host(host); |
| 887 | mmc_deselect_cards(host); |
| 888 | mmc_power_off(host); |
| 889 | mmc_release_host(host); |
| 890 | |
| 891 | return 0; |
| 892 | } |
| 893 | |
| 894 | EXPORT_SYMBOL(mmc_suspend_host); |
| 895 | |
| 896 | /** |
| 897 | * mmc_resume_host - resume a previously suspended host |
| 898 | * @host: mmc host |
| 899 | */ |
| 900 | int mmc_resume_host(struct mmc_host *host) |
| 901 | { |
| 902 | mmc_detect_change(host); |
| 903 | |
| 904 | return 0; |
| 905 | } |
| 906 | |
| 907 | EXPORT_SYMBOL(mmc_resume_host); |
| 908 | |
| 909 | #endif |
| 910 | |
| 911 | MODULE_LICENSE("GPL"); |