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