Skip to content

Commit

Permalink
[MMC] Fix SD timeout calculation
Browse files Browse the repository at this point in the history
Secure Digital cards use a different algorithm to calculate the timeout
for data transfers. Using the MMC one works often, but not always.

Signed-off-by: Pierre Ossman <[email protected]>
Signed-off-by: Russell King <[email protected]>
  • Loading branch information
Pierre Ossman authored and Russell King committed Sep 7, 2006
1 parent f57b225 commit 385e322
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
15 changes: 13 additions & 2 deletions drivers/mmc/mmc.c
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,7 @@ static void mmc_read_scrs(struct mmc_host *host)
struct mmc_request mrq;
struct mmc_command cmd;
struct mmc_data data;
unsigned int timeout_us;

struct scatterlist sg;

Expand Down Expand Up @@ -947,8 +948,18 @@ static void mmc_read_scrs(struct mmc_host *host)

memset(&data, 0, sizeof(struct mmc_data));

data.timeout_ns = card->csd.tacc_ns * 10;
data.timeout_clks = card->csd.tacc_clks * 10;
data.timeout_ns = card->csd.tacc_ns * 100;
data.timeout_clks = card->csd.tacc_clks * 100;

timeout_us = data.timeout_ns / 1000;
timeout_us += data.timeout_clks * 1000 /
(host->ios.clock / 1000);

if (timeout_us > 100000) {
data.timeout_ns = 100000000;
data.timeout_clks = 0;
}

data.blksz_bits = 3;
data.blksz = 1 << 3;
data.blocks = 1;
Expand Down
44 changes: 36 additions & 8 deletions drivers/mmc/mmc_block.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <linux/mutex.h>

#include <linux/mmc/card.h>
#include <linux/mmc/host.h>
#include <linux/mmc/protocol.h>

#include <asm/system.h>
Expand Down Expand Up @@ -171,28 +172,55 @@ static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)

brq.cmd.arg = req->sector << 9;
brq.cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
brq.data.timeout_ns = card->csd.tacc_ns * 10;
brq.data.timeout_clks = card->csd.tacc_clks * 10;
brq.data.blksz_bits = md->block_bits;
brq.data.blksz = 1 << md->block_bits;
brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
brq.stop.opcode = MMC_STOP_TRANSMISSION;
brq.stop.arg = 0;
brq.stop.flags = MMC_RSP_R1B | MMC_CMD_AC;

brq.data.timeout_ns = card->csd.tacc_ns * 10;
brq.data.timeout_clks = card->csd.tacc_clks * 10;

/*
* Scale up the timeout by the r2w factor
*/
if (rq_data_dir(req) == WRITE) {
brq.data.timeout_ns <<= card->csd.r2w_factor;
brq.data.timeout_clks <<= card->csd.r2w_factor;
}

/*
* SD cards use a 100 multiplier and has a upper limit
*/
if (mmc_card_sd(card)) {
unsigned int limit_us, timeout_us;

brq.data.timeout_ns *= 10;
brq.data.timeout_clks *= 10;

if (rq_data_dir(req) == READ)
limit_us = 100000;
else
limit_us = 250000;

timeout_us = brq.data.timeout_ns / 1000;
timeout_us += brq.data.timeout_clks * 1000 /
(card->host->ios.clock / 1000);

if (timeout_us > limit_us) {
brq.data.timeout_ns = limit_us * 1000;
brq.data.timeout_clks = 0;
}
}

if (rq_data_dir(req) == READ) {
brq.cmd.opcode = brq.data.blocks > 1 ? MMC_READ_MULTIPLE_BLOCK : MMC_READ_SINGLE_BLOCK;
brq.data.flags |= MMC_DATA_READ;
} else {
brq.cmd.opcode = MMC_WRITE_BLOCK;
brq.data.flags |= MMC_DATA_WRITE;
brq.data.blocks = 1;

/*
* Scale up the timeout by the r2w factor
*/
brq.data.timeout_ns <<= card->csd.r2w_factor;
brq.data.timeout_clks <<= card->csd.r2w_factor;
}

if (brq.data.blocks > 1) {
Expand Down

0 comments on commit 385e322

Please sign in to comment.