blob: 5046a1661342f2e13ac8417c14ed697163368390 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/mmc/mmc.c
3 *
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
Pierre Ossman5b4fd9a2005-09-06 15:18:56 -07005 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
6 * SD support Copyright (C) 2005 Pierre Ossman, All Rights Reserved.
Philip Langdalebce40a32006-10-21 12:35:02 +02007 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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 Torvalds1da177e2005-04-16 15:20:36 -070013#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 Ossmanb57c43a2005-09-06 15:18:53 -070021#include <asm/scatterlist.h>
22#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
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 Torvalds1da177e2005-04-16 15:20:36 -070030#define CMD_RETRIES 3
31
32/*
33 * OCR Bit positions to 10s of Vdd mV.
34 */
35static 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
41static const unsigned int tran_exp[] = {
42 10000, 100000, 1000000, 10000000,
43 0, 0, 0, 0
44};
45
46static 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
51static const unsigned int tacc_exp[] = {
52 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
53};
54
55static 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 Kingfe10c6a2006-05-04 13:51:45 +010062 * mmc_request_done - finish processing an MMC request
63 * @host: MMC host which completed request
64 * @mrq: MMC request which request
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 *
66 * MMC drivers should call this function when they have completed
Russell Kingfe10c6a2006-05-04 13:51:45 +010067 * their processing of a request.
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 */
69void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
70{
71 struct mmc_command *cmd = mrq->cmd;
Russell King920e70c2006-05-04 18:22:51 +010072 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 Torvalds1da177e2005-04-16 15:20:36 -070079
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
89EXPORT_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 */
99void
100mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
101{
Russell King920e70c2006-05-04 18:22:51 +0100102 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 Torvalds1da177e2005-04-16 15:20:36 -0700105
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100106 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 mrq->cmd->error = 0;
109 mrq->cmd->mrq = mrq;
110 if (mrq->data) {
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100111 BUG_ON(mrq->data->blksz > host->max_blk_size);
Pierre Ossman55db8902006-11-21 17:55:45 +0100112 BUG_ON(mrq->data->blocks > host->max_blk_count);
113 BUG_ON(mrq->data->blocks * mrq->data->blksz >
114 host->max_req_size);
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 mrq->cmd->data = mrq->data;
117 mrq->data->error = 0;
118 mrq->data->mrq = mrq;
119 if (mrq->stop) {
120 mrq->data->stop = mrq->stop;
121 mrq->stop->error = 0;
122 mrq->stop->mrq = mrq;
123 }
124 }
125 host->ops->request(host, mrq);
126}
127
128EXPORT_SYMBOL(mmc_start_request);
129
130static void mmc_wait_done(struct mmc_request *mrq)
131{
132 complete(mrq->done_data);
133}
134
135int mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
136{
Ingo Molnar0afffc72006-07-03 00:25:35 -0700137 DECLARE_COMPLETION_ONSTACK(complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 mrq->done_data = &complete;
140 mrq->done = mmc_wait_done;
141
142 mmc_start_request(host, mrq);
143
144 wait_for_completion(&complete);
145
146 return 0;
147}
148
149EXPORT_SYMBOL(mmc_wait_for_req);
150
151/**
152 * mmc_wait_for_cmd - start a command and wait for completion
153 * @host: MMC host to start command
154 * @cmd: MMC command to start
155 * @retries: maximum number of retries
156 *
157 * Start a new MMC command for a host, and wait for the command
158 * to complete. Return any error that occurred while the command
159 * was executing. Do not attempt to parse the response.
160 */
161int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
162{
163 struct mmc_request mrq;
164
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100165 BUG_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 memset(&mrq, 0, sizeof(struct mmc_request));
168
169 memset(cmd->resp, 0, sizeof(cmd->resp));
170 cmd->retries = retries;
171
172 mrq.cmd = cmd;
173 cmd->data = NULL;
174
175 mmc_wait_for_req(host, &mrq);
176
177 return cmd->error;
178}
179
180EXPORT_SYMBOL(mmc_wait_for_cmd);
181
Pierre Ossman335eadf2005-09-06 15:18:50 -0700182/**
183 * mmc_wait_for_app_cmd - start an application command and wait for
184 completion
185 * @host: MMC host to start command
186 * @rca: RCA to send MMC_APP_CMD to
187 * @cmd: MMC command to start
188 * @retries: maximum number of retries
189 *
190 * Sends a MMC_APP_CMD, checks the card response, sends the command
191 * in the parameter and waits for it to complete. Return any error
192 * that occurred while the command was executing. Do not attempt to
193 * parse the response.
194 */
195int mmc_wait_for_app_cmd(struct mmc_host *host, unsigned int rca,
196 struct mmc_command *cmd, int retries)
197{
198 struct mmc_request mrq;
199 struct mmc_command appcmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Pierre Ossman335eadf2005-09-06 15:18:50 -0700201 int i, err;
202
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100203 BUG_ON(!host->claimed);
Pierre Ossman335eadf2005-09-06 15:18:50 -0700204 BUG_ON(retries < 0);
205
206 err = MMC_ERR_INVALID;
207
208 /*
209 * We have to resend MMC_APP_CMD for each attempt so
210 * we cannot use the retries field in mmc_command.
211 */
212 for (i = 0;i <= retries;i++) {
213 memset(&mrq, 0, sizeof(struct mmc_request));
214
215 appcmd.opcode = MMC_APP_CMD;
216 appcmd.arg = rca << 16;
Russell Kinge9225172006-02-02 12:23:12 +0000217 appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossman335eadf2005-09-06 15:18:50 -0700218 appcmd.retries = 0;
219 memset(appcmd.resp, 0, sizeof(appcmd.resp));
220 appcmd.data = NULL;
221
222 mrq.cmd = &appcmd;
223 appcmd.data = NULL;
224
225 mmc_wait_for_req(host, &mrq);
226
227 if (appcmd.error) {
228 err = appcmd.error;
229 continue;
230 }
231
232 /* Check that card supported application commands */
233 if (!(appcmd.resp[0] & R1_APP_CMD))
234 return MMC_ERR_FAILED;
235
236 memset(&mrq, 0, sizeof(struct mmc_request));
237
238 memset(cmd->resp, 0, sizeof(cmd->resp));
239 cmd->retries = 0;
240
241 mrq.cmd = cmd;
242 cmd->data = NULL;
243
244 mmc_wait_for_req(host, &mrq);
245
246 err = cmd->error;
247 if (cmd->error == MMC_ERR_NONE)
248 break;
249 }
250
251 return err;
252}
253
254EXPORT_SYMBOL(mmc_wait_for_app_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Russell Kingd773d722006-09-07 15:57:12 +0100256/**
257 * mmc_set_data_timeout - set the timeout for a data command
258 * @data: data phase for command
259 * @card: the MMC card associated with the data transfer
260 * @write: flag to differentiate reads from writes
261 */
262void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
263 int write)
264{
265 unsigned int mult;
266
267 /*
268 * SD cards use a 100 multiplier rather than 10
269 */
270 mult = mmc_card_sd(card) ? 100 : 10;
271
272 /*
273 * Scale up the multiplier (and therefore the timeout) by
274 * the r2w factor for writes.
275 */
276 if (write)
277 mult <<= card->csd.r2w_factor;
278
279 data->timeout_ns = card->csd.tacc_ns * mult;
280 data->timeout_clks = card->csd.tacc_clks * mult;
281
282 /*
283 * SD cards also have an upper limit on the timeout.
284 */
285 if (mmc_card_sd(card)) {
286 unsigned int timeout_us, limit_us;
287
288 timeout_us = data->timeout_ns / 1000;
289 timeout_us += data->timeout_clks * 1000 /
290 (card->host->ios.clock / 1000);
291
292 if (write)
293 limit_us = 250000;
294 else
295 limit_us = 100000;
296
Philip Langdalefba68bd2007-01-04 06:57:32 -0800297 /*
298 * SDHC cards always use these fixed values.
299 */
300 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
Russell Kingd773d722006-09-07 15:57:12 +0100301 data->timeout_ns = limit_us * 1000;
302 data->timeout_clks = 0;
303 }
304 }
305}
306EXPORT_SYMBOL(mmc_set_data_timeout);
307
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700308static int mmc_select_card(struct mmc_host *host, struct mmc_card *card);
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310/**
311 * __mmc_claim_host - exclusively claim a host
312 * @host: mmc host to claim
313 * @card: mmc card to claim host for
314 *
315 * Claim a host for a set of operations. If a valid card
316 * is passed and this wasn't the last card selected, select
317 * the card before returning.
318 *
319 * Note: you should use mmc_card_claim_host or mmc_claim_host.
320 */
321int __mmc_claim_host(struct mmc_host *host, struct mmc_card *card)
322{
323 DECLARE_WAITQUEUE(wait, current);
324 unsigned long flags;
325 int err = 0;
326
327 add_wait_queue(&host->wq, &wait);
328 spin_lock_irqsave(&host->lock, flags);
329 while (1) {
330 set_current_state(TASK_UNINTERRUPTIBLE);
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100331 if (!host->claimed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 break;
333 spin_unlock_irqrestore(&host->lock, flags);
334 schedule();
335 spin_lock_irqsave(&host->lock, flags);
336 }
337 set_current_state(TASK_RUNNING);
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100338 host->claimed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 spin_unlock_irqrestore(&host->lock, flags);
340 remove_wait_queue(&host->wq, &wait);
341
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700342 if (card != (void *)-1) {
343 err = mmc_select_card(host, card);
344 if (err != MMC_ERR_NONE)
345 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347
348 return err;
349}
350
351EXPORT_SYMBOL(__mmc_claim_host);
352
353/**
354 * mmc_release_host - release a host
355 * @host: mmc host to release
356 *
357 * Release a MMC host, allowing others to claim the host
358 * for their operations.
359 */
360void mmc_release_host(struct mmc_host *host)
361{
362 unsigned long flags;
363
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100364 BUG_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100367 host->claimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 spin_unlock_irqrestore(&host->lock, flags);
369
370 wake_up(&host->wq);
371}
372
373EXPORT_SYMBOL(mmc_release_host);
374
Russell King920e70c2006-05-04 18:22:51 +0100375static inline void mmc_set_ios(struct mmc_host *host)
376{
377 struct mmc_ios *ios = &host->ios;
378
379 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u width %u\n",
380 mmc_hostname(host), ios->clock, ios->bus_mode,
381 ios->power_mode, ios->chip_select, ios->vdd,
382 ios->bus_width);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800383
Russell King920e70c2006-05-04 18:22:51 +0100384 host->ops->set_ios(host, ios);
385}
386
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700387static int mmc_select_card(struct mmc_host *host, struct mmc_card *card)
388{
389 int err;
390 struct mmc_command cmd;
391
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100392 BUG_ON(!host->claimed);
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700393
394 if (host->card_selected == card)
395 return MMC_ERR_NONE;
396
397 host->card_selected = card;
398
399 cmd.opcode = MMC_SELECT_CARD;
400 cmd.arg = card->rca << 16;
Russell Kinge9225172006-02-02 12:23:12 +0000401 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700402
403 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
404 if (err != MMC_ERR_NONE)
405 return err;
406
Pierre Ossmanf2182782005-09-06 15:18:55 -0700407 /*
Philip Langdalee45a1bd2006-10-29 10:14:19 +0100408 * We can only change the bus width of SD cards when
409 * they are selected so we have to put the handling
Pierre Ossmanf2182782005-09-06 15:18:55 -0700410 * here.
Philip Langdalee45a1bd2006-10-29 10:14:19 +0100411 *
412 * The card is in 1 bit mode by default so
413 * we only need to change if it supports the
414 * wider version.
Pierre Ossmanf2182782005-09-06 15:18:55 -0700415 */
Philip Langdalee45a1bd2006-10-29 10:14:19 +0100416 if (mmc_card_sd(card) &&
417 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
418
Pierre Ossmanf2182782005-09-06 15:18:55 -0700419 /*
Philip Langdalee45a1bd2006-10-29 10:14:19 +0100420 * Default bus width is 1 bit.
421 */
422 host->ios.bus_width = MMC_BUS_WIDTH_1;
423
424 if (host->caps & MMC_CAP_4_BIT_DATA) {
Pierre Ossmanf2182782005-09-06 15:18:55 -0700425 struct mmc_command cmd;
426 cmd.opcode = SD_APP_SET_BUS_WIDTH;
427 cmd.arg = SD_BUS_WIDTH_4;
Russell Kinge9225172006-02-02 12:23:12 +0000428 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanf2182782005-09-06 15:18:55 -0700429
430 err = mmc_wait_for_app_cmd(host, card->rca, &cmd,
431 CMD_RETRIES);
432 if (err != MMC_ERR_NONE)
433 return err;
434
435 host->ios.bus_width = MMC_BUS_WIDTH_4;
436 }
437 }
438
Russell King920e70c2006-05-04 18:22:51 +0100439 mmc_set_ios(host);
Pierre Ossmanf2182782005-09-06 15:18:55 -0700440
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700441 return MMC_ERR_NONE;
442}
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444/*
445 * Ensure that no card is selected.
446 */
447static void mmc_deselect_cards(struct mmc_host *host)
448{
449 struct mmc_command cmd;
450
451 if (host->card_selected) {
452 host->card_selected = NULL;
453
454 cmd.opcode = MMC_SELECT_CARD;
455 cmd.arg = 0;
Russell Kinge9225172006-02-02 12:23:12 +0000456 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 mmc_wait_for_cmd(host, &cmd, 0);
459 }
460}
461
462
463static inline void mmc_delay(unsigned int ms)
464{
Pierre Ossman73778122006-10-22 22:13:10 +0200465 if (ms < 1000 / HZ) {
466 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 mdelay(ms);
468 } else {
Pierre Ossman73778122006-10-22 22:13:10 +0200469 msleep(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471}
472
473/*
474 * Mask off any voltages we don't support and select
475 * the lowest voltage
476 */
477static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
478{
479 int bit;
480
481 ocr &= host->ocr_avail;
482
483 bit = ffs(ocr);
484 if (bit) {
485 bit -= 1;
486
Timo Teras63ef7312006-11-02 19:43:27 +0100487 ocr &= 3 << bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 host->ios.vdd = bit;
Russell King920e70c2006-05-04 18:22:51 +0100490 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 } else {
492 ocr = 0;
493 }
494
495 return ocr;
496}
497
498#define UNSTUFF_BITS(resp,start,size) \
499 ({ \
500 const int __size = size; \
501 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
502 const int __off = 3 - ((start) / 32); \
503 const int __shft = (start) & 31; \
504 u32 __res; \
505 \
506 __res = resp[__off] >> __shft; \
507 if (__size + __shft > 32) \
508 __res |= resp[__off-1] << ((32 - __shft) % 32); \
509 __res & __mask; \
510 })
511
512/*
513 * Given the decoded CSD structure, decode the raw CID to our CID structure.
514 */
515static void mmc_decode_cid(struct mmc_card *card)
516{
517 u32 *resp = card->raw_cid;
518
519 memset(&card->cid, 0, sizeof(struct mmc_cid));
520
Pierre Ossman335eadf2005-09-06 15:18:50 -0700521 if (mmc_card_sd(card)) {
522 /*
523 * SD doesn't currently have a version field so we will
524 * have to assume we can parse this.
525 */
526 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
527 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
528 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
529 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
530 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
531 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
532 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
533 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
534 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
535 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
536 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
537 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Pierre Ossman335eadf2005-09-06 15:18:50 -0700539 card->cid.year += 2000; /* SD cards year offset */
Pierre Ossmana00fc092005-09-06 15:18:52 -0700540 } else {
Pierre Ossman335eadf2005-09-06 15:18:50 -0700541 /*
542 * The selection of the format here is based upon published
543 * specs from sandisk and from what people have reported.
544 */
545 switch (card->csd.mmca_vsn) {
546 case 0: /* MMC v1.0 - v1.2 */
547 case 1: /* MMC v1.4 */
548 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
549 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
550 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
551 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
552 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
553 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
554 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
555 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
556 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
557 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
558 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
559 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
560 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
561 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Pierre Ossman335eadf2005-09-06 15:18:50 -0700563 case 2: /* MMC v2.0 - v2.2 */
564 case 3: /* MMC v3.1 - v3.3 */
Pierre Ossmancb757b42006-01-08 14:23:02 +0000565 case 4: /* MMC v4 */
Pierre Ossman335eadf2005-09-06 15:18:50 -0700566 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
567 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
568 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
569 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
570 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
571 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
572 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
573 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
574 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
575 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
576 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
577 break;
578
579 default:
580 printk("%s: card has unknown MMCA version %d\n",
581 mmc_hostname(card->host), card->csd.mmca_vsn);
582 mmc_card_set_bad(card);
583 break;
584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586}
587
588/*
589 * Given a 128-bit response, decode to our card CSD structure.
590 */
591static void mmc_decode_csd(struct mmc_card *card)
592{
593 struct mmc_csd *csd = &card->csd;
594 unsigned int e, m, csd_struct;
595 u32 *resp = card->raw_csd;
596
Pierre Ossman335eadf2005-09-06 15:18:50 -0700597 if (mmc_card_sd(card)) {
598 csd_struct = UNSTUFF_BITS(resp, 126, 2);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800599
600 switch (csd_struct) {
601 case 0:
602 m = UNSTUFF_BITS(resp, 115, 4);
603 e = UNSTUFF_BITS(resp, 112, 3);
604 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
605 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
606
607 m = UNSTUFF_BITS(resp, 99, 4);
608 e = UNSTUFF_BITS(resp, 96, 3);
609 csd->max_dtr = tran_exp[e] * tran_mant[m];
610 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
611
612 e = UNSTUFF_BITS(resp, 47, 3);
613 m = UNSTUFF_BITS(resp, 62, 12);
614 csd->capacity = (1 + m) << (e + 2);
615
616 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
617 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
618 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
619 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
620 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
621 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
622 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
623 break;
624 case 1:
625 /*
626 * This is a block-addressed SDHC card. Most
627 * interesting fields are unused and have fixed
628 * values. To avoid getting tripped by buggy cards,
629 * we assume those fixed values ourselves.
630 */
631 mmc_card_set_blockaddr(card);
632
633 csd->tacc_ns = 0; /* Unused */
634 csd->tacc_clks = 0; /* Unused */
635
636 m = UNSTUFF_BITS(resp, 99, 4);
637 e = UNSTUFF_BITS(resp, 96, 3);
638 csd->max_dtr = tran_exp[e] * tran_mant[m];
639 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
640
641 m = UNSTUFF_BITS(resp, 48, 22);
642 csd->capacity = (1 + m) << 10;
643
644 csd->read_blkbits = 9;
645 csd->read_partial = 0;
646 csd->write_misalign = 0;
647 csd->read_misalign = 0;
648 csd->r2w_factor = 4; /* Unused */
649 csd->write_blkbits = 9;
650 csd->write_partial = 0;
651 break;
652 default:
Pierre Ossman335eadf2005-09-06 15:18:50 -0700653 printk("%s: unrecognised CSD structure version %d\n",
654 mmc_hostname(card->host), csd_struct);
655 mmc_card_set_bad(card);
656 return;
657 }
Pierre Ossmana00fc092005-09-06 15:18:52 -0700658 } else {
Pierre Ossman335eadf2005-09-06 15:18:50 -0700659 /*
660 * We only understand CSD structure v1.1 and v1.2.
661 * v1.2 has extra information in bits 15, 11 and 10.
662 */
663 csd_struct = UNSTUFF_BITS(resp, 126, 2);
664 if (csd_struct != 1 && csd_struct != 2) {
665 printk("%s: unrecognised CSD structure version %d\n",
666 mmc_hostname(card->host), csd_struct);
667 mmc_card_set_bad(card);
668 return;
669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Pierre Ossman335eadf2005-09-06 15:18:50 -0700671 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
672 m = UNSTUFF_BITS(resp, 115, 4);
673 e = UNSTUFF_BITS(resp, 112, 3);
674 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
675 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Pierre Ossman335eadf2005-09-06 15:18:50 -0700677 m = UNSTUFF_BITS(resp, 99, 4);
678 e = UNSTUFF_BITS(resp, 96, 3);
679 csd->max_dtr = tran_exp[e] * tran_mant[m];
680 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Pierre Ossman335eadf2005-09-06 15:18:50 -0700682 e = UNSTUFF_BITS(resp, 47, 3);
683 m = UNSTUFF_BITS(resp, 62, 12);
684 csd->capacity = (1 + m) << (e + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Pierre Ossman335eadf2005-09-06 15:18:50 -0700686 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
Russell Kinga6f6c962006-01-03 22:38:44 +0000687 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
688 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
689 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
Russell King37be4e72006-05-02 17:24:59 +0100690 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
Russell Kinga6f6c962006-01-03 22:38:44 +0000691 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
692 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
Pierre Ossman335eadf2005-09-06 15:18:50 -0700693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
696/*
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700697 * Given a 64-bit response, decode to our card SCR structure.
698 */
699static void mmc_decode_scr(struct mmc_card *card)
700{
701 struct sd_scr *scr = &card->scr;
702 unsigned int scr_struct;
703 u32 resp[4];
704
705 BUG_ON(!mmc_card_sd(card));
706
707 resp[3] = card->raw_scr[1];
708 resp[2] = card->raw_scr[0];
709
710 scr_struct = UNSTUFF_BITS(resp, 60, 4);
711 if (scr_struct != 0) {
712 printk("%s: unrecognised SCR structure version %d\n",
713 mmc_hostname(card->host), scr_struct);
714 mmc_card_set_bad(card);
715 return;
716 }
717
718 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
719 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
720}
721
722/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 * Locate a MMC card on this MMC host given a raw CID.
724 */
725static struct mmc_card *mmc_find_card(struct mmc_host *host, u32 *raw_cid)
726{
727 struct mmc_card *card;
728
729 list_for_each_entry(card, &host->cards, node) {
730 if (memcmp(card->raw_cid, raw_cid, sizeof(card->raw_cid)) == 0)
731 return card;
732 }
733 return NULL;
734}
735
736/*
737 * Allocate a new MMC card, and assign a unique RCA.
738 */
739static struct mmc_card *
740mmc_alloc_card(struct mmc_host *host, u32 *raw_cid, unsigned int *frca)
741{
742 struct mmc_card *card, *c;
743 unsigned int rca = *frca;
744
745 card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL);
746 if (!card)
747 return ERR_PTR(-ENOMEM);
748
749 mmc_init_card(card, host);
750 memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid));
751
752 again:
753 list_for_each_entry(c, &host->cards, node)
754 if (c->rca == rca) {
755 rca++;
756 goto again;
757 }
758
759 card->rca = rca;
760
761 *frca = rca;
762
763 return card;
764}
765
766/*
767 * Tell attached cards to go to IDLE state
768 */
769static void mmc_idle_cards(struct mmc_host *host)
770{
771 struct mmc_command cmd;
772
Pierre Ossman865e9f12005-09-03 16:45:02 +0100773 host->ios.chip_select = MMC_CS_HIGH;
Russell King920e70c2006-05-04 18:22:51 +0100774 mmc_set_ios(host);
Pierre Ossman865e9f12005-09-03 16:45:02 +0100775
776 mmc_delay(1);
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 cmd.opcode = MMC_GO_IDLE_STATE;
779 cmd.arg = 0;
Russell Kinge9225172006-02-02 12:23:12 +0000780 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 mmc_wait_for_cmd(host, &cmd, 0);
783
784 mmc_delay(1);
Pierre Ossman865e9f12005-09-03 16:45:02 +0100785
786 host->ios.chip_select = MMC_CS_DONTCARE;
Russell King920e70c2006-05-04 18:22:51 +0100787 mmc_set_ios(host);
Pierre Ossman865e9f12005-09-03 16:45:02 +0100788
789 mmc_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
792/*
Russell King45f82452005-12-14 14:57:35 +0000793 * Apply power to the MMC stack. This is a two-stage process.
794 * First, we enable power to the card without the clock running.
795 * We then wait a bit for the power to stabilise. Finally,
796 * enable the bus drivers and clock to the card.
797 *
798 * We must _NOT_ enable the clock prior to power stablising.
799 *
800 * If a host does all the power sequencing itself, ignore the
801 * initial MMC_POWER_UP stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 */
803static void mmc_power_up(struct mmc_host *host)
804{
805 int bit = fls(host->ocr_avail) - 1;
806
807 host->ios.vdd = bit;
808 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
Pierre Ossman865e9f12005-09-03 16:45:02 +0100809 host->ios.chip_select = MMC_CS_DONTCARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 host->ios.power_mode = MMC_POWER_UP;
Pierre Ossmanf2182782005-09-06 15:18:55 -0700811 host->ios.bus_width = MMC_BUS_WIDTH_1;
Russell King920e70c2006-05-04 18:22:51 +0100812 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 mmc_delay(1);
815
816 host->ios.clock = host->f_min;
817 host->ios.power_mode = MMC_POWER_ON;
Russell King920e70c2006-05-04 18:22:51 +0100818 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 mmc_delay(2);
821}
822
823static void mmc_power_off(struct mmc_host *host)
824{
825 host->ios.clock = 0;
826 host->ios.vdd = 0;
827 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
Pierre Ossman865e9f12005-09-03 16:45:02 +0100828 host->ios.chip_select = MMC_CS_DONTCARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 host->ios.power_mode = MMC_POWER_OFF;
Pierre Ossmanf2182782005-09-06 15:18:55 -0700830 host->ios.bus_width = MMC_BUS_WIDTH_1;
Russell King920e70c2006-05-04 18:22:51 +0100831 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
834static int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
835{
836 struct mmc_command cmd;
837 int i, err = 0;
838
839 cmd.opcode = MMC_SEND_OP_COND;
840 cmd.arg = ocr;
Russell Kinge9225172006-02-02 12:23:12 +0000841 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 for (i = 100; i; i--) {
844 err = mmc_wait_for_cmd(host, &cmd, 0);
845 if (err != MMC_ERR_NONE)
846 break;
847
848 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
849 break;
850
851 err = MMC_ERR_TIMEOUT;
852
853 mmc_delay(10);
854 }
855
856 if (rocr)
857 *rocr = cmd.resp[0];
858
859 return err;
860}
861
Pierre Ossman335eadf2005-09-06 15:18:50 -0700862static int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
863{
864 struct mmc_command cmd;
865 int i, err = 0;
866
867 cmd.opcode = SD_APP_OP_COND;
868 cmd.arg = ocr;
Russell Kinge9225172006-02-02 12:23:12 +0000869 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
Pierre Ossman335eadf2005-09-06 15:18:50 -0700870
871 for (i = 100; i; i--) {
872 err = mmc_wait_for_app_cmd(host, 0, &cmd, CMD_RETRIES);
873 if (err != MMC_ERR_NONE)
874 break;
875
876 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
877 break;
878
879 err = MMC_ERR_TIMEOUT;
880
881 mmc_delay(10);
882 }
883
884 if (rocr)
885 *rocr = cmd.resp[0];
886
887 return err;
888}
889
Philip Langdalefba68bd2007-01-04 06:57:32 -0800890static int mmc_send_if_cond(struct mmc_host *host, u32 ocr, int *rsd2)
891{
892 struct mmc_command cmd;
893 int err, sd2;
894 static const u8 test_pattern = 0xAA;
895
896 /*
897 * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND
898 * before SD_APP_OP_COND. This command will harmlessly fail for
899 * SD 1.0 cards.
900 */
901 cmd.opcode = SD_SEND_IF_COND;
902 cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | test_pattern;
903 cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
904
905 err = mmc_wait_for_cmd(host, &cmd, 0);
906 if (err == MMC_ERR_NONE) {
907 if ((cmd.resp[0] & 0xFF) == test_pattern) {
908 sd2 = 1;
909 } else {
910 sd2 = 0;
911 err = MMC_ERR_FAILED;
912 }
913 } else {
914 /*
915 * Treat errors as SD 1.0 card.
916 */
917 sd2 = 0;
918 err = MMC_ERR_NONE;
919 }
920 if (rsd2)
921 *rsd2 = sd2;
922 return err;
923}
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925/*
926 * Discover cards by requesting their CID. If this command
927 * times out, it is not an error; there are no further cards
928 * to be discovered. Add new cards to the list.
929 *
930 * Create a mmc_card entry for each discovered card, assigning
931 * it an RCA, and save the raw CID for decoding later.
932 */
933static void mmc_discover_cards(struct mmc_host *host)
934{
935 struct mmc_card *card;
936 unsigned int first_rca = 1, err;
937
938 while (1) {
939 struct mmc_command cmd;
940
941 cmd.opcode = MMC_ALL_SEND_CID;
942 cmd.arg = 0;
Russell Kinge9225172006-02-02 12:23:12 +0000943 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
946 if (err == MMC_ERR_TIMEOUT) {
947 err = MMC_ERR_NONE;
948 break;
949 }
950 if (err != MMC_ERR_NONE) {
951 printk(KERN_ERR "%s: error requesting CID: %d\n",
Russell Kingd366b642005-08-19 09:40:08 +0100952 mmc_hostname(host), err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 break;
954 }
955
956 card = mmc_find_card(host, cmd.resp);
957 if (!card) {
958 card = mmc_alloc_card(host, cmd.resp, &first_rca);
959 if (IS_ERR(card)) {
960 err = PTR_ERR(card);
961 break;
962 }
963 list_add(&card->node, &host->cards);
964 }
965
966 card->state &= ~MMC_STATE_DEAD;
967
Pierre Ossman335eadf2005-09-06 15:18:50 -0700968 if (host->mode == MMC_MODE_SD) {
969 mmc_card_set_sd(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Pierre Ossman335eadf2005-09-06 15:18:50 -0700971 cmd.opcode = SD_SEND_RELATIVE_ADDR;
972 cmd.arg = 0;
Russell Kinge9225172006-02-02 12:23:12 +0000973 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
Pierre Ossman335eadf2005-09-06 15:18:50 -0700974
975 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
976 if (err != MMC_ERR_NONE)
977 mmc_card_set_dead(card);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700978 else {
Pierre Ossman335eadf2005-09-06 15:18:50 -0700979 card->rca = cmd.resp[0] >> 16;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700980
981 if (!host->ops->get_ro) {
982 printk(KERN_WARNING "%s: host does not "
983 "support reading read-only "
984 "switch. assuming write-enable.\n",
985 mmc_hostname(host));
986 } else {
987 if (host->ops->get_ro(host))
988 mmc_card_set_readonly(card);
989 }
990 }
991 } else {
Pierre Ossman335eadf2005-09-06 15:18:50 -0700992 cmd.opcode = MMC_SET_RELATIVE_ADDR;
993 cmd.arg = card->rca << 16;
Russell Kinge9225172006-02-02 12:23:12 +0000994 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossman335eadf2005-09-06 15:18:50 -0700995
996 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
997 if (err != MMC_ERR_NONE)
998 mmc_card_set_dead(card);
999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001}
1002
1003static void mmc_read_csds(struct mmc_host *host)
1004{
1005 struct mmc_card *card;
1006
1007 list_for_each_entry(card, &host->cards, node) {
1008 struct mmc_command cmd;
1009 int err;
1010
1011 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
1012 continue;
1013
1014 cmd.opcode = MMC_SEND_CSD;
1015 cmd.arg = card->rca << 16;
Russell Kinge9225172006-02-02 12:23:12 +00001016 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1019 if (err != MMC_ERR_NONE) {
1020 mmc_card_set_dead(card);
1021 continue;
1022 }
1023
1024 memcpy(card->raw_csd, cmd.resp, sizeof(card->raw_csd));
1025
1026 mmc_decode_csd(card);
1027 mmc_decode_cid(card);
1028 }
1029}
1030
Philip Langdalebce40a32006-10-21 12:35:02 +02001031static void mmc_process_ext_csds(struct mmc_host *host)
1032{
1033 int err;
1034 struct mmc_card *card;
1035
1036 struct mmc_request mrq;
1037 struct mmc_command cmd;
1038 struct mmc_data data;
1039
1040 struct scatterlist sg;
1041
1042 /*
1043 * As the ext_csd is so large and mostly unused, we don't store the
1044 * raw block in mmc_card.
1045 */
1046 u8 *ext_csd;
1047 ext_csd = kmalloc(512, GFP_KERNEL);
1048 if (!ext_csd) {
1049 printk("%s: could not allocate a buffer to receive the ext_csd."
1050 "mmc v4 cards will be treated as v3.\n",
1051 mmc_hostname(host));
1052 return;
1053 }
1054
1055 list_for_each_entry(card, &host->cards, node) {
1056 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
1057 continue;
1058 if (mmc_card_sd(card))
1059 continue;
1060 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
1061 continue;
1062
1063 err = mmc_select_card(host, card);
1064 if (err != MMC_ERR_NONE) {
1065 mmc_card_set_dead(card);
1066 continue;
1067 }
1068
1069 memset(&cmd, 0, sizeof(struct mmc_command));
1070
1071 cmd.opcode = MMC_SEND_EXT_CSD;
1072 cmd.arg = 0;
1073 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1074
1075 memset(&data, 0, sizeof(struct mmc_data));
1076
1077 mmc_set_data_timeout(&data, card, 0);
1078
1079 data.blksz = 512;
1080 data.blocks = 1;
1081 data.flags = MMC_DATA_READ;
1082 data.sg = &sg;
1083 data.sg_len = 1;
1084
1085 memset(&mrq, 0, sizeof(struct mmc_request));
1086
1087 mrq.cmd = &cmd;
1088 mrq.data = &data;
1089
1090 sg_init_one(&sg, ext_csd, 512);
1091
1092 mmc_wait_for_req(host, &mrq);
1093
1094 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
Pierre Ossmanae06eaf2007-01-07 16:59:06 +01001095 printk("%s: unable to read EXT_CSD, performance "
1096 "might suffer.\n", mmc_hostname(card->host));
Philip Langdalebce40a32006-10-21 12:35:02 +02001097 continue;
1098 }
1099
1100 switch (ext_csd[EXT_CSD_CARD_TYPE]) {
1101 case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
1102 card->ext_csd.hs_max_dtr = 52000000;
1103 break;
1104 case EXT_CSD_CARD_TYPE_26:
1105 card->ext_csd.hs_max_dtr = 26000000;
1106 break;
1107 default:
1108 /* MMC v4 spec says this cannot happen */
1109 printk("%s: card is mmc v4 but doesn't support "
1110 "any high-speed modes.\n",
1111 mmc_hostname(card->host));
Philip Langdalebce40a32006-10-21 12:35:02 +02001112 continue;
1113 }
1114
1115 /* Activate highspeed support. */
1116 cmd.opcode = MMC_SWITCH;
1117 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1118 (EXT_CSD_HS_TIMING << 16) |
1119 (1 << 8) |
1120 EXT_CSD_CMD_SET_NORMAL;
1121 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1122
1123 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1124 if (err != MMC_ERR_NONE) {
1125 printk("%s: failed to switch card to mmc v4 "
1126 "high-speed mode.\n",
1127 mmc_hostname(card->host));
1128 continue;
1129 }
1130
1131 mmc_card_set_highspeed(card);
Philip Langdalee45a1bd2006-10-29 10:14:19 +01001132
1133 /* Check for host support for wide-bus modes. */
1134 if (!(host->caps & MMC_CAP_4_BIT_DATA)) {
1135 continue;
1136 }
1137
1138 /* Activate 4-bit support. */
1139 cmd.opcode = MMC_SWITCH;
1140 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1141 (EXT_CSD_BUS_WIDTH << 16) |
1142 (EXT_CSD_BUS_WIDTH_4 << 8) |
1143 EXT_CSD_CMD_SET_NORMAL;
1144 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1145
1146 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1147 if (err != MMC_ERR_NONE) {
1148 printk("%s: failed to switch card to "
1149 "mmc v4 4-bit bus mode.\n",
1150 mmc_hostname(card->host));
1151 continue;
1152 }
1153
1154 host->ios.bus_width = MMC_BUS_WIDTH_4;
Philip Langdalebce40a32006-10-21 12:35:02 +02001155 }
1156
1157 kfree(ext_csd);
1158
1159 mmc_deselect_cards(host);
1160}
1161
Pierre Ossmanb57c43a2005-09-06 15:18:53 -07001162static void mmc_read_scrs(struct mmc_host *host)
1163{
1164 int err;
1165 struct mmc_card *card;
Pierre Ossmanb57c43a2005-09-06 15:18:53 -07001166 struct mmc_request mrq;
1167 struct mmc_command cmd;
1168 struct mmc_data data;
Pierre Ossmanb57c43a2005-09-06 15:18:53 -07001169 struct scatterlist sg;
1170
1171 list_for_each_entry(card, &host->cards, node) {
1172 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
1173 continue;
1174 if (!mmc_card_sd(card))
1175 continue;
1176
1177 err = mmc_select_card(host, card);
1178 if (err != MMC_ERR_NONE) {
1179 mmc_card_set_dead(card);
1180 continue;
1181 }
1182
1183 memset(&cmd, 0, sizeof(struct mmc_command));
1184
1185 cmd.opcode = MMC_APP_CMD;
1186 cmd.arg = card->rca << 16;
Russell Kinge9225172006-02-02 12:23:12 +00001187 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanb57c43a2005-09-06 15:18:53 -07001188
1189 err = mmc_wait_for_cmd(host, &cmd, 0);
1190 if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD)) {
1191 mmc_card_set_dead(card);
1192 continue;
1193 }
1194
1195 memset(&cmd, 0, sizeof(struct mmc_command));
1196
1197 cmd.opcode = SD_APP_SEND_SCR;
1198 cmd.arg = 0;
Russell Kinge9225172006-02-02 12:23:12 +00001199 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanb57c43a2005-09-06 15:18:53 -07001200
1201 memset(&data, 0, sizeof(struct mmc_data));
1202
Russell Kingd773d722006-09-07 15:57:12 +01001203 mmc_set_data_timeout(&data, card, 0);
Pierre Ossman385e32272006-06-18 14:34:37 +02001204
Pavel Pisa2c171bf2006-05-19 21:48:03 +01001205 data.blksz = 1 << 3;
Pierre Ossmanb57c43a2005-09-06 15:18:53 -07001206 data.blocks = 1;
1207 data.flags = MMC_DATA_READ;
1208 data.sg = &sg;
1209 data.sg_len = 1;
1210
1211 memset(&mrq, 0, sizeof(struct mmc_request));
1212
1213 mrq.cmd = &cmd;
1214 mrq.data = &data;
1215
1216 sg_init_one(&sg, (u8*)card->raw_scr, 8);
1217
Pierre Ossmane781de42005-12-05 10:00:50 +00001218 mmc_wait_for_req(host, &mrq);
1219
1220 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
Pierre Ossmanb57c43a2005-09-06 15:18:53 -07001221 mmc_card_set_dead(card);
1222 continue;
1223 }
1224
1225 card->raw_scr[0] = ntohl(card->raw_scr[0]);
1226 card->raw_scr[1] = ntohl(card->raw_scr[1]);
1227
1228 mmc_decode_scr(card);
1229 }
1230
1231 mmc_deselect_cards(host);
1232}
1233
Pierre Ossman7ccd2662006-11-08 23:03:10 +01001234static void mmc_read_switch_caps(struct mmc_host *host)
1235{
1236 int err;
1237 struct mmc_card *card;
1238 struct mmc_request mrq;
1239 struct mmc_command cmd;
1240 struct mmc_data data;
1241 unsigned char *status;
1242 struct scatterlist sg;
1243
1244 status = kmalloc(64, GFP_KERNEL);
1245 if (!status) {
1246 printk(KERN_WARNING "%s: Unable to allocate buffer for "
1247 "reading switch capabilities.\n",
1248 mmc_hostname(host));
1249 return;
1250 }
1251
1252 list_for_each_entry(card, &host->cards, node) {
1253 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
1254 continue;
1255 if (!mmc_card_sd(card))
1256 continue;
1257 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
1258 continue;
1259
1260 err = mmc_select_card(host, card);
1261 if (err != MMC_ERR_NONE) {
1262 mmc_card_set_dead(card);
1263 continue;
1264 }
1265
1266 memset(&cmd, 0, sizeof(struct mmc_command));
1267
1268 cmd.opcode = SD_SWITCH;
1269 cmd.arg = 0x00FFFFF1;
1270 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1271
1272 memset(&data, 0, sizeof(struct mmc_data));
1273
1274 mmc_set_data_timeout(&data, card, 0);
1275
1276 data.blksz = 64;
1277 data.blocks = 1;
1278 data.flags = MMC_DATA_READ;
1279 data.sg = &sg;
1280 data.sg_len = 1;
1281
1282 memset(&mrq, 0, sizeof(struct mmc_request));
1283
1284 mrq.cmd = &cmd;
1285 mrq.data = &data;
1286
1287 sg_init_one(&sg, status, 64);
1288
1289 mmc_wait_for_req(host, &mrq);
1290
1291 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
Pierre Ossmanae06eaf2007-01-07 16:59:06 +01001292 printk("%s: unable to read switch capabilities, "
1293 "performance might suffer.\n",
1294 mmc_hostname(card->host));
Pierre Ossman7ccd2662006-11-08 23:03:10 +01001295 continue;
1296 }
1297
1298 if (status[13] & 0x02)
1299 card->sw_caps.hs_max_dtr = 50000000;
1300
1301 memset(&cmd, 0, sizeof(struct mmc_command));
1302
1303 cmd.opcode = SD_SWITCH;
1304 cmd.arg = 0x80FFFFF1;
1305 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1306
1307 memset(&data, 0, sizeof(struct mmc_data));
1308
1309 mmc_set_data_timeout(&data, card, 0);
1310
1311 data.blksz = 64;
1312 data.blocks = 1;
1313 data.flags = MMC_DATA_READ;
1314 data.sg = &sg;
1315 data.sg_len = 1;
1316
1317 memset(&mrq, 0, sizeof(struct mmc_request));
1318
1319 mrq.cmd = &cmd;
1320 mrq.data = &data;
1321
1322 sg_init_one(&sg, status, 64);
1323
1324 mmc_wait_for_req(host, &mrq);
1325
Pierre Ossmanae06eaf2007-01-07 16:59:06 +01001326 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE ||
1327 (status[16] & 0xF) != 1) {
Pierre Ossman7ccd2662006-11-08 23:03:10 +01001328 printk(KERN_WARNING "%s: Problem switching card "
1329 "into high-speed mode!\n",
1330 mmc_hostname(host));
1331 continue;
1332 }
1333
1334 mmc_card_set_highspeed(card);
1335 }
1336
1337 kfree(status);
1338
1339 mmc_deselect_cards(host);
1340}
1341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342static unsigned int mmc_calculate_clock(struct mmc_host *host)
1343{
1344 struct mmc_card *card;
1345 unsigned int max_dtr = host->f_max;
1346
1347 list_for_each_entry(card, &host->cards, node)
Philip Langdalebce40a32006-10-21 12:35:02 +02001348 if (!mmc_card_dead(card)) {
Pierre Ossman7ccd2662006-11-08 23:03:10 +01001349 if (mmc_card_highspeed(card) && mmc_card_sd(card)) {
1350 if (max_dtr > card->sw_caps.hs_max_dtr)
1351 max_dtr = card->sw_caps.hs_max_dtr;
1352 } else if (mmc_card_highspeed(card) && !mmc_card_sd(card)) {
Philip Langdalebce40a32006-10-21 12:35:02 +02001353 if (max_dtr > card->ext_csd.hs_max_dtr)
Pierre Ossman7ccd2662006-11-08 23:03:10 +01001354 max_dtr = card->ext_csd.hs_max_dtr;
Philip Langdalebce40a32006-10-21 12:35:02 +02001355 } else if (max_dtr > card->csd.max_dtr) {
1356 max_dtr = card->csd.max_dtr;
1357 }
1358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Russell King920e70c2006-05-04 18:22:51 +01001360 pr_debug("%s: selected %d.%03dMHz transfer rate\n",
1361 mmc_hostname(host),
Russell Kingc6563172006-03-29 09:30:20 +01001362 max_dtr / 1000000, (max_dtr / 1000) % 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 return max_dtr;
1365}
1366
1367/*
1368 * Check whether cards we already know about are still present.
1369 * We do this by requesting status, and checking whether a card
1370 * responds.
1371 *
1372 * A request for status does not cause a state change in data
1373 * transfer mode.
1374 */
1375static void mmc_check_cards(struct mmc_host *host)
1376{
1377 struct list_head *l, *n;
1378
1379 mmc_deselect_cards(host);
1380
1381 list_for_each_safe(l, n, &host->cards) {
1382 struct mmc_card *card = mmc_list_to_card(l);
1383 struct mmc_command cmd;
1384 int err;
1385
1386 cmd.opcode = MMC_SEND_STATUS;
1387 cmd.arg = card->rca << 16;
Russell Kinge9225172006-02-02 12:23:12 +00001388 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1391 if (err == MMC_ERR_NONE)
1392 continue;
1393
1394 mmc_card_set_dead(card);
1395 }
1396}
1397
1398static void mmc_setup(struct mmc_host *host)
1399{
1400 if (host->ios.power_mode != MMC_POWER_ON) {
1401 int err;
1402 u32 ocr;
1403
Pierre Ossmana00fc092005-09-06 15:18:52 -07001404 host->mode = MMC_MODE_SD;
Pierre Ossman335eadf2005-09-06 15:18:50 -07001405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 mmc_power_up(host);
1407 mmc_idle_cards(host);
1408
Philip Langdalefba68bd2007-01-04 06:57:32 -08001409 err = mmc_send_if_cond(host, host->ocr_avail, NULL);
1410 if (err != MMC_ERR_NONE) {
1411 return;
1412 }
Pierre Ossmana00fc092005-09-06 15:18:52 -07001413 err = mmc_send_app_op_cond(host, 0, &ocr);
Pierre Ossman335eadf2005-09-06 15:18:50 -07001414
1415 /*
Pierre Ossmana00fc092005-09-06 15:18:52 -07001416 * If we fail to detect any SD cards then try
1417 * searching for MMC cards.
Pierre Ossman335eadf2005-09-06 15:18:50 -07001418 */
Pierre Ossmana00fc092005-09-06 15:18:52 -07001419 if (err != MMC_ERR_NONE) {
1420 host->mode = MMC_MODE_MMC;
1421
1422 err = mmc_send_op_cond(host, 0, &ocr);
Pierre Ossman335eadf2005-09-06 15:18:50 -07001423 if (err != MMC_ERR_NONE)
1424 return;
Pierre Ossman335eadf2005-09-06 15:18:50 -07001425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 host->ocr = mmc_select_voltage(host, ocr);
1428
1429 /*
1430 * Since we're changing the OCR value, we seem to
1431 * need to tell some cards to go back to the idle
1432 * state. We wait 1ms to give cards time to
1433 * respond.
1434 */
1435 if (host->ocr)
1436 mmc_idle_cards(host);
1437 } else {
1438 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1439 host->ios.clock = host->f_min;
Russell King920e70c2006-05-04 18:22:51 +01001440 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 /*
1443 * We should remember the OCR mask from the existing
1444 * cards, and detect the new cards OCR mask, combine
1445 * the two and re-select the VDD. However, if we do
1446 * change VDD, we should do an idle, and then do a
1447 * full re-initialisation. We would need to notify
1448 * drivers so that they can re-setup the cards as
1449 * well, while keeping their queues at bay.
1450 *
1451 * For the moment, we take the easy way out - if the
1452 * new cards don't like our currently selected VDD,
1453 * they drop off the bus.
1454 */
1455 }
1456
1457 if (host->ocr == 0)
1458 return;
1459
1460 /*
1461 * Send the selected OCR multiple times... until the cards
1462 * all get the idea that they should be ready for CMD2.
1463 * (My SanDisk card seems to need this.)
1464 */
Philip Langdalefba68bd2007-01-04 06:57:32 -08001465 if (host->mode == MMC_MODE_SD) {
1466 int err, sd2;
1467 err = mmc_send_if_cond(host, host->ocr, &sd2);
1468 if (err == MMC_ERR_NONE) {
1469 /*
1470 * If SD_SEND_IF_COND indicates an SD 2.0
1471 * compliant card and we should set bit 30
1472 * of the ocr to indicate that we can handle
1473 * block-addressed SDHC cards.
1474 */
1475 mmc_send_app_op_cond(host, host->ocr | (sd2 << 30), NULL);
1476 }
1477 } else {
Pierre Ossman335eadf2005-09-06 15:18:50 -07001478 mmc_send_op_cond(host, host->ocr, NULL);
Philip Langdalefba68bd2007-01-04 06:57:32 -08001479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 mmc_discover_cards(host);
1482
1483 /*
1484 * Ok, now switch to push-pull mode.
1485 */
1486 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
Russell King920e70c2006-05-04 18:22:51 +01001487 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 mmc_read_csds(host);
Pierre Ossmanb57c43a2005-09-06 15:18:53 -07001490
Pierre Ossman7ccd2662006-11-08 23:03:10 +01001491 if (host->mode == MMC_MODE_SD) {
Pierre Ossmanb57c43a2005-09-06 15:18:53 -07001492 mmc_read_scrs(host);
Pierre Ossman7ccd2662006-11-08 23:03:10 +01001493 mmc_read_switch_caps(host);
1494 } else
Philip Langdalebce40a32006-10-21 12:35:02 +02001495 mmc_process_ext_csds(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
1497
1498
1499/**
1500 * mmc_detect_change - process change of state on a MMC socket
1501 * @host: host which changed state.
Richard Purdie8dc00332005-09-08 17:53:01 +01001502 * @delay: optional delay to wait before detection (jiffies)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 *
1504 * All we know is that card(s) have been inserted or removed
1505 * from the socket(s). We don't know which socket or cards.
1506 */
Richard Purdie8dc00332005-09-08 17:53:01 +01001507void mmc_detect_change(struct mmc_host *host, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
David Howellsc4028952006-11-22 14:57:56 +00001509 mmc_schedule_delayed_work(&host->detect, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
1512EXPORT_SYMBOL(mmc_detect_change);
1513
1514
David Howellsc4028952006-11-22 14:57:56 +00001515static void mmc_rescan(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
David Howellsc4028952006-11-22 14:57:56 +00001517 struct mmc_host *host =
1518 container_of(work, struct mmc_host, detect.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 struct list_head *l, *n;
Timo Teras25a122f2006-10-25 09:37:41 +03001520 unsigned char power_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 mmc_claim_host(host);
1523
Timo Teras25a122f2006-10-25 09:37:41 +03001524 /*
1525 * Check for removed cards and newly inserted ones. We check for
1526 * removed cards first so we can intelligently re-select the VDD.
1527 */
1528 power_mode = host->ios.power_mode;
1529 if (power_mode == MMC_POWER_ON)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 mmc_check_cards(host);
1531
1532 mmc_setup(host);
1533
Timo Teras25a122f2006-10-25 09:37:41 +03001534 /*
1535 * Some broken cards process CMD1 even in stand-by state. There is
1536 * no reply, but an ILLEGAL_COMMAND error is cached and returned
1537 * after next command. We poll for card status here to clear any
1538 * possibly pending error.
1539 */
1540 if (power_mode == MMC_POWER_ON)
1541 mmc_check_cards(host);
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 if (!list_empty(&host->cards)) {
1544 /*
1545 * (Re-)calculate the fastest clock rate which the
1546 * attached cards and the host support.
1547 */
1548 host->ios.clock = mmc_calculate_clock(host);
Russell King920e70c2006-05-04 18:22:51 +01001549 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 }
1551
1552 mmc_release_host(host);
1553
1554 list_for_each_safe(l, n, &host->cards) {
1555 struct mmc_card *card = mmc_list_to_card(l);
1556
1557 /*
1558 * If this is a new and good card, register it.
1559 */
1560 if (!mmc_card_present(card) && !mmc_card_dead(card)) {
1561 if (mmc_register_card(card))
1562 mmc_card_set_dead(card);
1563 else
1564 mmc_card_set_present(card);
1565 }
1566
1567 /*
1568 * If this card is dead, destroy it.
1569 */
1570 if (mmc_card_dead(card)) {
1571 list_del(&card->node);
1572 mmc_remove_card(card);
1573 }
1574 }
1575
1576 /*
1577 * If we discover that there are no cards on the
1578 * bus, turn off the clock and power down.
1579 */
1580 if (list_empty(&host->cards))
1581 mmc_power_off(host);
1582}
1583
1584
1585/**
1586 * mmc_alloc_host - initialise the per-host structure.
1587 * @extra: sizeof private data structure
1588 * @dev: pointer to host device model structure
1589 *
1590 * Initialise the per-host structure.
1591 */
1592struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
1593{
1594 struct mmc_host *host;
1595
Russell King00b137c2005-08-19 09:41:24 +01001596 host = mmc_alloc_host_sysfs(extra, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if (host) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 spin_lock_init(&host->lock);
1599 init_waitqueue_head(&host->wq);
1600 INIT_LIST_HEAD(&host->cards);
David Howellsc4028952006-11-22 14:57:56 +00001601 INIT_DELAYED_WORK(&host->detect, mmc_rescan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 /*
1604 * By default, hosts do not support SGIO or large requests.
1605 * They have to set these according to their abilities.
1606 */
1607 host->max_hw_segs = 1;
1608 host->max_phys_segs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 host->max_seg_size = PAGE_CACHE_SIZE;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001610
Pierre Ossman55db8902006-11-21 17:55:45 +01001611 host->max_req_size = PAGE_CACHE_SIZE;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001612 host->max_blk_size = 512;
Pierre Ossman55db8902006-11-21 17:55:45 +01001613 host->max_blk_count = PAGE_CACHE_SIZE / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 }
1615
1616 return host;
1617}
1618
1619EXPORT_SYMBOL(mmc_alloc_host);
1620
1621/**
1622 * mmc_add_host - initialise host hardware
1623 * @host: mmc host
1624 */
1625int mmc_add_host(struct mmc_host *host)
1626{
Russell King00b137c2005-08-19 09:41:24 +01001627 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Russell King00b137c2005-08-19 09:41:24 +01001629 ret = mmc_add_host_sysfs(host);
1630 if (ret == 0) {
1631 mmc_power_off(host);
Richard Purdie8dc00332005-09-08 17:53:01 +01001632 mmc_detect_change(host, 0);
Russell King00b137c2005-08-19 09:41:24 +01001633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Russell King00b137c2005-08-19 09:41:24 +01001635 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636}
1637
1638EXPORT_SYMBOL(mmc_add_host);
1639
1640/**
1641 * mmc_remove_host - remove host hardware
1642 * @host: mmc host
1643 *
1644 * Unregister and remove all cards associated with this host,
1645 * and power down the MMC bus.
1646 */
1647void mmc_remove_host(struct mmc_host *host)
1648{
1649 struct list_head *l, *n;
1650
1651 list_for_each_safe(l, n, &host->cards) {
1652 struct mmc_card *card = mmc_list_to_card(l);
1653
1654 mmc_remove_card(card);
1655 }
1656
1657 mmc_power_off(host);
Russell King00b137c2005-08-19 09:41:24 +01001658 mmc_remove_host_sysfs(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
1660
1661EXPORT_SYMBOL(mmc_remove_host);
1662
1663/**
1664 * mmc_free_host - free the host structure
1665 * @host: mmc host
1666 *
1667 * Free the host once all references to it have been dropped.
1668 */
1669void mmc_free_host(struct mmc_host *host)
1670{
Pierre Ossman7104e2d2006-10-04 02:15:41 -07001671 mmc_flush_scheduled_work();
Russell King00b137c2005-08-19 09:41:24 +01001672 mmc_free_host_sysfs(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673}
1674
1675EXPORT_SYMBOL(mmc_free_host);
1676
1677#ifdef CONFIG_PM
1678
1679/**
1680 * mmc_suspend_host - suspend a host
1681 * @host: mmc host
1682 * @state: suspend mode (PM_SUSPEND_xxx)
1683 */
Pavel Macheke5378ca2005-04-16 15:25:29 -07001684int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
1686 mmc_claim_host(host);
1687 mmc_deselect_cards(host);
1688 mmc_power_off(host);
1689 mmc_release_host(host);
1690
1691 return 0;
1692}
1693
1694EXPORT_SYMBOL(mmc_suspend_host);
1695
1696/**
1697 * mmc_resume_host - resume a previously suspended host
1698 * @host: mmc host
1699 */
1700int mmc_resume_host(struct mmc_host *host)
1701{
David Howellsc4028952006-11-22 14:57:56 +00001702 mmc_rescan(&host->detect.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 return 0;
1705}
1706
1707EXPORT_SYMBOL(mmc_resume_host);
1708
1709#endif
1710
1711MODULE_LICENSE("GPL");