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> |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 19 | #include <asm/scatterlist.h> |
| 20 | #include <linux/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | #include <linux/mmc/card.h> |
| 23 | #include <linux/mmc/host.h> |
| 24 | #include <linux/mmc/protocol.h> |
| 25 | |
| 26 | #include "mmc.h" |
| 27 | |
| 28 | #ifdef CONFIG_MMC_DEBUG |
| 29 | #define DBG(x...) printk(KERN_DEBUG x) |
| 30 | #else |
| 31 | #define DBG(x...) do { } while (0) |
| 32 | #endif |
| 33 | |
| 34 | #define CMD_RETRIES 3 |
| 35 | |
| 36 | /* |
| 37 | * OCR Bit positions to 10s of Vdd mV. |
| 38 | */ |
| 39 | static const unsigned short mmc_ocr_bit_to_vdd[] = { |
| 40 | 150, 155, 160, 165, 170, 180, 190, 200, |
| 41 | 210, 220, 230, 240, 250, 260, 270, 280, |
| 42 | 290, 300, 310, 320, 330, 340, 350, 360 |
| 43 | }; |
| 44 | |
| 45 | static const unsigned int tran_exp[] = { |
| 46 | 10000, 100000, 1000000, 10000000, |
| 47 | 0, 0, 0, 0 |
| 48 | }; |
| 49 | |
| 50 | static const unsigned char tran_mant[] = { |
| 51 | 0, 10, 12, 13, 15, 20, 25, 30, |
| 52 | 35, 40, 45, 50, 55, 60, 70, 80, |
| 53 | }; |
| 54 | |
| 55 | static const unsigned int tacc_exp[] = { |
| 56 | 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, |
| 57 | }; |
| 58 | |
| 59 | static const unsigned int tacc_mant[] = { |
| 60 | 0, 10, 12, 13, 15, 20, 25, 30, |
| 61 | 35, 40, 45, 50, 55, 60, 70, 80, |
| 62 | }; |
| 63 | |
| 64 | |
| 65 | /** |
| 66 | * mmc_request_done - finish processing an MMC command |
| 67 | * @host: MMC host which completed command |
| 68 | * @mrq: MMC request which completed |
| 69 | * |
| 70 | * MMC drivers should call this function when they have completed |
| 71 | * their processing of a command. This should be called before the |
| 72 | * data part of the command has completed. |
| 73 | */ |
| 74 | void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq) |
| 75 | { |
| 76 | struct mmc_command *cmd = mrq->cmd; |
| 77 | int err = mrq->cmd->error; |
| 78 | DBG("MMC: req done (%02x): %d: %08x %08x %08x %08x\n", cmd->opcode, |
| 79 | err, cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]); |
| 80 | |
| 81 | if (err && cmd->retries) { |
| 82 | cmd->retries--; |
| 83 | cmd->error = 0; |
| 84 | host->ops->request(host, mrq); |
| 85 | } else if (mrq->done) { |
| 86 | mrq->done(mrq); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | EXPORT_SYMBOL(mmc_request_done); |
| 91 | |
| 92 | /** |
| 93 | * mmc_start_request - start a command on a host |
| 94 | * @host: MMC host to start command on |
| 95 | * @mrq: MMC request to start |
| 96 | * |
| 97 | * Queue a command on the specified host. We expect the |
| 98 | * caller to be holding the host lock with interrupts disabled. |
| 99 | */ |
| 100 | void |
| 101 | mmc_start_request(struct mmc_host *host, struct mmc_request *mrq) |
| 102 | { |
| 103 | DBG("MMC: starting cmd %02x arg %08x flags %08x\n", |
| 104 | mrq->cmd->opcode, mrq->cmd->arg, mrq->cmd->flags); |
| 105 | |
| 106 | WARN_ON(host->card_busy == NULL); |
| 107 | |
| 108 | mrq->cmd->error = 0; |
| 109 | mrq->cmd->mrq = mrq; |
| 110 | if (mrq->data) { |
| 111 | mrq->cmd->data = mrq->data; |
| 112 | mrq->data->error = 0; |
| 113 | mrq->data->mrq = mrq; |
| 114 | if (mrq->stop) { |
| 115 | mrq->data->stop = mrq->stop; |
| 116 | mrq->stop->error = 0; |
| 117 | mrq->stop->mrq = mrq; |
| 118 | } |
| 119 | } |
| 120 | host->ops->request(host, mrq); |
| 121 | } |
| 122 | |
| 123 | EXPORT_SYMBOL(mmc_start_request); |
| 124 | |
| 125 | static void mmc_wait_done(struct mmc_request *mrq) |
| 126 | { |
| 127 | complete(mrq->done_data); |
| 128 | } |
| 129 | |
| 130 | int mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq) |
| 131 | { |
| 132 | DECLARE_COMPLETION(complete); |
| 133 | |
| 134 | mrq->done_data = &complete; |
| 135 | mrq->done = mmc_wait_done; |
| 136 | |
| 137 | mmc_start_request(host, mrq); |
| 138 | |
| 139 | wait_for_completion(&complete); |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | EXPORT_SYMBOL(mmc_wait_for_req); |
| 145 | |
| 146 | /** |
| 147 | * mmc_wait_for_cmd - start a command and wait for completion |
| 148 | * @host: MMC host to start command |
| 149 | * @cmd: MMC command to start |
| 150 | * @retries: maximum number of retries |
| 151 | * |
| 152 | * Start a new MMC command for a host, and wait for the command |
| 153 | * to complete. Return any error that occurred while the command |
| 154 | * was executing. Do not attempt to parse the response. |
| 155 | */ |
| 156 | int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries) |
| 157 | { |
| 158 | struct mmc_request mrq; |
| 159 | |
| 160 | BUG_ON(host->card_busy == NULL); |
| 161 | |
| 162 | memset(&mrq, 0, sizeof(struct mmc_request)); |
| 163 | |
| 164 | memset(cmd->resp, 0, sizeof(cmd->resp)); |
| 165 | cmd->retries = retries; |
| 166 | |
| 167 | mrq.cmd = cmd; |
| 168 | cmd->data = NULL; |
| 169 | |
| 170 | mmc_wait_for_req(host, &mrq); |
| 171 | |
| 172 | return cmd->error; |
| 173 | } |
| 174 | |
| 175 | EXPORT_SYMBOL(mmc_wait_for_cmd); |
| 176 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 177 | /** |
| 178 | * mmc_wait_for_app_cmd - start an application command and wait for |
| 179 | completion |
| 180 | * @host: MMC host to start command |
| 181 | * @rca: RCA to send MMC_APP_CMD to |
| 182 | * @cmd: MMC command to start |
| 183 | * @retries: maximum number of retries |
| 184 | * |
| 185 | * Sends a MMC_APP_CMD, checks the card response, sends the command |
| 186 | * in the parameter and waits for it to complete. Return any error |
| 187 | * that occurred while the command was executing. Do not attempt to |
| 188 | * parse the response. |
| 189 | */ |
| 190 | int mmc_wait_for_app_cmd(struct mmc_host *host, unsigned int rca, |
| 191 | struct mmc_command *cmd, int retries) |
| 192 | { |
| 193 | struct mmc_request mrq; |
| 194 | struct mmc_command appcmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 196 | int i, err; |
| 197 | |
| 198 | BUG_ON(host->card_busy == NULL); |
| 199 | BUG_ON(retries < 0); |
| 200 | |
| 201 | err = MMC_ERR_INVALID; |
| 202 | |
| 203 | /* |
| 204 | * We have to resend MMC_APP_CMD for each attempt so |
| 205 | * we cannot use the retries field in mmc_command. |
| 206 | */ |
| 207 | for (i = 0;i <= retries;i++) { |
| 208 | memset(&mrq, 0, sizeof(struct mmc_request)); |
| 209 | |
| 210 | appcmd.opcode = MMC_APP_CMD; |
| 211 | appcmd.arg = rca << 16; |
| 212 | appcmd.flags = MMC_RSP_R1; |
| 213 | appcmd.retries = 0; |
| 214 | memset(appcmd.resp, 0, sizeof(appcmd.resp)); |
| 215 | appcmd.data = NULL; |
| 216 | |
| 217 | mrq.cmd = &appcmd; |
| 218 | appcmd.data = NULL; |
| 219 | |
| 220 | mmc_wait_for_req(host, &mrq); |
| 221 | |
| 222 | if (appcmd.error) { |
| 223 | err = appcmd.error; |
| 224 | continue; |
| 225 | } |
| 226 | |
| 227 | /* Check that card supported application commands */ |
| 228 | if (!(appcmd.resp[0] & R1_APP_CMD)) |
| 229 | return MMC_ERR_FAILED; |
| 230 | |
| 231 | memset(&mrq, 0, sizeof(struct mmc_request)); |
| 232 | |
| 233 | memset(cmd->resp, 0, sizeof(cmd->resp)); |
| 234 | cmd->retries = 0; |
| 235 | |
| 236 | mrq.cmd = cmd; |
| 237 | cmd->data = NULL; |
| 238 | |
| 239 | mmc_wait_for_req(host, &mrq); |
| 240 | |
| 241 | err = cmd->error; |
| 242 | if (cmd->error == MMC_ERR_NONE) |
| 243 | break; |
| 244 | } |
| 245 | |
| 246 | return err; |
| 247 | } |
| 248 | |
| 249 | EXPORT_SYMBOL(mmc_wait_for_app_cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 251 | static int mmc_select_card(struct mmc_host *host, struct mmc_card *card); |
| 252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | /** |
| 254 | * __mmc_claim_host - exclusively claim a host |
| 255 | * @host: mmc host to claim |
| 256 | * @card: mmc card to claim host for |
| 257 | * |
| 258 | * Claim a host for a set of operations. If a valid card |
| 259 | * is passed and this wasn't the last card selected, select |
| 260 | * the card before returning. |
| 261 | * |
| 262 | * Note: you should use mmc_card_claim_host or mmc_claim_host. |
| 263 | */ |
| 264 | int __mmc_claim_host(struct mmc_host *host, struct mmc_card *card) |
| 265 | { |
| 266 | DECLARE_WAITQUEUE(wait, current); |
| 267 | unsigned long flags; |
| 268 | int err = 0; |
| 269 | |
| 270 | add_wait_queue(&host->wq, &wait); |
| 271 | spin_lock_irqsave(&host->lock, flags); |
| 272 | while (1) { |
| 273 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 274 | if (host->card_busy == NULL) |
| 275 | break; |
| 276 | spin_unlock_irqrestore(&host->lock, flags); |
| 277 | schedule(); |
| 278 | spin_lock_irqsave(&host->lock, flags); |
| 279 | } |
| 280 | set_current_state(TASK_RUNNING); |
| 281 | host->card_busy = card; |
| 282 | spin_unlock_irqrestore(&host->lock, flags); |
| 283 | remove_wait_queue(&host->wq, &wait); |
| 284 | |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 285 | if (card != (void *)-1) { |
| 286 | err = mmc_select_card(host, card); |
| 287 | if (err != MMC_ERR_NONE) |
| 288 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | return err; |
| 292 | } |
| 293 | |
| 294 | EXPORT_SYMBOL(__mmc_claim_host); |
| 295 | |
| 296 | /** |
| 297 | * mmc_release_host - release a host |
| 298 | * @host: mmc host to release |
| 299 | * |
| 300 | * Release a MMC host, allowing others to claim the host |
| 301 | * for their operations. |
| 302 | */ |
| 303 | void mmc_release_host(struct mmc_host *host) |
| 304 | { |
| 305 | unsigned long flags; |
| 306 | |
| 307 | BUG_ON(host->card_busy == NULL); |
| 308 | |
| 309 | spin_lock_irqsave(&host->lock, flags); |
| 310 | host->card_busy = NULL; |
| 311 | spin_unlock_irqrestore(&host->lock, flags); |
| 312 | |
| 313 | wake_up(&host->wq); |
| 314 | } |
| 315 | |
| 316 | EXPORT_SYMBOL(mmc_release_host); |
| 317 | |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 318 | static int mmc_select_card(struct mmc_host *host, struct mmc_card *card) |
| 319 | { |
| 320 | int err; |
| 321 | struct mmc_command cmd; |
| 322 | |
| 323 | BUG_ON(host->card_busy == NULL); |
| 324 | |
| 325 | if (host->card_selected == card) |
| 326 | return MMC_ERR_NONE; |
| 327 | |
| 328 | host->card_selected = card; |
| 329 | |
| 330 | cmd.opcode = MMC_SELECT_CARD; |
| 331 | cmd.arg = card->rca << 16; |
| 332 | cmd.flags = MMC_RSP_R1; |
| 333 | |
| 334 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); |
| 335 | if (err != MMC_ERR_NONE) |
| 336 | return err; |
| 337 | |
Pierre Ossman | f218278 | 2005-09-06 15:18:55 -0700 | [diff] [blame^] | 338 | /* |
| 339 | * Default bus width is 1 bit. |
| 340 | */ |
| 341 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
| 342 | |
| 343 | /* |
| 344 | * We can only change the bus width of the selected |
| 345 | * card so therefore we have to put the handling |
| 346 | * here. |
| 347 | */ |
| 348 | if (host->caps & MMC_CAP_4_BIT_DATA) { |
| 349 | /* |
| 350 | * The card is in 1 bit mode by default so |
| 351 | * we only need to change if it supports the |
| 352 | * wider version. |
| 353 | */ |
| 354 | if (mmc_card_sd(card) && |
| 355 | (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) { |
| 356 | struct mmc_command cmd; |
| 357 | cmd.opcode = SD_APP_SET_BUS_WIDTH; |
| 358 | cmd.arg = SD_BUS_WIDTH_4; |
| 359 | cmd.flags = MMC_RSP_R1; |
| 360 | |
| 361 | err = mmc_wait_for_app_cmd(host, card->rca, &cmd, |
| 362 | CMD_RETRIES); |
| 363 | if (err != MMC_ERR_NONE) |
| 364 | return err; |
| 365 | |
| 366 | host->ios.bus_width = MMC_BUS_WIDTH_4; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | host->ops->set_ios(host, &host->ios); |
| 371 | |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 372 | return MMC_ERR_NONE; |
| 373 | } |
| 374 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | /* |
| 376 | * Ensure that no card is selected. |
| 377 | */ |
| 378 | static void mmc_deselect_cards(struct mmc_host *host) |
| 379 | { |
| 380 | struct mmc_command cmd; |
| 381 | |
| 382 | if (host->card_selected) { |
| 383 | host->card_selected = NULL; |
| 384 | |
| 385 | cmd.opcode = MMC_SELECT_CARD; |
| 386 | cmd.arg = 0; |
| 387 | cmd.flags = MMC_RSP_NONE; |
| 388 | |
| 389 | mmc_wait_for_cmd(host, &cmd, 0); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | |
| 394 | static inline void mmc_delay(unsigned int ms) |
| 395 | { |
| 396 | if (ms < HZ / 1000) { |
| 397 | yield(); |
| 398 | mdelay(ms); |
| 399 | } else { |
| 400 | msleep_interruptible (ms); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * Mask off any voltages we don't support and select |
| 406 | * the lowest voltage |
| 407 | */ |
| 408 | static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr) |
| 409 | { |
| 410 | int bit; |
| 411 | |
| 412 | ocr &= host->ocr_avail; |
| 413 | |
| 414 | bit = ffs(ocr); |
| 415 | if (bit) { |
| 416 | bit -= 1; |
| 417 | |
| 418 | ocr = 3 << bit; |
| 419 | |
| 420 | host->ios.vdd = bit; |
| 421 | host->ops->set_ios(host, &host->ios); |
| 422 | } else { |
| 423 | ocr = 0; |
| 424 | } |
| 425 | |
| 426 | return ocr; |
| 427 | } |
| 428 | |
| 429 | #define UNSTUFF_BITS(resp,start,size) \ |
| 430 | ({ \ |
| 431 | const int __size = size; \ |
| 432 | const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \ |
| 433 | const int __off = 3 - ((start) / 32); \ |
| 434 | const int __shft = (start) & 31; \ |
| 435 | u32 __res; \ |
| 436 | \ |
| 437 | __res = resp[__off] >> __shft; \ |
| 438 | if (__size + __shft > 32) \ |
| 439 | __res |= resp[__off-1] << ((32 - __shft) % 32); \ |
| 440 | __res & __mask; \ |
| 441 | }) |
| 442 | |
| 443 | /* |
| 444 | * Given the decoded CSD structure, decode the raw CID to our CID structure. |
| 445 | */ |
| 446 | static void mmc_decode_cid(struct mmc_card *card) |
| 447 | { |
| 448 | u32 *resp = card->raw_cid; |
| 449 | |
| 450 | memset(&card->cid, 0, sizeof(struct mmc_cid)); |
| 451 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 452 | if (mmc_card_sd(card)) { |
| 453 | /* |
| 454 | * SD doesn't currently have a version field so we will |
| 455 | * have to assume we can parse this. |
| 456 | */ |
| 457 | card->cid.manfid = UNSTUFF_BITS(resp, 120, 8); |
| 458 | card->cid.oemid = UNSTUFF_BITS(resp, 104, 16); |
| 459 | card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); |
| 460 | card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); |
| 461 | card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); |
| 462 | card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); |
| 463 | card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); |
| 464 | card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4); |
| 465 | card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4); |
| 466 | card->cid.serial = UNSTUFF_BITS(resp, 24, 32); |
| 467 | card->cid.year = UNSTUFF_BITS(resp, 12, 8); |
| 468 | card->cid.month = UNSTUFF_BITS(resp, 8, 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 470 | card->cid.year += 2000; /* SD cards year offset */ |
Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 471 | } else { |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 472 | /* |
| 473 | * The selection of the format here is based upon published |
| 474 | * specs from sandisk and from what people have reported. |
| 475 | */ |
| 476 | switch (card->csd.mmca_vsn) { |
| 477 | case 0: /* MMC v1.0 - v1.2 */ |
| 478 | case 1: /* MMC v1.4 */ |
| 479 | card->cid.manfid = UNSTUFF_BITS(resp, 104, 24); |
| 480 | card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); |
| 481 | card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); |
| 482 | card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); |
| 483 | card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); |
| 484 | card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); |
| 485 | card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8); |
| 486 | card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8); |
| 487 | card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4); |
| 488 | card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4); |
| 489 | card->cid.serial = UNSTUFF_BITS(resp, 16, 24); |
| 490 | card->cid.month = UNSTUFF_BITS(resp, 12, 4); |
| 491 | card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997; |
| 492 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 494 | case 2: /* MMC v2.0 - v2.2 */ |
| 495 | case 3: /* MMC v3.1 - v3.3 */ |
| 496 | card->cid.manfid = UNSTUFF_BITS(resp, 120, 8); |
| 497 | card->cid.oemid = UNSTUFF_BITS(resp, 104, 16); |
| 498 | card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); |
| 499 | card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); |
| 500 | card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); |
| 501 | card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); |
| 502 | card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); |
| 503 | card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8); |
| 504 | card->cid.serial = UNSTUFF_BITS(resp, 16, 32); |
| 505 | card->cid.month = UNSTUFF_BITS(resp, 12, 4); |
| 506 | card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997; |
| 507 | break; |
| 508 | |
| 509 | default: |
| 510 | printk("%s: card has unknown MMCA version %d\n", |
| 511 | mmc_hostname(card->host), card->csd.mmca_vsn); |
| 512 | mmc_card_set_bad(card); |
| 513 | break; |
| 514 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | |
| 518 | /* |
| 519 | * Given a 128-bit response, decode to our card CSD structure. |
| 520 | */ |
| 521 | static void mmc_decode_csd(struct mmc_card *card) |
| 522 | { |
| 523 | struct mmc_csd *csd = &card->csd; |
| 524 | unsigned int e, m, csd_struct; |
| 525 | u32 *resp = card->raw_csd; |
| 526 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 527 | if (mmc_card_sd(card)) { |
| 528 | csd_struct = UNSTUFF_BITS(resp, 126, 2); |
| 529 | if (csd_struct != 0) { |
| 530 | printk("%s: unrecognised CSD structure version %d\n", |
| 531 | mmc_hostname(card->host), csd_struct); |
| 532 | mmc_card_set_bad(card); |
| 533 | return; |
| 534 | } |
| 535 | |
| 536 | m = UNSTUFF_BITS(resp, 115, 4); |
| 537 | e = UNSTUFF_BITS(resp, 112, 3); |
| 538 | csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10; |
| 539 | csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100; |
| 540 | |
| 541 | m = UNSTUFF_BITS(resp, 99, 4); |
| 542 | e = UNSTUFF_BITS(resp, 96, 3); |
| 543 | csd->max_dtr = tran_exp[e] * tran_mant[m]; |
| 544 | csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); |
| 545 | |
| 546 | e = UNSTUFF_BITS(resp, 47, 3); |
| 547 | m = UNSTUFF_BITS(resp, 62, 12); |
| 548 | csd->capacity = (1 + m) << (e + 2); |
| 549 | |
| 550 | csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); |
Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 551 | } else { |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 552 | /* |
| 553 | * We only understand CSD structure v1.1 and v1.2. |
| 554 | * v1.2 has extra information in bits 15, 11 and 10. |
| 555 | */ |
| 556 | csd_struct = UNSTUFF_BITS(resp, 126, 2); |
| 557 | if (csd_struct != 1 && csd_struct != 2) { |
| 558 | printk("%s: unrecognised CSD structure version %d\n", |
| 559 | mmc_hostname(card->host), csd_struct); |
| 560 | mmc_card_set_bad(card); |
| 561 | return; |
| 562 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 564 | csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4); |
| 565 | m = UNSTUFF_BITS(resp, 115, 4); |
| 566 | e = UNSTUFF_BITS(resp, 112, 3); |
| 567 | csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10; |
| 568 | csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 570 | m = UNSTUFF_BITS(resp, 99, 4); |
| 571 | e = UNSTUFF_BITS(resp, 96, 3); |
| 572 | csd->max_dtr = tran_exp[e] * tran_mant[m]; |
| 573 | csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 575 | e = UNSTUFF_BITS(resp, 47, 3); |
| 576 | m = UNSTUFF_BITS(resp, 62, 12); |
| 577 | csd->capacity = (1 + m) << (e + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 579 | csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); |
| 580 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /* |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 584 | * Given a 64-bit response, decode to our card SCR structure. |
| 585 | */ |
| 586 | static void mmc_decode_scr(struct mmc_card *card) |
| 587 | { |
| 588 | struct sd_scr *scr = &card->scr; |
| 589 | unsigned int scr_struct; |
| 590 | u32 resp[4]; |
| 591 | |
| 592 | BUG_ON(!mmc_card_sd(card)); |
| 593 | |
| 594 | resp[3] = card->raw_scr[1]; |
| 595 | resp[2] = card->raw_scr[0]; |
| 596 | |
| 597 | scr_struct = UNSTUFF_BITS(resp, 60, 4); |
| 598 | if (scr_struct != 0) { |
| 599 | printk("%s: unrecognised SCR structure version %d\n", |
| 600 | mmc_hostname(card->host), scr_struct); |
| 601 | mmc_card_set_bad(card); |
| 602 | return; |
| 603 | } |
| 604 | |
| 605 | scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4); |
| 606 | scr->bus_widths = UNSTUFF_BITS(resp, 48, 4); |
| 607 | } |
| 608 | |
| 609 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | * Locate a MMC card on this MMC host given a raw CID. |
| 611 | */ |
| 612 | static struct mmc_card *mmc_find_card(struct mmc_host *host, u32 *raw_cid) |
| 613 | { |
| 614 | struct mmc_card *card; |
| 615 | |
| 616 | list_for_each_entry(card, &host->cards, node) { |
| 617 | if (memcmp(card->raw_cid, raw_cid, sizeof(card->raw_cid)) == 0) |
| 618 | return card; |
| 619 | } |
| 620 | return NULL; |
| 621 | } |
| 622 | |
| 623 | /* |
| 624 | * Allocate a new MMC card, and assign a unique RCA. |
| 625 | */ |
| 626 | static struct mmc_card * |
| 627 | mmc_alloc_card(struct mmc_host *host, u32 *raw_cid, unsigned int *frca) |
| 628 | { |
| 629 | struct mmc_card *card, *c; |
| 630 | unsigned int rca = *frca; |
| 631 | |
| 632 | card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL); |
| 633 | if (!card) |
| 634 | return ERR_PTR(-ENOMEM); |
| 635 | |
| 636 | mmc_init_card(card, host); |
| 637 | memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid)); |
| 638 | |
| 639 | again: |
| 640 | list_for_each_entry(c, &host->cards, node) |
| 641 | if (c->rca == rca) { |
| 642 | rca++; |
| 643 | goto again; |
| 644 | } |
| 645 | |
| 646 | card->rca = rca; |
| 647 | |
| 648 | *frca = rca; |
| 649 | |
| 650 | return card; |
| 651 | } |
| 652 | |
| 653 | /* |
| 654 | * Tell attached cards to go to IDLE state |
| 655 | */ |
| 656 | static void mmc_idle_cards(struct mmc_host *host) |
| 657 | { |
| 658 | struct mmc_command cmd; |
| 659 | |
Pierre Ossman | 865e9f1 | 2005-09-03 16:45:02 +0100 | [diff] [blame] | 660 | host->ios.chip_select = MMC_CS_HIGH; |
| 661 | host->ops->set_ios(host, &host->ios); |
| 662 | |
| 663 | mmc_delay(1); |
| 664 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | cmd.opcode = MMC_GO_IDLE_STATE; |
| 666 | cmd.arg = 0; |
| 667 | cmd.flags = MMC_RSP_NONE; |
| 668 | |
| 669 | mmc_wait_for_cmd(host, &cmd, 0); |
| 670 | |
| 671 | mmc_delay(1); |
Pierre Ossman | 865e9f1 | 2005-09-03 16:45:02 +0100 | [diff] [blame] | 672 | |
| 673 | host->ios.chip_select = MMC_CS_DONTCARE; |
| 674 | host->ops->set_ios(host, &host->ios); |
| 675 | |
| 676 | mmc_delay(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | /* |
| 680 | * Apply power to the MMC stack. |
| 681 | */ |
| 682 | static void mmc_power_up(struct mmc_host *host) |
| 683 | { |
| 684 | int bit = fls(host->ocr_avail) - 1; |
| 685 | |
| 686 | host->ios.vdd = bit; |
| 687 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; |
Pierre Ossman | 865e9f1 | 2005-09-03 16:45:02 +0100 | [diff] [blame] | 688 | host->ios.chip_select = MMC_CS_DONTCARE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | host->ios.power_mode = MMC_POWER_UP; |
Pierre Ossman | f218278 | 2005-09-06 15:18:55 -0700 | [diff] [blame^] | 690 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | host->ops->set_ios(host, &host->ios); |
| 692 | |
| 693 | mmc_delay(1); |
| 694 | |
| 695 | host->ios.clock = host->f_min; |
| 696 | host->ios.power_mode = MMC_POWER_ON; |
| 697 | host->ops->set_ios(host, &host->ios); |
| 698 | |
| 699 | mmc_delay(2); |
| 700 | } |
| 701 | |
| 702 | static void mmc_power_off(struct mmc_host *host) |
| 703 | { |
| 704 | host->ios.clock = 0; |
| 705 | host->ios.vdd = 0; |
| 706 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; |
Pierre Ossman | 865e9f1 | 2005-09-03 16:45:02 +0100 | [diff] [blame] | 707 | host->ios.chip_select = MMC_CS_DONTCARE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | host->ios.power_mode = MMC_POWER_OFF; |
Pierre Ossman | f218278 | 2005-09-06 15:18:55 -0700 | [diff] [blame^] | 709 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | host->ops->set_ios(host, &host->ios); |
| 711 | } |
| 712 | |
| 713 | static int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr) |
| 714 | { |
| 715 | struct mmc_command cmd; |
| 716 | int i, err = 0; |
| 717 | |
| 718 | cmd.opcode = MMC_SEND_OP_COND; |
| 719 | cmd.arg = ocr; |
| 720 | cmd.flags = MMC_RSP_R3; |
| 721 | |
| 722 | for (i = 100; i; i--) { |
| 723 | err = mmc_wait_for_cmd(host, &cmd, 0); |
| 724 | if (err != MMC_ERR_NONE) |
| 725 | break; |
| 726 | |
| 727 | if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0) |
| 728 | break; |
| 729 | |
| 730 | err = MMC_ERR_TIMEOUT; |
| 731 | |
| 732 | mmc_delay(10); |
| 733 | } |
| 734 | |
| 735 | if (rocr) |
| 736 | *rocr = cmd.resp[0]; |
| 737 | |
| 738 | return err; |
| 739 | } |
| 740 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 741 | static int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr) |
| 742 | { |
| 743 | struct mmc_command cmd; |
| 744 | int i, err = 0; |
| 745 | |
| 746 | cmd.opcode = SD_APP_OP_COND; |
| 747 | cmd.arg = ocr; |
| 748 | cmd.flags = MMC_RSP_R3; |
| 749 | |
| 750 | for (i = 100; i; i--) { |
| 751 | err = mmc_wait_for_app_cmd(host, 0, &cmd, CMD_RETRIES); |
| 752 | if (err != MMC_ERR_NONE) |
| 753 | break; |
| 754 | |
| 755 | if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0) |
| 756 | break; |
| 757 | |
| 758 | err = MMC_ERR_TIMEOUT; |
| 759 | |
| 760 | mmc_delay(10); |
| 761 | } |
| 762 | |
| 763 | if (rocr) |
| 764 | *rocr = cmd.resp[0]; |
| 765 | |
| 766 | return err; |
| 767 | } |
| 768 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | /* |
| 770 | * Discover cards by requesting their CID. If this command |
| 771 | * times out, it is not an error; there are no further cards |
| 772 | * to be discovered. Add new cards to the list. |
| 773 | * |
| 774 | * Create a mmc_card entry for each discovered card, assigning |
| 775 | * it an RCA, and save the raw CID for decoding later. |
| 776 | */ |
| 777 | static void mmc_discover_cards(struct mmc_host *host) |
| 778 | { |
| 779 | struct mmc_card *card; |
| 780 | unsigned int first_rca = 1, err; |
| 781 | |
| 782 | while (1) { |
| 783 | struct mmc_command cmd; |
| 784 | |
| 785 | cmd.opcode = MMC_ALL_SEND_CID; |
| 786 | cmd.arg = 0; |
| 787 | cmd.flags = MMC_RSP_R2; |
| 788 | |
| 789 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); |
| 790 | if (err == MMC_ERR_TIMEOUT) { |
| 791 | err = MMC_ERR_NONE; |
| 792 | break; |
| 793 | } |
| 794 | if (err != MMC_ERR_NONE) { |
| 795 | printk(KERN_ERR "%s: error requesting CID: %d\n", |
Russell King | d366b64 | 2005-08-19 09:40:08 +0100 | [diff] [blame] | 796 | mmc_hostname(host), err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | break; |
| 798 | } |
| 799 | |
| 800 | card = mmc_find_card(host, cmd.resp); |
| 801 | if (!card) { |
| 802 | card = mmc_alloc_card(host, cmd.resp, &first_rca); |
| 803 | if (IS_ERR(card)) { |
| 804 | err = PTR_ERR(card); |
| 805 | break; |
| 806 | } |
| 807 | list_add(&card->node, &host->cards); |
| 808 | } |
| 809 | |
| 810 | card->state &= ~MMC_STATE_DEAD; |
| 811 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 812 | if (host->mode == MMC_MODE_SD) { |
| 813 | mmc_card_set_sd(card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 815 | cmd.opcode = SD_SEND_RELATIVE_ADDR; |
| 816 | cmd.arg = 0; |
| 817 | cmd.flags = MMC_RSP_R1; |
| 818 | |
| 819 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); |
| 820 | if (err != MMC_ERR_NONE) |
| 821 | mmc_card_set_dead(card); |
Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 822 | else { |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 823 | card->rca = cmd.resp[0] >> 16; |
Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 824 | |
| 825 | if (!host->ops->get_ro) { |
| 826 | printk(KERN_WARNING "%s: host does not " |
| 827 | "support reading read-only " |
| 828 | "switch. assuming write-enable.\n", |
| 829 | mmc_hostname(host)); |
| 830 | } else { |
| 831 | if (host->ops->get_ro(host)) |
| 832 | mmc_card_set_readonly(card); |
| 833 | } |
| 834 | } |
| 835 | } else { |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 836 | cmd.opcode = MMC_SET_RELATIVE_ADDR; |
| 837 | cmd.arg = card->rca << 16; |
| 838 | cmd.flags = MMC_RSP_R1; |
| 839 | |
| 840 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); |
| 841 | if (err != MMC_ERR_NONE) |
| 842 | mmc_card_set_dead(card); |
| 843 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | } |
| 845 | } |
| 846 | |
| 847 | static void mmc_read_csds(struct mmc_host *host) |
| 848 | { |
| 849 | struct mmc_card *card; |
| 850 | |
| 851 | list_for_each_entry(card, &host->cards, node) { |
| 852 | struct mmc_command cmd; |
| 853 | int err; |
| 854 | |
| 855 | if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT)) |
| 856 | continue; |
| 857 | |
| 858 | cmd.opcode = MMC_SEND_CSD; |
| 859 | cmd.arg = card->rca << 16; |
| 860 | cmd.flags = MMC_RSP_R2; |
| 861 | |
| 862 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); |
| 863 | if (err != MMC_ERR_NONE) { |
| 864 | mmc_card_set_dead(card); |
| 865 | continue; |
| 866 | } |
| 867 | |
| 868 | memcpy(card->raw_csd, cmd.resp, sizeof(card->raw_csd)); |
| 869 | |
| 870 | mmc_decode_csd(card); |
| 871 | mmc_decode_cid(card); |
| 872 | } |
| 873 | } |
| 874 | |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 875 | static void mmc_read_scrs(struct mmc_host *host) |
| 876 | { |
| 877 | int err; |
| 878 | struct mmc_card *card; |
| 879 | |
| 880 | struct mmc_request mrq; |
| 881 | struct mmc_command cmd; |
| 882 | struct mmc_data data; |
| 883 | |
| 884 | struct scatterlist sg; |
| 885 | |
| 886 | list_for_each_entry(card, &host->cards, node) { |
| 887 | if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT)) |
| 888 | continue; |
| 889 | if (!mmc_card_sd(card)) |
| 890 | continue; |
| 891 | |
| 892 | err = mmc_select_card(host, card); |
| 893 | if (err != MMC_ERR_NONE) { |
| 894 | mmc_card_set_dead(card); |
| 895 | continue; |
| 896 | } |
| 897 | |
| 898 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 899 | |
| 900 | cmd.opcode = MMC_APP_CMD; |
| 901 | cmd.arg = card->rca << 16; |
| 902 | cmd.flags = MMC_RSP_R1; |
| 903 | |
| 904 | err = mmc_wait_for_cmd(host, &cmd, 0); |
| 905 | if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD)) { |
| 906 | mmc_card_set_dead(card); |
| 907 | continue; |
| 908 | } |
| 909 | |
| 910 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 911 | |
| 912 | cmd.opcode = SD_APP_SEND_SCR; |
| 913 | cmd.arg = 0; |
| 914 | cmd.flags = MMC_RSP_R1; |
| 915 | |
| 916 | memset(&data, 0, sizeof(struct mmc_data)); |
| 917 | |
| 918 | data.timeout_ns = card->csd.tacc_ns * 10; |
| 919 | data.timeout_clks = card->csd.tacc_clks * 10; |
| 920 | data.blksz_bits = 3; |
| 921 | data.blocks = 1; |
| 922 | data.flags = MMC_DATA_READ; |
| 923 | data.sg = &sg; |
| 924 | data.sg_len = 1; |
| 925 | |
| 926 | memset(&mrq, 0, sizeof(struct mmc_request)); |
| 927 | |
| 928 | mrq.cmd = &cmd; |
| 929 | mrq.data = &data; |
| 930 | |
| 931 | sg_init_one(&sg, (u8*)card->raw_scr, 8); |
| 932 | |
| 933 | err = mmc_wait_for_req(host, &mrq); |
| 934 | if (err != MMC_ERR_NONE) { |
| 935 | mmc_card_set_dead(card); |
| 936 | continue; |
| 937 | } |
| 938 | |
| 939 | card->raw_scr[0] = ntohl(card->raw_scr[0]); |
| 940 | card->raw_scr[1] = ntohl(card->raw_scr[1]); |
| 941 | |
| 942 | mmc_decode_scr(card); |
| 943 | } |
| 944 | |
| 945 | mmc_deselect_cards(host); |
| 946 | } |
| 947 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | static unsigned int mmc_calculate_clock(struct mmc_host *host) |
| 949 | { |
| 950 | struct mmc_card *card; |
| 951 | unsigned int max_dtr = host->f_max; |
| 952 | |
| 953 | list_for_each_entry(card, &host->cards, node) |
| 954 | if (!mmc_card_dead(card) && max_dtr > card->csd.max_dtr) |
| 955 | max_dtr = card->csd.max_dtr; |
| 956 | |
| 957 | DBG("MMC: selected %d.%03dMHz transfer rate\n", |
| 958 | max_dtr / 1000000, (max_dtr / 1000) % 1000); |
| 959 | |
| 960 | return max_dtr; |
| 961 | } |
| 962 | |
| 963 | /* |
| 964 | * Check whether cards we already know about are still present. |
| 965 | * We do this by requesting status, and checking whether a card |
| 966 | * responds. |
| 967 | * |
| 968 | * A request for status does not cause a state change in data |
| 969 | * transfer mode. |
| 970 | */ |
| 971 | static void mmc_check_cards(struct mmc_host *host) |
| 972 | { |
| 973 | struct list_head *l, *n; |
| 974 | |
| 975 | mmc_deselect_cards(host); |
| 976 | |
| 977 | list_for_each_safe(l, n, &host->cards) { |
| 978 | struct mmc_card *card = mmc_list_to_card(l); |
| 979 | struct mmc_command cmd; |
| 980 | int err; |
| 981 | |
| 982 | cmd.opcode = MMC_SEND_STATUS; |
| 983 | cmd.arg = card->rca << 16; |
| 984 | cmd.flags = MMC_RSP_R1; |
| 985 | |
| 986 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); |
| 987 | if (err == MMC_ERR_NONE) |
| 988 | continue; |
| 989 | |
| 990 | mmc_card_set_dead(card); |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | static void mmc_setup(struct mmc_host *host) |
| 995 | { |
| 996 | if (host->ios.power_mode != MMC_POWER_ON) { |
| 997 | int err; |
| 998 | u32 ocr; |
| 999 | |
Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 1000 | host->mode = MMC_MODE_SD; |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 1001 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | mmc_power_up(host); |
| 1003 | mmc_idle_cards(host); |
| 1004 | |
Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 1005 | err = mmc_send_app_op_cond(host, 0, &ocr); |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 1006 | |
| 1007 | /* |
Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 1008 | * If we fail to detect any SD cards then try |
| 1009 | * searching for MMC cards. |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 1010 | */ |
Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 1011 | if (err != MMC_ERR_NONE) { |
| 1012 | host->mode = MMC_MODE_MMC; |
| 1013 | |
| 1014 | err = mmc_send_op_cond(host, 0, &ocr); |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 1015 | if (err != MMC_ERR_NONE) |
| 1016 | return; |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 1017 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | |
| 1019 | host->ocr = mmc_select_voltage(host, ocr); |
| 1020 | |
| 1021 | /* |
| 1022 | * Since we're changing the OCR value, we seem to |
| 1023 | * need to tell some cards to go back to the idle |
| 1024 | * state. We wait 1ms to give cards time to |
| 1025 | * respond. |
| 1026 | */ |
| 1027 | if (host->ocr) |
| 1028 | mmc_idle_cards(host); |
| 1029 | } else { |
| 1030 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; |
| 1031 | host->ios.clock = host->f_min; |
| 1032 | host->ops->set_ios(host, &host->ios); |
| 1033 | |
| 1034 | /* |
| 1035 | * We should remember the OCR mask from the existing |
| 1036 | * cards, and detect the new cards OCR mask, combine |
| 1037 | * the two and re-select the VDD. However, if we do |
| 1038 | * change VDD, we should do an idle, and then do a |
| 1039 | * full re-initialisation. We would need to notify |
| 1040 | * drivers so that they can re-setup the cards as |
| 1041 | * well, while keeping their queues at bay. |
| 1042 | * |
| 1043 | * For the moment, we take the easy way out - if the |
| 1044 | * new cards don't like our currently selected VDD, |
| 1045 | * they drop off the bus. |
| 1046 | */ |
| 1047 | } |
| 1048 | |
| 1049 | if (host->ocr == 0) |
| 1050 | return; |
| 1051 | |
| 1052 | /* |
| 1053 | * Send the selected OCR multiple times... until the cards |
| 1054 | * all get the idea that they should be ready for CMD2. |
| 1055 | * (My SanDisk card seems to need this.) |
| 1056 | */ |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 1057 | if (host->mode == MMC_MODE_SD) |
| 1058 | mmc_send_app_op_cond(host, host->ocr, NULL); |
| 1059 | else |
| 1060 | mmc_send_op_cond(host, host->ocr, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | |
| 1062 | mmc_discover_cards(host); |
| 1063 | |
| 1064 | /* |
| 1065 | * Ok, now switch to push-pull mode. |
| 1066 | */ |
| 1067 | host->ios.bus_mode = MMC_BUSMODE_PUSHPULL; |
| 1068 | host->ops->set_ios(host, &host->ios); |
| 1069 | |
| 1070 | mmc_read_csds(host); |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 1071 | |
| 1072 | if (host->mode == MMC_MODE_SD) |
| 1073 | mmc_read_scrs(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | |
| 1077 | /** |
| 1078 | * mmc_detect_change - process change of state on a MMC socket |
| 1079 | * @host: host which changed state. |
| 1080 | * |
| 1081 | * All we know is that card(s) have been inserted or removed |
| 1082 | * from the socket(s). We don't know which socket or cards. |
| 1083 | */ |
| 1084 | void mmc_detect_change(struct mmc_host *host) |
| 1085 | { |
| 1086 | schedule_work(&host->detect); |
| 1087 | } |
| 1088 | |
| 1089 | EXPORT_SYMBOL(mmc_detect_change); |
| 1090 | |
| 1091 | |
| 1092 | static void mmc_rescan(void *data) |
| 1093 | { |
| 1094 | struct mmc_host *host = data; |
| 1095 | struct list_head *l, *n; |
| 1096 | |
| 1097 | mmc_claim_host(host); |
| 1098 | |
| 1099 | if (host->ios.power_mode == MMC_POWER_ON) |
| 1100 | mmc_check_cards(host); |
| 1101 | |
| 1102 | mmc_setup(host); |
| 1103 | |
| 1104 | if (!list_empty(&host->cards)) { |
| 1105 | /* |
| 1106 | * (Re-)calculate the fastest clock rate which the |
| 1107 | * attached cards and the host support. |
| 1108 | */ |
| 1109 | host->ios.clock = mmc_calculate_clock(host); |
| 1110 | host->ops->set_ios(host, &host->ios); |
| 1111 | } |
| 1112 | |
| 1113 | mmc_release_host(host); |
| 1114 | |
| 1115 | list_for_each_safe(l, n, &host->cards) { |
| 1116 | struct mmc_card *card = mmc_list_to_card(l); |
| 1117 | |
| 1118 | /* |
| 1119 | * If this is a new and good card, register it. |
| 1120 | */ |
| 1121 | if (!mmc_card_present(card) && !mmc_card_dead(card)) { |
| 1122 | if (mmc_register_card(card)) |
| 1123 | mmc_card_set_dead(card); |
| 1124 | else |
| 1125 | mmc_card_set_present(card); |
| 1126 | } |
| 1127 | |
| 1128 | /* |
| 1129 | * If this card is dead, destroy it. |
| 1130 | */ |
| 1131 | if (mmc_card_dead(card)) { |
| 1132 | list_del(&card->node); |
| 1133 | mmc_remove_card(card); |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | /* |
| 1138 | * If we discover that there are no cards on the |
| 1139 | * bus, turn off the clock and power down. |
| 1140 | */ |
| 1141 | if (list_empty(&host->cards)) |
| 1142 | mmc_power_off(host); |
| 1143 | } |
| 1144 | |
| 1145 | |
| 1146 | /** |
| 1147 | * mmc_alloc_host - initialise the per-host structure. |
| 1148 | * @extra: sizeof private data structure |
| 1149 | * @dev: pointer to host device model structure |
| 1150 | * |
| 1151 | * Initialise the per-host structure. |
| 1152 | */ |
| 1153 | struct mmc_host *mmc_alloc_host(int extra, struct device *dev) |
| 1154 | { |
| 1155 | struct mmc_host *host; |
| 1156 | |
Russell King | 00b137c | 2005-08-19 09:41:24 +0100 | [diff] [blame] | 1157 | host = mmc_alloc_host_sysfs(extra, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | if (host) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | spin_lock_init(&host->lock); |
| 1160 | init_waitqueue_head(&host->wq); |
| 1161 | INIT_LIST_HEAD(&host->cards); |
| 1162 | INIT_WORK(&host->detect, mmc_rescan, host); |
| 1163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | /* |
| 1165 | * By default, hosts do not support SGIO or large requests. |
| 1166 | * They have to set these according to their abilities. |
| 1167 | */ |
| 1168 | host->max_hw_segs = 1; |
| 1169 | host->max_phys_segs = 1; |
| 1170 | host->max_sectors = 1 << (PAGE_CACHE_SHIFT - 9); |
| 1171 | host->max_seg_size = PAGE_CACHE_SIZE; |
| 1172 | } |
| 1173 | |
| 1174 | return host; |
| 1175 | } |
| 1176 | |
| 1177 | EXPORT_SYMBOL(mmc_alloc_host); |
| 1178 | |
| 1179 | /** |
| 1180 | * mmc_add_host - initialise host hardware |
| 1181 | * @host: mmc host |
| 1182 | */ |
| 1183 | int mmc_add_host(struct mmc_host *host) |
| 1184 | { |
Russell King | 00b137c | 2005-08-19 09:41:24 +0100 | [diff] [blame] | 1185 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | |
Russell King | 00b137c | 2005-08-19 09:41:24 +0100 | [diff] [blame] | 1187 | ret = mmc_add_host_sysfs(host); |
| 1188 | if (ret == 0) { |
| 1189 | mmc_power_off(host); |
| 1190 | mmc_detect_change(host); |
| 1191 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | |
Russell King | 00b137c | 2005-08-19 09:41:24 +0100 | [diff] [blame] | 1193 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | EXPORT_SYMBOL(mmc_add_host); |
| 1197 | |
| 1198 | /** |
| 1199 | * mmc_remove_host - remove host hardware |
| 1200 | * @host: mmc host |
| 1201 | * |
| 1202 | * Unregister and remove all cards associated with this host, |
| 1203 | * and power down the MMC bus. |
| 1204 | */ |
| 1205 | void mmc_remove_host(struct mmc_host *host) |
| 1206 | { |
| 1207 | struct list_head *l, *n; |
| 1208 | |
| 1209 | list_for_each_safe(l, n, &host->cards) { |
| 1210 | struct mmc_card *card = mmc_list_to_card(l); |
| 1211 | |
| 1212 | mmc_remove_card(card); |
| 1213 | } |
| 1214 | |
| 1215 | mmc_power_off(host); |
Russell King | 00b137c | 2005-08-19 09:41:24 +0100 | [diff] [blame] | 1216 | mmc_remove_host_sysfs(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | EXPORT_SYMBOL(mmc_remove_host); |
| 1220 | |
| 1221 | /** |
| 1222 | * mmc_free_host - free the host structure |
| 1223 | * @host: mmc host |
| 1224 | * |
| 1225 | * Free the host once all references to it have been dropped. |
| 1226 | */ |
| 1227 | void mmc_free_host(struct mmc_host *host) |
| 1228 | { |
| 1229 | flush_scheduled_work(); |
Russell King | 00b137c | 2005-08-19 09:41:24 +0100 | [diff] [blame] | 1230 | mmc_free_host_sysfs(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | EXPORT_SYMBOL(mmc_free_host); |
| 1234 | |
| 1235 | #ifdef CONFIG_PM |
| 1236 | |
| 1237 | /** |
| 1238 | * mmc_suspend_host - suspend a host |
| 1239 | * @host: mmc host |
| 1240 | * @state: suspend mode (PM_SUSPEND_xxx) |
| 1241 | */ |
Pavel Machek | e5378ca | 2005-04-16 15:25:29 -0700 | [diff] [blame] | 1242 | int mmc_suspend_host(struct mmc_host *host, pm_message_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | { |
| 1244 | mmc_claim_host(host); |
| 1245 | mmc_deselect_cards(host); |
| 1246 | mmc_power_off(host); |
| 1247 | mmc_release_host(host); |
| 1248 | |
| 1249 | return 0; |
| 1250 | } |
| 1251 | |
| 1252 | EXPORT_SYMBOL(mmc_suspend_host); |
| 1253 | |
| 1254 | /** |
| 1255 | * mmc_resume_host - resume a previously suspended host |
| 1256 | * @host: mmc host |
| 1257 | */ |
| 1258 | int mmc_resume_host(struct mmc_host *host) |
| 1259 | { |
| 1260 | mmc_detect_change(host); |
| 1261 | |
| 1262 | return 0; |
| 1263 | } |
| 1264 | |
| 1265 | EXPORT_SYMBOL(mmc_resume_host); |
| 1266 | |
| 1267 | #endif |
| 1268 | |
| 1269 | MODULE_LICENSE("GPL"); |