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. |
Philip Langdale | bce40a3 | 2006-10-21 12:35:02 +0200 | [diff] [blame] | 7 | * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | /** |
Russell King | fe10c6a | 2006-05-04 13:51:45 +0100 | [diff] [blame] | 62 | * mmc_request_done - finish processing an MMC request |
| 63 | * @host: MMC host which completed request |
| 64 | * @mrq: MMC request which request |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | * |
| 66 | * MMC drivers should call this function when they have completed |
Russell King | fe10c6a | 2006-05-04 13:51:45 +0100 | [diff] [blame] | 67 | * their processing of a request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | */ |
| 69 | void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq) |
| 70 | { |
| 71 | struct mmc_command *cmd = mrq->cmd; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 72 | int err = cmd->error; |
| 73 | |
| 74 | pr_debug("%s: req done (CMD%u): %d/%d/%d: %08x %08x %08x %08x\n", |
| 75 | mmc_hostname(host), cmd->opcode, err, |
| 76 | mrq->data ? mrq->data->error : 0, |
| 77 | mrq->stop ? mrq->stop->error : 0, |
| 78 | cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | if (err && cmd->retries) { |
| 81 | cmd->retries--; |
| 82 | cmd->error = 0; |
| 83 | host->ops->request(host, mrq); |
| 84 | } else if (mrq->done) { |
| 85 | mrq->done(mrq); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | EXPORT_SYMBOL(mmc_request_done); |
| 90 | |
| 91 | /** |
| 92 | * mmc_start_request - start a command on a host |
| 93 | * @host: MMC host to start command on |
| 94 | * @mrq: MMC request to start |
| 95 | * |
| 96 | * Queue a command on the specified host. We expect the |
| 97 | * caller to be holding the host lock with interrupts disabled. |
| 98 | */ |
| 99 | void |
| 100 | mmc_start_request(struct mmc_host *host, struct mmc_request *mrq) |
| 101 | { |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 102 | pr_debug("%s: starting CMD%u arg %08x flags %08x\n", |
| 103 | mmc_hostname(host), mrq->cmd->opcode, |
| 104 | mrq->cmd->arg, mrq->cmd->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
| 106 | WARN_ON(host->card_busy == NULL); |
| 107 | |
| 108 | mrq->cmd->error = 0; |
| 109 | mrq->cmd->mrq = mrq; |
| 110 | if (mrq->data) { |
| 111 | mrq->cmd->data = mrq->data; |
| 112 | mrq->data->error = 0; |
| 113 | mrq->data->mrq = mrq; |
| 114 | if (mrq->stop) { |
| 115 | mrq->data->stop = mrq->stop; |
| 116 | mrq->stop->error = 0; |
| 117 | mrq->stop->mrq = mrq; |
| 118 | } |
| 119 | } |
| 120 | host->ops->request(host, mrq); |
| 121 | } |
| 122 | |
| 123 | EXPORT_SYMBOL(mmc_start_request); |
| 124 | |
| 125 | static void mmc_wait_done(struct mmc_request *mrq) |
| 126 | { |
| 127 | complete(mrq->done_data); |
| 128 | } |
| 129 | |
| 130 | int mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq) |
| 131 | { |
Ingo Molnar | 0afffc7 | 2006-07-03 00:25:35 -0700 | [diff] [blame] | 132 | DECLARE_COMPLETION_ONSTACK(complete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
| 134 | mrq->done_data = &complete; |
| 135 | mrq->done = mmc_wait_done; |
| 136 | |
| 137 | mmc_start_request(host, mrq); |
| 138 | |
| 139 | wait_for_completion(&complete); |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | EXPORT_SYMBOL(mmc_wait_for_req); |
| 145 | |
| 146 | /** |
| 147 | * mmc_wait_for_cmd - start a command and wait for completion |
| 148 | * @host: MMC host to start command |
| 149 | * @cmd: MMC command to start |
| 150 | * @retries: maximum number of retries |
| 151 | * |
| 152 | * Start a new MMC command for a host, and wait for the command |
| 153 | * to complete. Return any error that occurred while the command |
| 154 | * was executing. Do not attempt to parse the response. |
| 155 | */ |
| 156 | int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries) |
| 157 | { |
| 158 | struct mmc_request mrq; |
| 159 | |
| 160 | BUG_ON(host->card_busy == NULL); |
| 161 | |
| 162 | memset(&mrq, 0, sizeof(struct mmc_request)); |
| 163 | |
| 164 | memset(cmd->resp, 0, sizeof(cmd->resp)); |
| 165 | cmd->retries = retries; |
| 166 | |
| 167 | mrq.cmd = cmd; |
| 168 | cmd->data = NULL; |
| 169 | |
| 170 | mmc_wait_for_req(host, &mrq); |
| 171 | |
| 172 | return cmd->error; |
| 173 | } |
| 174 | |
| 175 | EXPORT_SYMBOL(mmc_wait_for_cmd); |
| 176 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 177 | /** |
| 178 | * mmc_wait_for_app_cmd - start an application command and wait for |
| 179 | completion |
| 180 | * @host: MMC host to start command |
| 181 | * @rca: RCA to send MMC_APP_CMD to |
| 182 | * @cmd: MMC command to start |
| 183 | * @retries: maximum number of retries |
| 184 | * |
| 185 | * Sends a MMC_APP_CMD, checks the card response, sends the command |
| 186 | * in the parameter and waits for it to complete. Return any error |
| 187 | * that occurred while the command was executing. Do not attempt to |
| 188 | * parse the response. |
| 189 | */ |
| 190 | int mmc_wait_for_app_cmd(struct mmc_host *host, unsigned int rca, |
| 191 | struct mmc_command *cmd, int retries) |
| 192 | { |
| 193 | struct mmc_request mrq; |
| 194 | struct mmc_command appcmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 196 | int i, err; |
| 197 | |
| 198 | BUG_ON(host->card_busy == NULL); |
| 199 | BUG_ON(retries < 0); |
| 200 | |
| 201 | err = MMC_ERR_INVALID; |
| 202 | |
| 203 | /* |
| 204 | * We have to resend MMC_APP_CMD for each attempt so |
| 205 | * we cannot use the retries field in mmc_command. |
| 206 | */ |
| 207 | for (i = 0;i <= retries;i++) { |
| 208 | memset(&mrq, 0, sizeof(struct mmc_request)); |
| 209 | |
| 210 | appcmd.opcode = MMC_APP_CMD; |
| 211 | appcmd.arg = rca << 16; |
Russell King | e922517 | 2006-02-02 12:23:12 +0000 | [diff] [blame] | 212 | appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 213 | appcmd.retries = 0; |
| 214 | memset(appcmd.resp, 0, sizeof(appcmd.resp)); |
| 215 | appcmd.data = NULL; |
| 216 | |
| 217 | mrq.cmd = &appcmd; |
| 218 | appcmd.data = NULL; |
| 219 | |
| 220 | mmc_wait_for_req(host, &mrq); |
| 221 | |
| 222 | if (appcmd.error) { |
| 223 | err = appcmd.error; |
| 224 | continue; |
| 225 | } |
| 226 | |
| 227 | /* Check that card supported application commands */ |
| 228 | if (!(appcmd.resp[0] & R1_APP_CMD)) |
| 229 | return MMC_ERR_FAILED; |
| 230 | |
| 231 | memset(&mrq, 0, sizeof(struct mmc_request)); |
| 232 | |
| 233 | memset(cmd->resp, 0, sizeof(cmd->resp)); |
| 234 | cmd->retries = 0; |
| 235 | |
| 236 | mrq.cmd = cmd; |
| 237 | cmd->data = NULL; |
| 238 | |
| 239 | mmc_wait_for_req(host, &mrq); |
| 240 | |
| 241 | err = cmd->error; |
| 242 | if (cmd->error == MMC_ERR_NONE) |
| 243 | break; |
| 244 | } |
| 245 | |
| 246 | return err; |
| 247 | } |
| 248 | |
| 249 | EXPORT_SYMBOL(mmc_wait_for_app_cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 251 | /** |
| 252 | * mmc_set_data_timeout - set the timeout for a data command |
| 253 | * @data: data phase for command |
| 254 | * @card: the MMC card associated with the data transfer |
| 255 | * @write: flag to differentiate reads from writes |
| 256 | */ |
| 257 | void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card, |
| 258 | int write) |
| 259 | { |
| 260 | unsigned int mult; |
| 261 | |
| 262 | /* |
| 263 | * SD cards use a 100 multiplier rather than 10 |
| 264 | */ |
| 265 | mult = mmc_card_sd(card) ? 100 : 10; |
| 266 | |
| 267 | /* |
| 268 | * Scale up the multiplier (and therefore the timeout) by |
| 269 | * the r2w factor for writes. |
| 270 | */ |
| 271 | if (write) |
| 272 | mult <<= card->csd.r2w_factor; |
| 273 | |
| 274 | data->timeout_ns = card->csd.tacc_ns * mult; |
| 275 | data->timeout_clks = card->csd.tacc_clks * mult; |
| 276 | |
| 277 | /* |
| 278 | * SD cards also have an upper limit on the timeout. |
| 279 | */ |
| 280 | if (mmc_card_sd(card)) { |
| 281 | unsigned int timeout_us, limit_us; |
| 282 | |
| 283 | timeout_us = data->timeout_ns / 1000; |
| 284 | timeout_us += data->timeout_clks * 1000 / |
| 285 | (card->host->ios.clock / 1000); |
| 286 | |
| 287 | if (write) |
| 288 | limit_us = 250000; |
| 289 | else |
| 290 | limit_us = 100000; |
| 291 | |
| 292 | if (timeout_us > limit_us) { |
| 293 | data->timeout_ns = limit_us * 1000; |
| 294 | data->timeout_clks = 0; |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | EXPORT_SYMBOL(mmc_set_data_timeout); |
| 299 | |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 300 | static int mmc_select_card(struct mmc_host *host, struct mmc_card *card); |
| 301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | /** |
| 303 | * __mmc_claim_host - exclusively claim a host |
| 304 | * @host: mmc host to claim |
| 305 | * @card: mmc card to claim host for |
| 306 | * |
| 307 | * Claim a host for a set of operations. If a valid card |
| 308 | * is passed and this wasn't the last card selected, select |
| 309 | * the card before returning. |
| 310 | * |
| 311 | * Note: you should use mmc_card_claim_host or mmc_claim_host. |
| 312 | */ |
| 313 | int __mmc_claim_host(struct mmc_host *host, struct mmc_card *card) |
| 314 | { |
| 315 | DECLARE_WAITQUEUE(wait, current); |
| 316 | unsigned long flags; |
| 317 | int err = 0; |
| 318 | |
| 319 | add_wait_queue(&host->wq, &wait); |
| 320 | spin_lock_irqsave(&host->lock, flags); |
| 321 | while (1) { |
| 322 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 323 | if (host->card_busy == NULL) |
| 324 | break; |
| 325 | spin_unlock_irqrestore(&host->lock, flags); |
| 326 | schedule(); |
| 327 | spin_lock_irqsave(&host->lock, flags); |
| 328 | } |
| 329 | set_current_state(TASK_RUNNING); |
| 330 | host->card_busy = card; |
| 331 | spin_unlock_irqrestore(&host->lock, flags); |
| 332 | remove_wait_queue(&host->wq, &wait); |
| 333 | |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 334 | if (card != (void *)-1) { |
| 335 | err = mmc_select_card(host, card); |
| 336 | if (err != MMC_ERR_NONE) |
| 337 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | return err; |
| 341 | } |
| 342 | |
| 343 | EXPORT_SYMBOL(__mmc_claim_host); |
| 344 | |
| 345 | /** |
| 346 | * mmc_release_host - release a host |
| 347 | * @host: mmc host to release |
| 348 | * |
| 349 | * Release a MMC host, allowing others to claim the host |
| 350 | * for their operations. |
| 351 | */ |
| 352 | void mmc_release_host(struct mmc_host *host) |
| 353 | { |
| 354 | unsigned long flags; |
| 355 | |
| 356 | BUG_ON(host->card_busy == NULL); |
| 357 | |
| 358 | spin_lock_irqsave(&host->lock, flags); |
| 359 | host->card_busy = NULL; |
| 360 | spin_unlock_irqrestore(&host->lock, flags); |
| 361 | |
| 362 | wake_up(&host->wq); |
| 363 | } |
| 364 | |
| 365 | EXPORT_SYMBOL(mmc_release_host); |
| 366 | |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 367 | static inline void mmc_set_ios(struct mmc_host *host) |
| 368 | { |
| 369 | struct mmc_ios *ios = &host->ios; |
| 370 | |
| 371 | pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u width %u\n", |
| 372 | mmc_hostname(host), ios->clock, ios->bus_mode, |
| 373 | ios->power_mode, ios->chip_select, ios->vdd, |
| 374 | ios->bus_width); |
| 375 | |
| 376 | host->ops->set_ios(host, ios); |
| 377 | } |
| 378 | |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 379 | static int mmc_select_card(struct mmc_host *host, struct mmc_card *card) |
| 380 | { |
| 381 | int err; |
| 382 | struct mmc_command cmd; |
| 383 | |
| 384 | BUG_ON(host->card_busy == NULL); |
| 385 | |
| 386 | if (host->card_selected == card) |
| 387 | return MMC_ERR_NONE; |
| 388 | |
| 389 | host->card_selected = card; |
| 390 | |
| 391 | cmd.opcode = MMC_SELECT_CARD; |
| 392 | cmd.arg = card->rca << 16; |
Russell King | e922517 | 2006-02-02 12:23:12 +0000 | [diff] [blame] | 393 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 394 | |
| 395 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); |
| 396 | if (err != MMC_ERR_NONE) |
| 397 | return err; |
| 398 | |
Pierre Ossman | f218278 | 2005-09-06 15:18:55 -0700 | [diff] [blame] | 399 | /* |
Philip Langdale | e45a1bd | 2006-10-29 10:14:19 +0100 | [diff] [blame] | 400 | * We can only change the bus width of SD cards when |
| 401 | * they are selected so we have to put the handling |
Pierre Ossman | f218278 | 2005-09-06 15:18:55 -0700 | [diff] [blame] | 402 | * here. |
Philip Langdale | e45a1bd | 2006-10-29 10:14:19 +0100 | [diff] [blame] | 403 | * |
| 404 | * The card is in 1 bit mode by default so |
| 405 | * we only need to change if it supports the |
| 406 | * wider version. |
Pierre Ossman | f218278 | 2005-09-06 15:18:55 -0700 | [diff] [blame] | 407 | */ |
Philip Langdale | e45a1bd | 2006-10-29 10:14:19 +0100 | [diff] [blame] | 408 | if (mmc_card_sd(card) && |
| 409 | (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) { |
| 410 | |
Pierre Ossman | f218278 | 2005-09-06 15:18:55 -0700 | [diff] [blame] | 411 | /* |
Philip Langdale | e45a1bd | 2006-10-29 10:14:19 +0100 | [diff] [blame] | 412 | * Default bus width is 1 bit. |
| 413 | */ |
| 414 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
| 415 | |
| 416 | if (host->caps & MMC_CAP_4_BIT_DATA) { |
Pierre Ossman | f218278 | 2005-09-06 15:18:55 -0700 | [diff] [blame] | 417 | struct mmc_command cmd; |
| 418 | cmd.opcode = SD_APP_SET_BUS_WIDTH; |
| 419 | cmd.arg = SD_BUS_WIDTH_4; |
Russell King | e922517 | 2006-02-02 12:23:12 +0000 | [diff] [blame] | 420 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
Pierre Ossman | f218278 | 2005-09-06 15:18:55 -0700 | [diff] [blame] | 421 | |
| 422 | err = mmc_wait_for_app_cmd(host, card->rca, &cmd, |
| 423 | CMD_RETRIES); |
| 424 | if (err != MMC_ERR_NONE) |
| 425 | return err; |
| 426 | |
| 427 | host->ios.bus_width = MMC_BUS_WIDTH_4; |
| 428 | } |
| 429 | } |
| 430 | |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 431 | mmc_set_ios(host); |
Pierre Ossman | f218278 | 2005-09-06 15:18:55 -0700 | [diff] [blame] | 432 | |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 433 | return MMC_ERR_NONE; |
| 434 | } |
| 435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | /* |
| 437 | * Ensure that no card is selected. |
| 438 | */ |
| 439 | static void mmc_deselect_cards(struct mmc_host *host) |
| 440 | { |
| 441 | struct mmc_command cmd; |
| 442 | |
| 443 | if (host->card_selected) { |
| 444 | host->card_selected = NULL; |
| 445 | |
| 446 | cmd.opcode = MMC_SELECT_CARD; |
| 447 | cmd.arg = 0; |
Russell King | e922517 | 2006-02-02 12:23:12 +0000 | [diff] [blame] | 448 | cmd.flags = MMC_RSP_NONE | MMC_CMD_AC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | |
| 450 | mmc_wait_for_cmd(host, &cmd, 0); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | |
| 455 | static inline void mmc_delay(unsigned int ms) |
| 456 | { |
Pierre Ossman | 7377812 | 2006-10-22 22:13:10 +0200 | [diff] [blame] | 457 | if (ms < 1000 / HZ) { |
| 458 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | mdelay(ms); |
| 460 | } else { |
Pierre Ossman | 7377812 | 2006-10-22 22:13:10 +0200 | [diff] [blame] | 461 | msleep(ms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | |
| 465 | /* |
| 466 | * Mask off any voltages we don't support and select |
| 467 | * the lowest voltage |
| 468 | */ |
| 469 | static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr) |
| 470 | { |
| 471 | int bit; |
| 472 | |
| 473 | ocr &= host->ocr_avail; |
| 474 | |
| 475 | bit = ffs(ocr); |
| 476 | if (bit) { |
| 477 | bit -= 1; |
| 478 | |
Timo Teras | 63ef731 | 2006-11-02 19:43:27 +0100 | [diff] [blame] | 479 | ocr &= 3 << bit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
| 481 | host->ios.vdd = bit; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 482 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | } else { |
| 484 | ocr = 0; |
| 485 | } |
| 486 | |
| 487 | return ocr; |
| 488 | } |
| 489 | |
| 490 | #define UNSTUFF_BITS(resp,start,size) \ |
| 491 | ({ \ |
| 492 | const int __size = size; \ |
| 493 | const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \ |
| 494 | const int __off = 3 - ((start) / 32); \ |
| 495 | const int __shft = (start) & 31; \ |
| 496 | u32 __res; \ |
| 497 | \ |
| 498 | __res = resp[__off] >> __shft; \ |
| 499 | if (__size + __shft > 32) \ |
| 500 | __res |= resp[__off-1] << ((32 - __shft) % 32); \ |
| 501 | __res & __mask; \ |
| 502 | }) |
| 503 | |
| 504 | /* |
| 505 | * Given the decoded CSD structure, decode the raw CID to our CID structure. |
| 506 | */ |
| 507 | static void mmc_decode_cid(struct mmc_card *card) |
| 508 | { |
| 509 | u32 *resp = card->raw_cid; |
| 510 | |
| 511 | memset(&card->cid, 0, sizeof(struct mmc_cid)); |
| 512 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 513 | if (mmc_card_sd(card)) { |
| 514 | /* |
| 515 | * SD doesn't currently have a version field so we will |
| 516 | * have to assume we can parse this. |
| 517 | */ |
| 518 | card->cid.manfid = UNSTUFF_BITS(resp, 120, 8); |
| 519 | card->cid.oemid = UNSTUFF_BITS(resp, 104, 16); |
| 520 | card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); |
| 521 | card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); |
| 522 | card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); |
| 523 | card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); |
| 524 | card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); |
| 525 | card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4); |
| 526 | card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4); |
| 527 | card->cid.serial = UNSTUFF_BITS(resp, 24, 32); |
| 528 | card->cid.year = UNSTUFF_BITS(resp, 12, 8); |
| 529 | card->cid.month = UNSTUFF_BITS(resp, 8, 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 531 | card->cid.year += 2000; /* SD cards year offset */ |
Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 532 | } else { |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 533 | /* |
| 534 | * The selection of the format here is based upon published |
| 535 | * specs from sandisk and from what people have reported. |
| 536 | */ |
| 537 | switch (card->csd.mmca_vsn) { |
| 538 | case 0: /* MMC v1.0 - v1.2 */ |
| 539 | case 1: /* MMC v1.4 */ |
| 540 | card->cid.manfid = UNSTUFF_BITS(resp, 104, 24); |
| 541 | card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); |
| 542 | card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); |
| 543 | card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); |
| 544 | card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); |
| 545 | card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); |
| 546 | card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8); |
| 547 | card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8); |
| 548 | card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4); |
| 549 | card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4); |
| 550 | card->cid.serial = UNSTUFF_BITS(resp, 16, 24); |
| 551 | card->cid.month = UNSTUFF_BITS(resp, 12, 4); |
| 552 | card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997; |
| 553 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 555 | case 2: /* MMC v2.0 - v2.2 */ |
| 556 | case 3: /* MMC v3.1 - v3.3 */ |
Pierre Ossman | cb757b4 | 2006-01-08 14:23:02 +0000 | [diff] [blame] | 557 | case 4: /* MMC v4 */ |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 558 | card->cid.manfid = UNSTUFF_BITS(resp, 120, 8); |
| 559 | card->cid.oemid = UNSTUFF_BITS(resp, 104, 16); |
| 560 | card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); |
| 561 | card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); |
| 562 | card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); |
| 563 | card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); |
| 564 | card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); |
| 565 | card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8); |
| 566 | card->cid.serial = UNSTUFF_BITS(resp, 16, 32); |
| 567 | card->cid.month = UNSTUFF_BITS(resp, 12, 4); |
| 568 | card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997; |
| 569 | break; |
| 570 | |
| 571 | default: |
| 572 | printk("%s: card has unknown MMCA version %d\n", |
| 573 | mmc_hostname(card->host), card->csd.mmca_vsn); |
| 574 | mmc_card_set_bad(card); |
| 575 | break; |
| 576 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | |
| 580 | /* |
| 581 | * Given a 128-bit response, decode to our card CSD structure. |
| 582 | */ |
| 583 | static void mmc_decode_csd(struct mmc_card *card) |
| 584 | { |
| 585 | struct mmc_csd *csd = &card->csd; |
| 586 | unsigned int e, m, csd_struct; |
| 587 | u32 *resp = card->raw_csd; |
| 588 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 589 | if (mmc_card_sd(card)) { |
| 590 | csd_struct = UNSTUFF_BITS(resp, 126, 2); |
| 591 | if (csd_struct != 0) { |
| 592 | printk("%s: unrecognised CSD structure version %d\n", |
| 593 | mmc_hostname(card->host), csd_struct); |
| 594 | mmc_card_set_bad(card); |
| 595 | return; |
| 596 | } |
| 597 | |
| 598 | m = UNSTUFF_BITS(resp, 115, 4); |
| 599 | e = UNSTUFF_BITS(resp, 112, 3); |
| 600 | csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10; |
| 601 | csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100; |
| 602 | |
| 603 | m = UNSTUFF_BITS(resp, 99, 4); |
| 604 | e = UNSTUFF_BITS(resp, 96, 3); |
| 605 | csd->max_dtr = tran_exp[e] * tran_mant[m]; |
| 606 | csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); |
| 607 | |
| 608 | e = UNSTUFF_BITS(resp, 47, 3); |
| 609 | m = UNSTUFF_BITS(resp, 62, 12); |
| 610 | csd->capacity = (1 + m) << (e + 2); |
| 611 | |
| 612 | csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 613 | csd->read_partial = UNSTUFF_BITS(resp, 79, 1); |
| 614 | csd->write_misalign = UNSTUFF_BITS(resp, 78, 1); |
| 615 | csd->read_misalign = UNSTUFF_BITS(resp, 77, 1); |
Russell King | 37be4e7 | 2006-05-02 17:24:59 +0100 | [diff] [blame] | 616 | csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 617 | csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); |
| 618 | csd->write_partial = UNSTUFF_BITS(resp, 21, 1); |
Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 619 | } else { |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 620 | /* |
| 621 | * We only understand CSD structure v1.1 and v1.2. |
| 622 | * v1.2 has extra information in bits 15, 11 and 10. |
| 623 | */ |
| 624 | csd_struct = UNSTUFF_BITS(resp, 126, 2); |
| 625 | if (csd_struct != 1 && csd_struct != 2) { |
| 626 | printk("%s: unrecognised CSD structure version %d\n", |
| 627 | mmc_hostname(card->host), csd_struct); |
| 628 | mmc_card_set_bad(card); |
| 629 | return; |
| 630 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 632 | csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4); |
| 633 | m = UNSTUFF_BITS(resp, 115, 4); |
| 634 | e = UNSTUFF_BITS(resp, 112, 3); |
| 635 | csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10; |
| 636 | csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 638 | m = UNSTUFF_BITS(resp, 99, 4); |
| 639 | e = UNSTUFF_BITS(resp, 96, 3); |
| 640 | csd->max_dtr = tran_exp[e] * tran_mant[m]; |
| 641 | csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 643 | e = UNSTUFF_BITS(resp, 47, 3); |
| 644 | m = UNSTUFF_BITS(resp, 62, 12); |
| 645 | csd->capacity = (1 + m) << (e + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 647 | csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 648 | csd->read_partial = UNSTUFF_BITS(resp, 79, 1); |
| 649 | csd->write_misalign = UNSTUFF_BITS(resp, 78, 1); |
| 650 | csd->read_misalign = UNSTUFF_BITS(resp, 77, 1); |
Russell King | 37be4e7 | 2006-05-02 17:24:59 +0100 | [diff] [blame] | 651 | csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 652 | csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); |
| 653 | csd->write_partial = UNSTUFF_BITS(resp, 21, 1); |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 654 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | /* |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 658 | * Given a 64-bit response, decode to our card SCR structure. |
| 659 | */ |
| 660 | static void mmc_decode_scr(struct mmc_card *card) |
| 661 | { |
| 662 | struct sd_scr *scr = &card->scr; |
| 663 | unsigned int scr_struct; |
| 664 | u32 resp[4]; |
| 665 | |
| 666 | BUG_ON(!mmc_card_sd(card)); |
| 667 | |
| 668 | resp[3] = card->raw_scr[1]; |
| 669 | resp[2] = card->raw_scr[0]; |
| 670 | |
| 671 | scr_struct = UNSTUFF_BITS(resp, 60, 4); |
| 672 | if (scr_struct != 0) { |
| 673 | printk("%s: unrecognised SCR structure version %d\n", |
| 674 | mmc_hostname(card->host), scr_struct); |
| 675 | mmc_card_set_bad(card); |
| 676 | return; |
| 677 | } |
| 678 | |
| 679 | scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4); |
| 680 | scr->bus_widths = UNSTUFF_BITS(resp, 48, 4); |
| 681 | } |
| 682 | |
| 683 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | * Locate a MMC card on this MMC host given a raw CID. |
| 685 | */ |
| 686 | static struct mmc_card *mmc_find_card(struct mmc_host *host, u32 *raw_cid) |
| 687 | { |
| 688 | struct mmc_card *card; |
| 689 | |
| 690 | list_for_each_entry(card, &host->cards, node) { |
| 691 | if (memcmp(card->raw_cid, raw_cid, sizeof(card->raw_cid)) == 0) |
| 692 | return card; |
| 693 | } |
| 694 | return NULL; |
| 695 | } |
| 696 | |
| 697 | /* |
| 698 | * Allocate a new MMC card, and assign a unique RCA. |
| 699 | */ |
| 700 | static struct mmc_card * |
| 701 | mmc_alloc_card(struct mmc_host *host, u32 *raw_cid, unsigned int *frca) |
| 702 | { |
| 703 | struct mmc_card *card, *c; |
| 704 | unsigned int rca = *frca; |
| 705 | |
| 706 | card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL); |
| 707 | if (!card) |
| 708 | return ERR_PTR(-ENOMEM); |
| 709 | |
| 710 | mmc_init_card(card, host); |
| 711 | memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid)); |
| 712 | |
| 713 | again: |
| 714 | list_for_each_entry(c, &host->cards, node) |
| 715 | if (c->rca == rca) { |
| 716 | rca++; |
| 717 | goto again; |
| 718 | } |
| 719 | |
| 720 | card->rca = rca; |
| 721 | |
| 722 | *frca = rca; |
| 723 | |
| 724 | return card; |
| 725 | } |
| 726 | |
| 727 | /* |
| 728 | * Tell attached cards to go to IDLE state |
| 729 | */ |
| 730 | static void mmc_idle_cards(struct mmc_host *host) |
| 731 | { |
| 732 | struct mmc_command cmd; |
| 733 | |
Pierre Ossman | 865e9f1 | 2005-09-03 16:45:02 +0100 | [diff] [blame] | 734 | host->ios.chip_select = MMC_CS_HIGH; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 735 | mmc_set_ios(host); |
Pierre Ossman | 865e9f1 | 2005-09-03 16:45:02 +0100 | [diff] [blame] | 736 | |
| 737 | mmc_delay(1); |
| 738 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | cmd.opcode = MMC_GO_IDLE_STATE; |
| 740 | cmd.arg = 0; |
Russell King | e922517 | 2006-02-02 12:23:12 +0000 | [diff] [blame] | 741 | cmd.flags = MMC_RSP_NONE | MMC_CMD_BC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | |
| 743 | mmc_wait_for_cmd(host, &cmd, 0); |
| 744 | |
| 745 | mmc_delay(1); |
Pierre Ossman | 865e9f1 | 2005-09-03 16:45:02 +0100 | [diff] [blame] | 746 | |
| 747 | host->ios.chip_select = MMC_CS_DONTCARE; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 748 | mmc_set_ios(host); |
Pierre Ossman | 865e9f1 | 2005-09-03 16:45:02 +0100 | [diff] [blame] | 749 | |
| 750 | mmc_delay(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | /* |
Russell King | 45f8245 | 2005-12-14 14:57:35 +0000 | [diff] [blame] | 754 | * Apply power to the MMC stack. This is a two-stage process. |
| 755 | * First, we enable power to the card without the clock running. |
| 756 | * We then wait a bit for the power to stabilise. Finally, |
| 757 | * enable the bus drivers and clock to the card. |
| 758 | * |
| 759 | * We must _NOT_ enable the clock prior to power stablising. |
| 760 | * |
| 761 | * If a host does all the power sequencing itself, ignore the |
| 762 | * initial MMC_POWER_UP stage. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | */ |
| 764 | static void mmc_power_up(struct mmc_host *host) |
| 765 | { |
| 766 | int bit = fls(host->ocr_avail) - 1; |
| 767 | |
| 768 | host->ios.vdd = bit; |
| 769 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; |
Pierre Ossman | 865e9f1 | 2005-09-03 16:45:02 +0100 | [diff] [blame] | 770 | host->ios.chip_select = MMC_CS_DONTCARE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | host->ios.power_mode = MMC_POWER_UP; |
Pierre Ossman | f218278 | 2005-09-06 15:18:55 -0700 | [diff] [blame] | 772 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 773 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | |
| 775 | mmc_delay(1); |
| 776 | |
| 777 | host->ios.clock = host->f_min; |
| 778 | host->ios.power_mode = MMC_POWER_ON; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 779 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | |
| 781 | mmc_delay(2); |
| 782 | } |
| 783 | |
| 784 | static void mmc_power_off(struct mmc_host *host) |
| 785 | { |
| 786 | host->ios.clock = 0; |
| 787 | host->ios.vdd = 0; |
| 788 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; |
Pierre Ossman | 865e9f1 | 2005-09-03 16:45:02 +0100 | [diff] [blame] | 789 | host->ios.chip_select = MMC_CS_DONTCARE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | host->ios.power_mode = MMC_POWER_OFF; |
Pierre Ossman | f218278 | 2005-09-06 15:18:55 -0700 | [diff] [blame] | 791 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 792 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | static int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr) |
| 796 | { |
| 797 | struct mmc_command cmd; |
| 798 | int i, err = 0; |
| 799 | |
| 800 | cmd.opcode = MMC_SEND_OP_COND; |
| 801 | cmd.arg = ocr; |
Russell King | e922517 | 2006-02-02 12:23:12 +0000 | [diff] [blame] | 802 | cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | |
| 804 | for (i = 100; i; i--) { |
| 805 | err = mmc_wait_for_cmd(host, &cmd, 0); |
| 806 | if (err != MMC_ERR_NONE) |
| 807 | break; |
| 808 | |
| 809 | if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0) |
| 810 | break; |
| 811 | |
| 812 | err = MMC_ERR_TIMEOUT; |
| 813 | |
| 814 | mmc_delay(10); |
| 815 | } |
| 816 | |
| 817 | if (rocr) |
| 818 | *rocr = cmd.resp[0]; |
| 819 | |
| 820 | return err; |
| 821 | } |
| 822 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 823 | static int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr) |
| 824 | { |
| 825 | struct mmc_command cmd; |
| 826 | int i, err = 0; |
| 827 | |
| 828 | cmd.opcode = SD_APP_OP_COND; |
| 829 | cmd.arg = ocr; |
Russell King | e922517 | 2006-02-02 12:23:12 +0000 | [diff] [blame] | 830 | cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 831 | |
| 832 | for (i = 100; i; i--) { |
| 833 | err = mmc_wait_for_app_cmd(host, 0, &cmd, CMD_RETRIES); |
| 834 | if (err != MMC_ERR_NONE) |
| 835 | break; |
| 836 | |
| 837 | if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0) |
| 838 | break; |
| 839 | |
| 840 | err = MMC_ERR_TIMEOUT; |
| 841 | |
| 842 | mmc_delay(10); |
| 843 | } |
| 844 | |
| 845 | if (rocr) |
| 846 | *rocr = cmd.resp[0]; |
| 847 | |
| 848 | return err; |
| 849 | } |
| 850 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | /* |
| 852 | * Discover cards by requesting their CID. If this command |
| 853 | * times out, it is not an error; there are no further cards |
| 854 | * to be discovered. Add new cards to the list. |
| 855 | * |
| 856 | * Create a mmc_card entry for each discovered card, assigning |
| 857 | * it an RCA, and save the raw CID for decoding later. |
| 858 | */ |
| 859 | static void mmc_discover_cards(struct mmc_host *host) |
| 860 | { |
| 861 | struct mmc_card *card; |
| 862 | unsigned int first_rca = 1, err; |
| 863 | |
| 864 | while (1) { |
| 865 | struct mmc_command cmd; |
| 866 | |
| 867 | cmd.opcode = MMC_ALL_SEND_CID; |
| 868 | cmd.arg = 0; |
Russell King | e922517 | 2006-02-02 12:23:12 +0000 | [diff] [blame] | 869 | cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | |
| 871 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); |
| 872 | if (err == MMC_ERR_TIMEOUT) { |
| 873 | err = MMC_ERR_NONE; |
| 874 | break; |
| 875 | } |
| 876 | if (err != MMC_ERR_NONE) { |
| 877 | printk(KERN_ERR "%s: error requesting CID: %d\n", |
Russell King | d366b64 | 2005-08-19 09:40:08 +0100 | [diff] [blame] | 878 | mmc_hostname(host), err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | break; |
| 880 | } |
| 881 | |
| 882 | card = mmc_find_card(host, cmd.resp); |
| 883 | if (!card) { |
| 884 | card = mmc_alloc_card(host, cmd.resp, &first_rca); |
| 885 | if (IS_ERR(card)) { |
| 886 | err = PTR_ERR(card); |
| 887 | break; |
| 888 | } |
| 889 | list_add(&card->node, &host->cards); |
| 890 | } |
| 891 | |
| 892 | card->state &= ~MMC_STATE_DEAD; |
| 893 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 894 | if (host->mode == MMC_MODE_SD) { |
| 895 | mmc_card_set_sd(card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 897 | cmd.opcode = SD_SEND_RELATIVE_ADDR; |
| 898 | cmd.arg = 0; |
Russell King | e922517 | 2006-02-02 12:23:12 +0000 | [diff] [blame] | 899 | cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR; |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 900 | |
| 901 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); |
| 902 | if (err != MMC_ERR_NONE) |
| 903 | mmc_card_set_dead(card); |
Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 904 | else { |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 905 | card->rca = cmd.resp[0] >> 16; |
Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 906 | |
| 907 | if (!host->ops->get_ro) { |
| 908 | printk(KERN_WARNING "%s: host does not " |
| 909 | "support reading read-only " |
| 910 | "switch. assuming write-enable.\n", |
| 911 | mmc_hostname(host)); |
| 912 | } else { |
| 913 | if (host->ops->get_ro(host)) |
| 914 | mmc_card_set_readonly(card); |
| 915 | } |
| 916 | } |
| 917 | } else { |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 918 | cmd.opcode = MMC_SET_RELATIVE_ADDR; |
| 919 | cmd.arg = card->rca << 16; |
Russell King | e922517 | 2006-02-02 12:23:12 +0000 | [diff] [blame] | 920 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 921 | |
| 922 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); |
| 923 | if (err != MMC_ERR_NONE) |
| 924 | mmc_card_set_dead(card); |
| 925 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | } |
| 927 | } |
| 928 | |
| 929 | static void mmc_read_csds(struct mmc_host *host) |
| 930 | { |
| 931 | struct mmc_card *card; |
| 932 | |
| 933 | list_for_each_entry(card, &host->cards, node) { |
| 934 | struct mmc_command cmd; |
| 935 | int err; |
| 936 | |
| 937 | if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT)) |
| 938 | continue; |
| 939 | |
| 940 | cmd.opcode = MMC_SEND_CSD; |
| 941 | cmd.arg = card->rca << 16; |
Russell King | e922517 | 2006-02-02 12:23:12 +0000 | [diff] [blame] | 942 | cmd.flags = MMC_RSP_R2 | MMC_CMD_AC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | |
| 944 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); |
| 945 | if (err != MMC_ERR_NONE) { |
| 946 | mmc_card_set_dead(card); |
| 947 | continue; |
| 948 | } |
| 949 | |
| 950 | memcpy(card->raw_csd, cmd.resp, sizeof(card->raw_csd)); |
| 951 | |
| 952 | mmc_decode_csd(card); |
| 953 | mmc_decode_cid(card); |
| 954 | } |
| 955 | } |
| 956 | |
Philip Langdale | bce40a3 | 2006-10-21 12:35:02 +0200 | [diff] [blame] | 957 | static void mmc_process_ext_csds(struct mmc_host *host) |
| 958 | { |
| 959 | int err; |
| 960 | struct mmc_card *card; |
| 961 | |
| 962 | struct mmc_request mrq; |
| 963 | struct mmc_command cmd; |
| 964 | struct mmc_data data; |
| 965 | |
| 966 | struct scatterlist sg; |
| 967 | |
| 968 | /* |
| 969 | * As the ext_csd is so large and mostly unused, we don't store the |
| 970 | * raw block in mmc_card. |
| 971 | */ |
| 972 | u8 *ext_csd; |
| 973 | ext_csd = kmalloc(512, GFP_KERNEL); |
| 974 | if (!ext_csd) { |
| 975 | printk("%s: could not allocate a buffer to receive the ext_csd." |
| 976 | "mmc v4 cards will be treated as v3.\n", |
| 977 | mmc_hostname(host)); |
| 978 | return; |
| 979 | } |
| 980 | |
| 981 | list_for_each_entry(card, &host->cards, node) { |
| 982 | if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT)) |
| 983 | continue; |
| 984 | if (mmc_card_sd(card)) |
| 985 | continue; |
| 986 | if (card->csd.mmca_vsn < CSD_SPEC_VER_4) |
| 987 | continue; |
| 988 | |
| 989 | err = mmc_select_card(host, card); |
| 990 | if (err != MMC_ERR_NONE) { |
| 991 | mmc_card_set_dead(card); |
| 992 | continue; |
| 993 | } |
| 994 | |
| 995 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 996 | |
| 997 | cmd.opcode = MMC_SEND_EXT_CSD; |
| 998 | cmd.arg = 0; |
| 999 | cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; |
| 1000 | |
| 1001 | memset(&data, 0, sizeof(struct mmc_data)); |
| 1002 | |
| 1003 | mmc_set_data_timeout(&data, card, 0); |
| 1004 | |
| 1005 | data.blksz = 512; |
| 1006 | data.blocks = 1; |
| 1007 | data.flags = MMC_DATA_READ; |
| 1008 | data.sg = &sg; |
| 1009 | data.sg_len = 1; |
| 1010 | |
| 1011 | memset(&mrq, 0, sizeof(struct mmc_request)); |
| 1012 | |
| 1013 | mrq.cmd = &cmd; |
| 1014 | mrq.data = &data; |
| 1015 | |
| 1016 | sg_init_one(&sg, ext_csd, 512); |
| 1017 | |
| 1018 | mmc_wait_for_req(host, &mrq); |
| 1019 | |
| 1020 | if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) { |
| 1021 | mmc_card_set_dead(card); |
| 1022 | continue; |
| 1023 | } |
| 1024 | |
| 1025 | switch (ext_csd[EXT_CSD_CARD_TYPE]) { |
| 1026 | case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26: |
| 1027 | card->ext_csd.hs_max_dtr = 52000000; |
| 1028 | break; |
| 1029 | case EXT_CSD_CARD_TYPE_26: |
| 1030 | card->ext_csd.hs_max_dtr = 26000000; |
| 1031 | break; |
| 1032 | default: |
| 1033 | /* MMC v4 spec says this cannot happen */ |
| 1034 | printk("%s: card is mmc v4 but doesn't support " |
| 1035 | "any high-speed modes.\n", |
| 1036 | mmc_hostname(card->host)); |
| 1037 | mmc_card_set_bad(card); |
| 1038 | continue; |
| 1039 | } |
| 1040 | |
| 1041 | /* Activate highspeed support. */ |
| 1042 | cmd.opcode = MMC_SWITCH; |
| 1043 | cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | |
| 1044 | (EXT_CSD_HS_TIMING << 16) | |
| 1045 | (1 << 8) | |
| 1046 | EXT_CSD_CMD_SET_NORMAL; |
| 1047 | cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; |
| 1048 | |
| 1049 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); |
| 1050 | if (err != MMC_ERR_NONE) { |
| 1051 | printk("%s: failed to switch card to mmc v4 " |
| 1052 | "high-speed mode.\n", |
| 1053 | mmc_hostname(card->host)); |
| 1054 | continue; |
| 1055 | } |
| 1056 | |
| 1057 | mmc_card_set_highspeed(card); |
Philip Langdale | e45a1bd | 2006-10-29 10:14:19 +0100 | [diff] [blame] | 1058 | |
| 1059 | /* Check for host support for wide-bus modes. */ |
| 1060 | if (!(host->caps & MMC_CAP_4_BIT_DATA)) { |
| 1061 | continue; |
| 1062 | } |
| 1063 | |
| 1064 | /* Activate 4-bit support. */ |
| 1065 | cmd.opcode = MMC_SWITCH; |
| 1066 | cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | |
| 1067 | (EXT_CSD_BUS_WIDTH << 16) | |
| 1068 | (EXT_CSD_BUS_WIDTH_4 << 8) | |
| 1069 | EXT_CSD_CMD_SET_NORMAL; |
| 1070 | cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; |
| 1071 | |
| 1072 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); |
| 1073 | if (err != MMC_ERR_NONE) { |
| 1074 | printk("%s: failed to switch card to " |
| 1075 | "mmc v4 4-bit bus mode.\n", |
| 1076 | mmc_hostname(card->host)); |
| 1077 | continue; |
| 1078 | } |
| 1079 | |
| 1080 | host->ios.bus_width = MMC_BUS_WIDTH_4; |
Philip Langdale | bce40a3 | 2006-10-21 12:35:02 +0200 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | kfree(ext_csd); |
| 1084 | |
| 1085 | mmc_deselect_cards(host); |
| 1086 | } |
| 1087 | |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 1088 | static void mmc_read_scrs(struct mmc_host *host) |
| 1089 | { |
| 1090 | int err; |
| 1091 | struct mmc_card *card; |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 1092 | struct mmc_request mrq; |
| 1093 | struct mmc_command cmd; |
| 1094 | struct mmc_data data; |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 1095 | struct scatterlist sg; |
| 1096 | |
| 1097 | list_for_each_entry(card, &host->cards, node) { |
| 1098 | if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT)) |
| 1099 | continue; |
| 1100 | if (!mmc_card_sd(card)) |
| 1101 | continue; |
| 1102 | |
| 1103 | err = mmc_select_card(host, card); |
| 1104 | if (err != MMC_ERR_NONE) { |
| 1105 | mmc_card_set_dead(card); |
| 1106 | continue; |
| 1107 | } |
| 1108 | |
| 1109 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 1110 | |
| 1111 | cmd.opcode = MMC_APP_CMD; |
| 1112 | cmd.arg = card->rca << 16; |
Russell King | e922517 | 2006-02-02 12:23:12 +0000 | [diff] [blame] | 1113 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 1114 | |
| 1115 | err = mmc_wait_for_cmd(host, &cmd, 0); |
| 1116 | if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD)) { |
| 1117 | mmc_card_set_dead(card); |
| 1118 | continue; |
| 1119 | } |
| 1120 | |
| 1121 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 1122 | |
| 1123 | cmd.opcode = SD_APP_SEND_SCR; |
| 1124 | cmd.arg = 0; |
Russell King | e922517 | 2006-02-02 12:23:12 +0000 | [diff] [blame] | 1125 | cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 1126 | |
| 1127 | memset(&data, 0, sizeof(struct mmc_data)); |
| 1128 | |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 1129 | mmc_set_data_timeout(&data, card, 0); |
Pierre Ossman | 385e3227 | 2006-06-18 14:34:37 +0200 | [diff] [blame] | 1130 | |
Pavel Pisa | 2c171bf | 2006-05-19 21:48:03 +0100 | [diff] [blame] | 1131 | data.blksz = 1 << 3; |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 1132 | data.blocks = 1; |
| 1133 | data.flags = MMC_DATA_READ; |
| 1134 | data.sg = &sg; |
| 1135 | data.sg_len = 1; |
| 1136 | |
| 1137 | memset(&mrq, 0, sizeof(struct mmc_request)); |
| 1138 | |
| 1139 | mrq.cmd = &cmd; |
| 1140 | mrq.data = &data; |
| 1141 | |
| 1142 | sg_init_one(&sg, (u8*)card->raw_scr, 8); |
| 1143 | |
Pierre Ossman | e781de4 | 2005-12-05 10:00:50 +0000 | [diff] [blame] | 1144 | mmc_wait_for_req(host, &mrq); |
| 1145 | |
| 1146 | if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) { |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 1147 | mmc_card_set_dead(card); |
| 1148 | continue; |
| 1149 | } |
| 1150 | |
| 1151 | card->raw_scr[0] = ntohl(card->raw_scr[0]); |
| 1152 | card->raw_scr[1] = ntohl(card->raw_scr[1]); |
| 1153 | |
| 1154 | mmc_decode_scr(card); |
| 1155 | } |
| 1156 | |
| 1157 | mmc_deselect_cards(host); |
| 1158 | } |
| 1159 | |
Pierre Ossman | 7ccd266 | 2006-11-08 23:03:10 +0100 | [diff] [blame] | 1160 | static void mmc_read_switch_caps(struct mmc_host *host) |
| 1161 | { |
| 1162 | int err; |
| 1163 | struct mmc_card *card; |
| 1164 | struct mmc_request mrq; |
| 1165 | struct mmc_command cmd; |
| 1166 | struct mmc_data data; |
| 1167 | unsigned char *status; |
| 1168 | struct scatterlist sg; |
| 1169 | |
| 1170 | status = kmalloc(64, GFP_KERNEL); |
| 1171 | if (!status) { |
| 1172 | printk(KERN_WARNING "%s: Unable to allocate buffer for " |
| 1173 | "reading switch capabilities.\n", |
| 1174 | mmc_hostname(host)); |
| 1175 | return; |
| 1176 | } |
| 1177 | |
| 1178 | list_for_each_entry(card, &host->cards, node) { |
| 1179 | if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT)) |
| 1180 | continue; |
| 1181 | if (!mmc_card_sd(card)) |
| 1182 | continue; |
| 1183 | if (card->scr.sda_vsn < SCR_SPEC_VER_1) |
| 1184 | continue; |
| 1185 | |
| 1186 | err = mmc_select_card(host, card); |
| 1187 | if (err != MMC_ERR_NONE) { |
| 1188 | mmc_card_set_dead(card); |
| 1189 | continue; |
| 1190 | } |
| 1191 | |
| 1192 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 1193 | |
| 1194 | cmd.opcode = SD_SWITCH; |
| 1195 | cmd.arg = 0x00FFFFF1; |
| 1196 | cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; |
| 1197 | |
| 1198 | memset(&data, 0, sizeof(struct mmc_data)); |
| 1199 | |
| 1200 | mmc_set_data_timeout(&data, card, 0); |
| 1201 | |
| 1202 | data.blksz = 64; |
| 1203 | data.blocks = 1; |
| 1204 | data.flags = MMC_DATA_READ; |
| 1205 | data.sg = &sg; |
| 1206 | data.sg_len = 1; |
| 1207 | |
| 1208 | memset(&mrq, 0, sizeof(struct mmc_request)); |
| 1209 | |
| 1210 | mrq.cmd = &cmd; |
| 1211 | mrq.data = &data; |
| 1212 | |
| 1213 | sg_init_one(&sg, status, 64); |
| 1214 | |
| 1215 | mmc_wait_for_req(host, &mrq); |
| 1216 | |
| 1217 | if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) { |
| 1218 | mmc_card_set_dead(card); |
| 1219 | continue; |
| 1220 | } |
| 1221 | |
| 1222 | if (status[13] & 0x02) |
| 1223 | card->sw_caps.hs_max_dtr = 50000000; |
| 1224 | |
| 1225 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 1226 | |
| 1227 | cmd.opcode = SD_SWITCH; |
| 1228 | cmd.arg = 0x80FFFFF1; |
| 1229 | cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; |
| 1230 | |
| 1231 | memset(&data, 0, sizeof(struct mmc_data)); |
| 1232 | |
| 1233 | mmc_set_data_timeout(&data, card, 0); |
| 1234 | |
| 1235 | data.blksz = 64; |
| 1236 | data.blocks = 1; |
| 1237 | data.flags = MMC_DATA_READ; |
| 1238 | data.sg = &sg; |
| 1239 | data.sg_len = 1; |
| 1240 | |
| 1241 | memset(&mrq, 0, sizeof(struct mmc_request)); |
| 1242 | |
| 1243 | mrq.cmd = &cmd; |
| 1244 | mrq.data = &data; |
| 1245 | |
| 1246 | sg_init_one(&sg, status, 64); |
| 1247 | |
| 1248 | mmc_wait_for_req(host, &mrq); |
| 1249 | |
| 1250 | if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) { |
| 1251 | mmc_card_set_dead(card); |
| 1252 | continue; |
| 1253 | } |
| 1254 | |
| 1255 | if ((status[16] & 0xF) != 1) { |
| 1256 | printk(KERN_WARNING "%s: Problem switching card " |
| 1257 | "into high-speed mode!\n", |
| 1258 | mmc_hostname(host)); |
| 1259 | continue; |
| 1260 | } |
| 1261 | |
| 1262 | mmc_card_set_highspeed(card); |
| 1263 | } |
| 1264 | |
| 1265 | kfree(status); |
| 1266 | |
| 1267 | mmc_deselect_cards(host); |
| 1268 | } |
| 1269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | static unsigned int mmc_calculate_clock(struct mmc_host *host) |
| 1271 | { |
| 1272 | struct mmc_card *card; |
| 1273 | unsigned int max_dtr = host->f_max; |
| 1274 | |
| 1275 | list_for_each_entry(card, &host->cards, node) |
Philip Langdale | bce40a3 | 2006-10-21 12:35:02 +0200 | [diff] [blame] | 1276 | if (!mmc_card_dead(card)) { |
Pierre Ossman | 7ccd266 | 2006-11-08 23:03:10 +0100 | [diff] [blame] | 1277 | if (mmc_card_highspeed(card) && mmc_card_sd(card)) { |
| 1278 | if (max_dtr > card->sw_caps.hs_max_dtr) |
| 1279 | max_dtr = card->sw_caps.hs_max_dtr; |
| 1280 | } else if (mmc_card_highspeed(card) && !mmc_card_sd(card)) { |
Philip Langdale | bce40a3 | 2006-10-21 12:35:02 +0200 | [diff] [blame] | 1281 | if (max_dtr > card->ext_csd.hs_max_dtr) |
Pierre Ossman | 7ccd266 | 2006-11-08 23:03:10 +0100 | [diff] [blame] | 1282 | max_dtr = card->ext_csd.hs_max_dtr; |
Philip Langdale | bce40a3 | 2006-10-21 12:35:02 +0200 | [diff] [blame] | 1283 | } else if (max_dtr > card->csd.max_dtr) { |
| 1284 | max_dtr = card->csd.max_dtr; |
| 1285 | } |
| 1286 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 1288 | pr_debug("%s: selected %d.%03dMHz transfer rate\n", |
| 1289 | mmc_hostname(host), |
Russell King | c656317 | 2006-03-29 09:30:20 +0100 | [diff] [blame] | 1290 | max_dtr / 1000000, (max_dtr / 1000) % 1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | |
| 1292 | return max_dtr; |
| 1293 | } |
| 1294 | |
| 1295 | /* |
| 1296 | * Check whether cards we already know about are still present. |
| 1297 | * We do this by requesting status, and checking whether a card |
| 1298 | * responds. |
| 1299 | * |
| 1300 | * A request for status does not cause a state change in data |
| 1301 | * transfer mode. |
| 1302 | */ |
| 1303 | static void mmc_check_cards(struct mmc_host *host) |
| 1304 | { |
| 1305 | struct list_head *l, *n; |
| 1306 | |
| 1307 | mmc_deselect_cards(host); |
| 1308 | |
| 1309 | list_for_each_safe(l, n, &host->cards) { |
| 1310 | struct mmc_card *card = mmc_list_to_card(l); |
| 1311 | struct mmc_command cmd; |
| 1312 | int err; |
| 1313 | |
| 1314 | cmd.opcode = MMC_SEND_STATUS; |
| 1315 | cmd.arg = card->rca << 16; |
Russell King | e922517 | 2006-02-02 12:23:12 +0000 | [diff] [blame] | 1316 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | |
| 1318 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); |
| 1319 | if (err == MMC_ERR_NONE) |
| 1320 | continue; |
| 1321 | |
| 1322 | mmc_card_set_dead(card); |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | static void mmc_setup(struct mmc_host *host) |
| 1327 | { |
| 1328 | if (host->ios.power_mode != MMC_POWER_ON) { |
| 1329 | int err; |
| 1330 | u32 ocr; |
| 1331 | |
Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 1332 | host->mode = MMC_MODE_SD; |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 1333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | mmc_power_up(host); |
| 1335 | mmc_idle_cards(host); |
| 1336 | |
Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 1337 | err = mmc_send_app_op_cond(host, 0, &ocr); |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 1338 | |
| 1339 | /* |
Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 1340 | * If we fail to detect any SD cards then try |
| 1341 | * searching for MMC cards. |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 1342 | */ |
Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 1343 | if (err != MMC_ERR_NONE) { |
| 1344 | host->mode = MMC_MODE_MMC; |
| 1345 | |
| 1346 | err = mmc_send_op_cond(host, 0, &ocr); |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 1347 | if (err != MMC_ERR_NONE) |
| 1348 | return; |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 1349 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | |
| 1351 | host->ocr = mmc_select_voltage(host, ocr); |
| 1352 | |
| 1353 | /* |
| 1354 | * Since we're changing the OCR value, we seem to |
| 1355 | * need to tell some cards to go back to the idle |
| 1356 | * state. We wait 1ms to give cards time to |
| 1357 | * respond. |
| 1358 | */ |
| 1359 | if (host->ocr) |
| 1360 | mmc_idle_cards(host); |
| 1361 | } else { |
| 1362 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; |
| 1363 | host->ios.clock = host->f_min; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 1364 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | |
| 1366 | /* |
| 1367 | * We should remember the OCR mask from the existing |
| 1368 | * cards, and detect the new cards OCR mask, combine |
| 1369 | * the two and re-select the VDD. However, if we do |
| 1370 | * change VDD, we should do an idle, and then do a |
| 1371 | * full re-initialisation. We would need to notify |
| 1372 | * drivers so that they can re-setup the cards as |
| 1373 | * well, while keeping their queues at bay. |
| 1374 | * |
| 1375 | * For the moment, we take the easy way out - if the |
| 1376 | * new cards don't like our currently selected VDD, |
| 1377 | * they drop off the bus. |
| 1378 | */ |
| 1379 | } |
| 1380 | |
| 1381 | if (host->ocr == 0) |
| 1382 | return; |
| 1383 | |
| 1384 | /* |
| 1385 | * Send the selected OCR multiple times... until the cards |
| 1386 | * all get the idea that they should be ready for CMD2. |
| 1387 | * (My SanDisk card seems to need this.) |
| 1388 | */ |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 1389 | if (host->mode == MMC_MODE_SD) |
| 1390 | mmc_send_app_op_cond(host, host->ocr, NULL); |
| 1391 | else |
| 1392 | mmc_send_op_cond(host, host->ocr, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | |
| 1394 | mmc_discover_cards(host); |
| 1395 | |
| 1396 | /* |
| 1397 | * Ok, now switch to push-pull mode. |
| 1398 | */ |
| 1399 | host->ios.bus_mode = MMC_BUSMODE_PUSHPULL; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 1400 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | |
| 1402 | mmc_read_csds(host); |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 1403 | |
Pierre Ossman | 7ccd266 | 2006-11-08 23:03:10 +0100 | [diff] [blame] | 1404 | if (host->mode == MMC_MODE_SD) { |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 1405 | mmc_read_scrs(host); |
Pierre Ossman | 7ccd266 | 2006-11-08 23:03:10 +0100 | [diff] [blame] | 1406 | mmc_read_switch_caps(host); |
| 1407 | } else |
Philip Langdale | bce40a3 | 2006-10-21 12:35:02 +0200 | [diff] [blame] | 1408 | mmc_process_ext_csds(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | |
| 1412 | /** |
| 1413 | * mmc_detect_change - process change of state on a MMC socket |
| 1414 | * @host: host which changed state. |
Richard Purdie | 8dc0033 | 2005-09-08 17:53:01 +0100 | [diff] [blame] | 1415 | * @delay: optional delay to wait before detection (jiffies) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | * |
| 1417 | * All we know is that card(s) have been inserted or removed |
| 1418 | * from the socket(s). We don't know which socket or cards. |
| 1419 | */ |
Richard Purdie | 8dc0033 | 2005-09-08 17:53:01 +0100 | [diff] [blame] | 1420 | void mmc_detect_change(struct mmc_host *host, unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1422 | mmc_schedule_delayed_work(&host->detect, delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | EXPORT_SYMBOL(mmc_detect_change); |
| 1426 | |
| 1427 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1428 | static void mmc_rescan(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1430 | struct mmc_host *host = |
| 1431 | container_of(work, struct mmc_host, detect.work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | struct list_head *l, *n; |
Timo Teras | 25a122f | 2006-10-25 09:37:41 +0300 | [diff] [blame] | 1433 | unsigned char power_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | |
| 1435 | mmc_claim_host(host); |
| 1436 | |
Timo Teras | 25a122f | 2006-10-25 09:37:41 +0300 | [diff] [blame] | 1437 | /* |
| 1438 | * Check for removed cards and newly inserted ones. We check for |
| 1439 | * removed cards first so we can intelligently re-select the VDD. |
| 1440 | */ |
| 1441 | power_mode = host->ios.power_mode; |
| 1442 | if (power_mode == MMC_POWER_ON) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | mmc_check_cards(host); |
| 1444 | |
| 1445 | mmc_setup(host); |
| 1446 | |
Timo Teras | 25a122f | 2006-10-25 09:37:41 +0300 | [diff] [blame] | 1447 | /* |
| 1448 | * Some broken cards process CMD1 even in stand-by state. There is |
| 1449 | * no reply, but an ILLEGAL_COMMAND error is cached and returned |
| 1450 | * after next command. We poll for card status here to clear any |
| 1451 | * possibly pending error. |
| 1452 | */ |
| 1453 | if (power_mode == MMC_POWER_ON) |
| 1454 | mmc_check_cards(host); |
| 1455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | if (!list_empty(&host->cards)) { |
| 1457 | /* |
| 1458 | * (Re-)calculate the fastest clock rate which the |
| 1459 | * attached cards and the host support. |
| 1460 | */ |
| 1461 | host->ios.clock = mmc_calculate_clock(host); |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 1462 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | mmc_release_host(host); |
| 1466 | |
| 1467 | list_for_each_safe(l, n, &host->cards) { |
| 1468 | struct mmc_card *card = mmc_list_to_card(l); |
| 1469 | |
| 1470 | /* |
| 1471 | * If this is a new and good card, register it. |
| 1472 | */ |
| 1473 | if (!mmc_card_present(card) && !mmc_card_dead(card)) { |
| 1474 | if (mmc_register_card(card)) |
| 1475 | mmc_card_set_dead(card); |
| 1476 | else |
| 1477 | mmc_card_set_present(card); |
| 1478 | } |
| 1479 | |
| 1480 | /* |
| 1481 | * If this card is dead, destroy it. |
| 1482 | */ |
| 1483 | if (mmc_card_dead(card)) { |
| 1484 | list_del(&card->node); |
| 1485 | mmc_remove_card(card); |
| 1486 | } |
| 1487 | } |
| 1488 | |
| 1489 | /* |
| 1490 | * If we discover that there are no cards on the |
| 1491 | * bus, turn off the clock and power down. |
| 1492 | */ |
| 1493 | if (list_empty(&host->cards)) |
| 1494 | mmc_power_off(host); |
| 1495 | } |
| 1496 | |
| 1497 | |
| 1498 | /** |
| 1499 | * mmc_alloc_host - initialise the per-host structure. |
| 1500 | * @extra: sizeof private data structure |
| 1501 | * @dev: pointer to host device model structure |
| 1502 | * |
| 1503 | * Initialise the per-host structure. |
| 1504 | */ |
| 1505 | struct mmc_host *mmc_alloc_host(int extra, struct device *dev) |
| 1506 | { |
| 1507 | struct mmc_host *host; |
| 1508 | |
Russell King | 00b137c | 2005-08-19 09:41:24 +0100 | [diff] [blame] | 1509 | host = mmc_alloc_host_sysfs(extra, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | if (host) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | spin_lock_init(&host->lock); |
| 1512 | init_waitqueue_head(&host->wq); |
| 1513 | INIT_LIST_HEAD(&host->cards); |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1514 | INIT_DELAYED_WORK(&host->detect, mmc_rescan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | /* |
| 1517 | * By default, hosts do not support SGIO or large requests. |
| 1518 | * They have to set these according to their abilities. |
| 1519 | */ |
| 1520 | host->max_hw_segs = 1; |
| 1521 | host->max_phys_segs = 1; |
| 1522 | host->max_sectors = 1 << (PAGE_CACHE_SHIFT - 9); |
| 1523 | host->max_seg_size = PAGE_CACHE_SIZE; |
| 1524 | } |
| 1525 | |
| 1526 | return host; |
| 1527 | } |
| 1528 | |
| 1529 | EXPORT_SYMBOL(mmc_alloc_host); |
| 1530 | |
| 1531 | /** |
| 1532 | * mmc_add_host - initialise host hardware |
| 1533 | * @host: mmc host |
| 1534 | */ |
| 1535 | int mmc_add_host(struct mmc_host *host) |
| 1536 | { |
Russell King | 00b137c | 2005-08-19 09:41:24 +0100 | [diff] [blame] | 1537 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | |
Russell King | 00b137c | 2005-08-19 09:41:24 +0100 | [diff] [blame] | 1539 | ret = mmc_add_host_sysfs(host); |
| 1540 | if (ret == 0) { |
| 1541 | mmc_power_off(host); |
Richard Purdie | 8dc0033 | 2005-09-08 17:53:01 +0100 | [diff] [blame] | 1542 | mmc_detect_change(host, 0); |
Russell King | 00b137c | 2005-08-19 09:41:24 +0100 | [diff] [blame] | 1543 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | |
Russell King | 00b137c | 2005-08-19 09:41:24 +0100 | [diff] [blame] | 1545 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | } |
| 1547 | |
| 1548 | EXPORT_SYMBOL(mmc_add_host); |
| 1549 | |
| 1550 | /** |
| 1551 | * mmc_remove_host - remove host hardware |
| 1552 | * @host: mmc host |
| 1553 | * |
| 1554 | * Unregister and remove all cards associated with this host, |
| 1555 | * and power down the MMC bus. |
| 1556 | */ |
| 1557 | void mmc_remove_host(struct mmc_host *host) |
| 1558 | { |
| 1559 | struct list_head *l, *n; |
| 1560 | |
| 1561 | list_for_each_safe(l, n, &host->cards) { |
| 1562 | struct mmc_card *card = mmc_list_to_card(l); |
| 1563 | |
| 1564 | mmc_remove_card(card); |
| 1565 | } |
| 1566 | |
| 1567 | mmc_power_off(host); |
Russell King | 00b137c | 2005-08-19 09:41:24 +0100 | [diff] [blame] | 1568 | mmc_remove_host_sysfs(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | EXPORT_SYMBOL(mmc_remove_host); |
| 1572 | |
| 1573 | /** |
| 1574 | * mmc_free_host - free the host structure |
| 1575 | * @host: mmc host |
| 1576 | * |
| 1577 | * Free the host once all references to it have been dropped. |
| 1578 | */ |
| 1579 | void mmc_free_host(struct mmc_host *host) |
| 1580 | { |
Pierre Ossman | 7104e2d | 2006-10-04 02:15:41 -0700 | [diff] [blame] | 1581 | mmc_flush_scheduled_work(); |
Russell King | 00b137c | 2005-08-19 09:41:24 +0100 | [diff] [blame] | 1582 | mmc_free_host_sysfs(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | EXPORT_SYMBOL(mmc_free_host); |
| 1586 | |
| 1587 | #ifdef CONFIG_PM |
| 1588 | |
| 1589 | /** |
| 1590 | * mmc_suspend_host - suspend a host |
| 1591 | * @host: mmc host |
| 1592 | * @state: suspend mode (PM_SUSPEND_xxx) |
| 1593 | */ |
Pavel Machek | e5378ca | 2005-04-16 15:25:29 -0700 | [diff] [blame] | 1594 | int mmc_suspend_host(struct mmc_host *host, pm_message_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | { |
| 1596 | mmc_claim_host(host); |
| 1597 | mmc_deselect_cards(host); |
| 1598 | mmc_power_off(host); |
| 1599 | mmc_release_host(host); |
| 1600 | |
| 1601 | return 0; |
| 1602 | } |
| 1603 | |
| 1604 | EXPORT_SYMBOL(mmc_suspend_host); |
| 1605 | |
| 1606 | /** |
| 1607 | * mmc_resume_host - resume a previously suspended host |
| 1608 | * @host: mmc host |
| 1609 | */ |
| 1610 | int mmc_resume_host(struct mmc_host *host) |
| 1611 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1612 | mmc_rescan(&host->detect.work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | |
| 1614 | return 0; |
| 1615 | } |
| 1616 | |
| 1617 | EXPORT_SYMBOL(mmc_resume_host); |
| 1618 | |
| 1619 | #endif |
| 1620 | |
| 1621 | MODULE_LICENSE("GPL"); |