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