blob: e8f896c61b3474a592a1303275230dab070e6b1f [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{
Pierre Ossman976d9272007-04-13 22:47:01 +0200102#ifdef CONFIG_MMC_DEBUG
103 unsigned int i, sz;
104#endif
105
Russell King920e70c2006-05-04 18:22:51 +0100106 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
107 mmc_hostname(host), mrq->cmd->opcode,
108 mrq->cmd->arg, mrq->cmd->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100110 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 mrq->cmd->error = 0;
113 mrq->cmd->mrq = mrq;
114 if (mrq->data) {
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100115 BUG_ON(mrq->data->blksz > host->max_blk_size);
Pierre Ossman55db8902006-11-21 17:55:45 +0100116 BUG_ON(mrq->data->blocks > host->max_blk_count);
117 BUG_ON(mrq->data->blocks * mrq->data->blksz >
118 host->max_req_size);
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100119
Pierre Ossman976d9272007-04-13 22:47:01 +0200120#ifdef CONFIG_MMC_DEBUG
121 sz = 0;
122 for (i = 0;i < mrq->data->sg_len;i++)
123 sz += mrq->data->sg[i].length;
124 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
125#endif
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 mrq->cmd->data = mrq->data;
128 mrq->data->error = 0;
129 mrq->data->mrq = mrq;
130 if (mrq->stop) {
131 mrq->data->stop = mrq->stop;
132 mrq->stop->error = 0;
133 mrq->stop->mrq = mrq;
134 }
135 }
136 host->ops->request(host, mrq);
137}
138
139EXPORT_SYMBOL(mmc_start_request);
140
141static void mmc_wait_done(struct mmc_request *mrq)
142{
143 complete(mrq->done_data);
144}
145
146int mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
147{
Ingo Molnar0afffc72006-07-03 00:25:35 -0700148 DECLARE_COMPLETION_ONSTACK(complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 mrq->done_data = &complete;
151 mrq->done = mmc_wait_done;
152
153 mmc_start_request(host, mrq);
154
155 wait_for_completion(&complete);
156
157 return 0;
158}
159
160EXPORT_SYMBOL(mmc_wait_for_req);
161
162/**
163 * mmc_wait_for_cmd - start a command and wait for completion
164 * @host: MMC host to start command
165 * @cmd: MMC command to start
166 * @retries: maximum number of retries
167 *
168 * Start a new MMC command for a host, and wait for the command
169 * to complete. Return any error that occurred while the command
170 * was executing. Do not attempt to parse the response.
171 */
172int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
173{
174 struct mmc_request mrq;
175
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100176 BUG_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 memset(&mrq, 0, sizeof(struct mmc_request));
179
180 memset(cmd->resp, 0, sizeof(cmd->resp));
181 cmd->retries = retries;
182
183 mrq.cmd = cmd;
184 cmd->data = NULL;
185
186 mmc_wait_for_req(host, &mrq);
187
188 return cmd->error;
189}
190
191EXPORT_SYMBOL(mmc_wait_for_cmd);
192
Pierre Ossman335eadf2005-09-06 15:18:50 -0700193/**
194 * mmc_wait_for_app_cmd - start an application command and wait for
195 completion
196 * @host: MMC host to start command
197 * @rca: RCA to send MMC_APP_CMD to
198 * @cmd: MMC command to start
199 * @retries: maximum number of retries
200 *
201 * Sends a MMC_APP_CMD, checks the card response, sends the command
202 * in the parameter and waits for it to complete. Return any error
203 * that occurred while the command was executing. Do not attempt to
204 * parse the response.
205 */
206int mmc_wait_for_app_cmd(struct mmc_host *host, unsigned int rca,
207 struct mmc_command *cmd, int retries)
208{
209 struct mmc_request mrq;
210 struct mmc_command appcmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Pierre Ossman335eadf2005-09-06 15:18:50 -0700212 int i, err;
213
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100214 BUG_ON(!host->claimed);
Pierre Ossman335eadf2005-09-06 15:18:50 -0700215 BUG_ON(retries < 0);
216
217 err = MMC_ERR_INVALID;
218
219 /*
220 * We have to resend MMC_APP_CMD for each attempt so
221 * we cannot use the retries field in mmc_command.
222 */
223 for (i = 0;i <= retries;i++) {
224 memset(&mrq, 0, sizeof(struct mmc_request));
225
226 appcmd.opcode = MMC_APP_CMD;
227 appcmd.arg = rca << 16;
Russell Kinge9225172006-02-02 12:23:12 +0000228 appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossman335eadf2005-09-06 15:18:50 -0700229 appcmd.retries = 0;
230 memset(appcmd.resp, 0, sizeof(appcmd.resp));
231 appcmd.data = NULL;
232
233 mrq.cmd = &appcmd;
234 appcmd.data = NULL;
235
236 mmc_wait_for_req(host, &mrq);
237
238 if (appcmd.error) {
239 err = appcmd.error;
240 continue;
241 }
242
243 /* Check that card supported application commands */
244 if (!(appcmd.resp[0] & R1_APP_CMD))
245 return MMC_ERR_FAILED;
246
247 memset(&mrq, 0, sizeof(struct mmc_request));
248
249 memset(cmd->resp, 0, sizeof(cmd->resp));
250 cmd->retries = 0;
251
252 mrq.cmd = cmd;
253 cmd->data = NULL;
254
255 mmc_wait_for_req(host, &mrq);
256
257 err = cmd->error;
258 if (cmd->error == MMC_ERR_NONE)
259 break;
260 }
261
262 return err;
263}
264
265EXPORT_SYMBOL(mmc_wait_for_app_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Russell Kingd773d722006-09-07 15:57:12 +0100267/**
268 * mmc_set_data_timeout - set the timeout for a data command
269 * @data: data phase for command
270 * @card: the MMC card associated with the data transfer
271 * @write: flag to differentiate reads from writes
272 */
273void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
274 int write)
275{
276 unsigned int mult;
277
278 /*
279 * SD cards use a 100 multiplier rather than 10
280 */
281 mult = mmc_card_sd(card) ? 100 : 10;
282
283 /*
284 * Scale up the multiplier (and therefore the timeout) by
285 * the r2w factor for writes.
286 */
287 if (write)
288 mult <<= card->csd.r2w_factor;
289
290 data->timeout_ns = card->csd.tacc_ns * mult;
291 data->timeout_clks = card->csd.tacc_clks * mult;
292
293 /*
294 * SD cards also have an upper limit on the timeout.
295 */
296 if (mmc_card_sd(card)) {
297 unsigned int timeout_us, limit_us;
298
299 timeout_us = data->timeout_ns / 1000;
300 timeout_us += data->timeout_clks * 1000 /
301 (card->host->ios.clock / 1000);
302
303 if (write)
304 limit_us = 250000;
305 else
306 limit_us = 100000;
307
Philip Langdalefba68bd2007-01-04 06:57:32 -0800308 /*
309 * SDHC cards always use these fixed values.
310 */
311 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
Russell Kingd773d722006-09-07 15:57:12 +0100312 data->timeout_ns = limit_us * 1000;
313 data->timeout_clks = 0;
314 }
315 }
316}
317EXPORT_SYMBOL(mmc_set_data_timeout);
318
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700319static int mmc_select_card(struct mmc_host *host, struct mmc_card *card);
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321/**
322 * __mmc_claim_host - exclusively claim a host
323 * @host: mmc host to claim
324 * @card: mmc card to claim host for
325 *
326 * Claim a host for a set of operations. If a valid card
327 * is passed and this wasn't the last card selected, select
328 * the card before returning.
329 *
330 * Note: you should use mmc_card_claim_host or mmc_claim_host.
331 */
332int __mmc_claim_host(struct mmc_host *host, struct mmc_card *card)
333{
334 DECLARE_WAITQUEUE(wait, current);
335 unsigned long flags;
336 int err = 0;
337
338 add_wait_queue(&host->wq, &wait);
339 spin_lock_irqsave(&host->lock, flags);
340 while (1) {
341 set_current_state(TASK_UNINTERRUPTIBLE);
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100342 if (!host->claimed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 break;
344 spin_unlock_irqrestore(&host->lock, flags);
345 schedule();
346 spin_lock_irqsave(&host->lock, flags);
347 }
348 set_current_state(TASK_RUNNING);
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100349 host->claimed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 spin_unlock_irqrestore(&host->lock, flags);
351 remove_wait_queue(&host->wq, &wait);
352
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700353 if (card != (void *)-1) {
354 err = mmc_select_card(host, card);
355 if (err != MMC_ERR_NONE)
356 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358
359 return err;
360}
361
362EXPORT_SYMBOL(__mmc_claim_host);
363
364/**
365 * mmc_release_host - release a host
366 * @host: mmc host to release
367 *
368 * Release a MMC host, allowing others to claim the host
369 * for their operations.
370 */
371void mmc_release_host(struct mmc_host *host)
372{
373 unsigned long flags;
374
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100375 BUG_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100378 host->claimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 spin_unlock_irqrestore(&host->lock, flags);
380
381 wake_up(&host->wq);
382}
383
384EXPORT_SYMBOL(mmc_release_host);
385
Russell King920e70c2006-05-04 18:22:51 +0100386static inline void mmc_set_ios(struct mmc_host *host)
387{
388 struct mmc_ios *ios = &host->ios;
389
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100390 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
391 "width %u timing %u\n",
Russell King920e70c2006-05-04 18:22:51 +0100392 mmc_hostname(host), ios->clock, ios->bus_mode,
393 ios->power_mode, ios->chip_select, ios->vdd,
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100394 ios->bus_width, ios->timing);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800395
Russell King920e70c2006-05-04 18:22:51 +0100396 host->ops->set_ios(host, ios);
397}
398
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700399static int mmc_select_card(struct mmc_host *host, struct mmc_card *card)
400{
401 int err;
402 struct mmc_command cmd;
403
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100404 BUG_ON(!host->claimed);
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700405
406 if (host->card_selected == card)
407 return MMC_ERR_NONE;
408
409 host->card_selected = card;
410
411 cmd.opcode = MMC_SELECT_CARD;
412 cmd.arg = card->rca << 16;
Russell Kinge9225172006-02-02 12:23:12 +0000413 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700414
415 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
416 if (err != MMC_ERR_NONE)
417 return err;
418
Pierre Ossmanf2182782005-09-06 15:18:55 -0700419 /*
Philip Langdalee45a1bd2006-10-29 10:14:19 +0100420 * We can only change the bus width of SD cards when
421 * they are selected so we have to put the handling
Pierre Ossmanf2182782005-09-06 15:18:55 -0700422 * here.
Philip Langdalee45a1bd2006-10-29 10:14:19 +0100423 *
424 * The card is in 1 bit mode by default so
425 * we only need to change if it supports the
426 * wider version.
Pierre Ossmanf2182782005-09-06 15:18:55 -0700427 */
Philip Langdalee45a1bd2006-10-29 10:14:19 +0100428 if (mmc_card_sd(card) &&
429 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
430
Pierre Ossmanf2182782005-09-06 15:18:55 -0700431 /*
Philip Langdalee45a1bd2006-10-29 10:14:19 +0100432 * Default bus width is 1 bit.
433 */
434 host->ios.bus_width = MMC_BUS_WIDTH_1;
435
436 if (host->caps & MMC_CAP_4_BIT_DATA) {
Pierre Ossmanf2182782005-09-06 15:18:55 -0700437 struct mmc_command cmd;
438 cmd.opcode = SD_APP_SET_BUS_WIDTH;
439 cmd.arg = SD_BUS_WIDTH_4;
Russell Kinge9225172006-02-02 12:23:12 +0000440 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanf2182782005-09-06 15:18:55 -0700441
442 err = mmc_wait_for_app_cmd(host, card->rca, &cmd,
443 CMD_RETRIES);
444 if (err != MMC_ERR_NONE)
445 return err;
446
447 host->ios.bus_width = MMC_BUS_WIDTH_4;
448 }
449 }
450
Russell King920e70c2006-05-04 18:22:51 +0100451 mmc_set_ios(host);
Pierre Ossmanf2182782005-09-06 15:18:55 -0700452
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700453 return MMC_ERR_NONE;
454}
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456/*
457 * Ensure that no card is selected.
458 */
459static void mmc_deselect_cards(struct mmc_host *host)
460{
461 struct mmc_command cmd;
462
463 if (host->card_selected) {
464 host->card_selected = NULL;
465
466 cmd.opcode = MMC_SELECT_CARD;
467 cmd.arg = 0;
Russell Kinge9225172006-02-02 12:23:12 +0000468 cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 mmc_wait_for_cmd(host, &cmd, 0);
471 }
472}
473
474
475static inline void mmc_delay(unsigned int ms)
476{
Pierre Ossman73778122006-10-22 22:13:10 +0200477 if (ms < 1000 / HZ) {
478 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 mdelay(ms);
480 } else {
Pierre Ossman73778122006-10-22 22:13:10 +0200481 msleep(ms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483}
484
485/*
486 * Mask off any voltages we don't support and select
487 * the lowest voltage
488 */
489static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
490{
491 int bit;
492
493 ocr &= host->ocr_avail;
494
495 bit = ffs(ocr);
496 if (bit) {
497 bit -= 1;
498
Timo Teras63ef7312006-11-02 19:43:27 +0100499 ocr &= 3 << bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 host->ios.vdd = bit;
Russell King920e70c2006-05-04 18:22:51 +0100502 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 } else {
504 ocr = 0;
505 }
506
507 return ocr;
508}
509
510#define UNSTUFF_BITS(resp,start,size) \
511 ({ \
512 const int __size = size; \
513 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
514 const int __off = 3 - ((start) / 32); \
515 const int __shft = (start) & 31; \
516 u32 __res; \
517 \
518 __res = resp[__off] >> __shft; \
519 if (__size + __shft > 32) \
520 __res |= resp[__off-1] << ((32 - __shft) % 32); \
521 __res & __mask; \
522 })
523
524/*
525 * Given the decoded CSD structure, decode the raw CID to our CID structure.
526 */
527static void mmc_decode_cid(struct mmc_card *card)
528{
529 u32 *resp = card->raw_cid;
530
531 memset(&card->cid, 0, sizeof(struct mmc_cid));
532
Pierre Ossman335eadf2005-09-06 15:18:50 -0700533 if (mmc_card_sd(card)) {
534 /*
535 * SD doesn't currently have a version field so we will
536 * have to assume we can parse this.
537 */
538 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
539 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
540 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
541 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
542 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
543 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
544 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
545 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
546 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
547 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
548 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
549 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Pierre Ossman335eadf2005-09-06 15:18:50 -0700551 card->cid.year += 2000; /* SD cards year offset */
Pierre Ossmana00fc092005-09-06 15:18:52 -0700552 } else {
Pierre Ossman335eadf2005-09-06 15:18:50 -0700553 /*
554 * The selection of the format here is based upon published
555 * specs from sandisk and from what people have reported.
556 */
557 switch (card->csd.mmca_vsn) {
558 case 0: /* MMC v1.0 - v1.2 */
559 case 1: /* MMC v1.4 */
560 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
561 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
562 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
563 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
564 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
565 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
566 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
567 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
568 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
569 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
570 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
571 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
572 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
573 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Pierre Ossman335eadf2005-09-06 15:18:50 -0700575 case 2: /* MMC v2.0 - v2.2 */
576 case 3: /* MMC v3.1 - v3.3 */
Pierre Ossmancb757b42006-01-08 14:23:02 +0000577 case 4: /* MMC v4 */
Pierre Ossman335eadf2005-09-06 15:18:50 -0700578 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
579 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
580 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
581 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
582 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
583 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
584 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
585 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
586 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
587 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
588 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
589 break;
590
591 default:
592 printk("%s: card has unknown MMCA version %d\n",
593 mmc_hostname(card->host), card->csd.mmca_vsn);
594 mmc_card_set_bad(card);
595 break;
596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598}
599
600/*
601 * Given a 128-bit response, decode to our card CSD structure.
602 */
603static void mmc_decode_csd(struct mmc_card *card)
604{
605 struct mmc_csd *csd = &card->csd;
606 unsigned int e, m, csd_struct;
607 u32 *resp = card->raw_csd;
608
Pierre Ossman335eadf2005-09-06 15:18:50 -0700609 if (mmc_card_sd(card)) {
610 csd_struct = UNSTUFF_BITS(resp, 126, 2);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800611
612 switch (csd_struct) {
613 case 0:
614 m = UNSTUFF_BITS(resp, 115, 4);
615 e = UNSTUFF_BITS(resp, 112, 3);
616 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
617 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
618
619 m = UNSTUFF_BITS(resp, 99, 4);
620 e = UNSTUFF_BITS(resp, 96, 3);
621 csd->max_dtr = tran_exp[e] * tran_mant[m];
622 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
623
624 e = UNSTUFF_BITS(resp, 47, 3);
625 m = UNSTUFF_BITS(resp, 62, 12);
626 csd->capacity = (1 + m) << (e + 2);
627
628 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
629 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
630 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
631 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
632 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
633 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
634 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
635 break;
636 case 1:
637 /*
638 * This is a block-addressed SDHC card. Most
639 * interesting fields are unused and have fixed
640 * values. To avoid getting tripped by buggy cards,
641 * we assume those fixed values ourselves.
642 */
643 mmc_card_set_blockaddr(card);
644
645 csd->tacc_ns = 0; /* Unused */
646 csd->tacc_clks = 0; /* Unused */
647
648 m = UNSTUFF_BITS(resp, 99, 4);
649 e = UNSTUFF_BITS(resp, 96, 3);
650 csd->max_dtr = tran_exp[e] * tran_mant[m];
651 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
652
653 m = UNSTUFF_BITS(resp, 48, 22);
654 csd->capacity = (1 + m) << 10;
655
656 csd->read_blkbits = 9;
657 csd->read_partial = 0;
658 csd->write_misalign = 0;
659 csd->read_misalign = 0;
660 csd->r2w_factor = 4; /* Unused */
661 csd->write_blkbits = 9;
662 csd->write_partial = 0;
663 break;
664 default:
Pierre Ossman335eadf2005-09-06 15:18:50 -0700665 printk("%s: unrecognised CSD structure version %d\n",
666 mmc_hostname(card->host), csd_struct);
667 mmc_card_set_bad(card);
668 return;
669 }
Pierre Ossmana00fc092005-09-06 15:18:52 -0700670 } else {
Pierre Ossman335eadf2005-09-06 15:18:50 -0700671 /*
672 * We only understand CSD structure v1.1 and v1.2.
673 * v1.2 has extra information in bits 15, 11 and 10.
674 */
675 csd_struct = UNSTUFF_BITS(resp, 126, 2);
676 if (csd_struct != 1 && csd_struct != 2) {
677 printk("%s: unrecognised CSD structure version %d\n",
678 mmc_hostname(card->host), csd_struct);
679 mmc_card_set_bad(card);
680 return;
681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Pierre Ossman335eadf2005-09-06 15:18:50 -0700683 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
684 m = UNSTUFF_BITS(resp, 115, 4);
685 e = UNSTUFF_BITS(resp, 112, 3);
686 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
687 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Pierre Ossman335eadf2005-09-06 15:18:50 -0700689 m = UNSTUFF_BITS(resp, 99, 4);
690 e = UNSTUFF_BITS(resp, 96, 3);
691 csd->max_dtr = tran_exp[e] * tran_mant[m];
692 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Pierre Ossman335eadf2005-09-06 15:18:50 -0700694 e = UNSTUFF_BITS(resp, 47, 3);
695 m = UNSTUFF_BITS(resp, 62, 12);
696 csd->capacity = (1 + m) << (e + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Pierre Ossman335eadf2005-09-06 15:18:50 -0700698 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
Russell Kinga6f6c962006-01-03 22:38:44 +0000699 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
700 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
701 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
Russell King37be4e72006-05-02 17:24:59 +0100702 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
Russell Kinga6f6c962006-01-03 22:38:44 +0000703 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
704 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
Pierre Ossman335eadf2005-09-06 15:18:50 -0700705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
708/*
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700709 * Given a 64-bit response, decode to our card SCR structure.
710 */
711static void mmc_decode_scr(struct mmc_card *card)
712{
713 struct sd_scr *scr = &card->scr;
714 unsigned int scr_struct;
715 u32 resp[4];
716
717 BUG_ON(!mmc_card_sd(card));
718
719 resp[3] = card->raw_scr[1];
720 resp[2] = card->raw_scr[0];
721
722 scr_struct = UNSTUFF_BITS(resp, 60, 4);
723 if (scr_struct != 0) {
724 printk("%s: unrecognised SCR structure version %d\n",
725 mmc_hostname(card->host), scr_struct);
726 mmc_card_set_bad(card);
727 return;
728 }
729
730 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
731 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
732}
733
734/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 * Locate a MMC card on this MMC host given a raw CID.
736 */
737static struct mmc_card *mmc_find_card(struct mmc_host *host, u32 *raw_cid)
738{
739 struct mmc_card *card;
740
741 list_for_each_entry(card, &host->cards, node) {
742 if (memcmp(card->raw_cid, raw_cid, sizeof(card->raw_cid)) == 0)
743 return card;
744 }
745 return NULL;
746}
747
748/*
749 * Allocate a new MMC card, and assign a unique RCA.
750 */
751static struct mmc_card *
752mmc_alloc_card(struct mmc_host *host, u32 *raw_cid, unsigned int *frca)
753{
754 struct mmc_card *card, *c;
755 unsigned int rca = *frca;
756
757 card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL);
758 if (!card)
759 return ERR_PTR(-ENOMEM);
760
761 mmc_init_card(card, host);
762 memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid));
763
764 again:
765 list_for_each_entry(c, &host->cards, node)
766 if (c->rca == rca) {
767 rca++;
768 goto again;
769 }
770
771 card->rca = rca;
772
773 *frca = rca;
774
775 return card;
776}
777
778/*
779 * Tell attached cards to go to IDLE state
780 */
781static void mmc_idle_cards(struct mmc_host *host)
782{
783 struct mmc_command cmd;
784
Pierre Ossman865e9f12005-09-03 16:45:02 +0100785 host->ios.chip_select = MMC_CS_HIGH;
Russell King920e70c2006-05-04 18:22:51 +0100786 mmc_set_ios(host);
Pierre Ossman865e9f12005-09-03 16:45:02 +0100787
788 mmc_delay(1);
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 cmd.opcode = MMC_GO_IDLE_STATE;
791 cmd.arg = 0;
Russell Kinge9225172006-02-02 12:23:12 +0000792 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 mmc_wait_for_cmd(host, &cmd, 0);
795
796 mmc_delay(1);
Pierre Ossman865e9f12005-09-03 16:45:02 +0100797
798 host->ios.chip_select = MMC_CS_DONTCARE;
Russell King920e70c2006-05-04 18:22:51 +0100799 mmc_set_ios(host);
Pierre Ossman865e9f12005-09-03 16:45:02 +0100800
801 mmc_delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
804/*
Russell King45f82452005-12-14 14:57:35 +0000805 * Apply power to the MMC stack. This is a two-stage process.
806 * First, we enable power to the card without the clock running.
807 * We then wait a bit for the power to stabilise. Finally,
808 * enable the bus drivers and clock to the card.
809 *
810 * We must _NOT_ enable the clock prior to power stablising.
811 *
812 * If a host does all the power sequencing itself, ignore the
813 * initial MMC_POWER_UP stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 */
815static void mmc_power_up(struct mmc_host *host)
816{
817 int bit = fls(host->ocr_avail) - 1;
818
819 host->ios.vdd = bit;
820 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
Pierre Ossman865e9f12005-09-03 16:45:02 +0100821 host->ios.chip_select = MMC_CS_DONTCARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 host->ios.power_mode = MMC_POWER_UP;
Pierre Ossmanf2182782005-09-06 15:18:55 -0700823 host->ios.bus_width = MMC_BUS_WIDTH_1;
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100824 host->ios.timing = MMC_TIMING_LEGACY;
Russell King920e70c2006-05-04 18:22:51 +0100825 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 mmc_delay(1);
828
829 host->ios.clock = host->f_min;
830 host->ios.power_mode = MMC_POWER_ON;
Russell King920e70c2006-05-04 18:22:51 +0100831 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 mmc_delay(2);
834}
835
836static void mmc_power_off(struct mmc_host *host)
837{
838 host->ios.clock = 0;
839 host->ios.vdd = 0;
840 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
Pierre Ossman865e9f12005-09-03 16:45:02 +0100841 host->ios.chip_select = MMC_CS_DONTCARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 host->ios.power_mode = MMC_POWER_OFF;
Pierre Ossmanf2182782005-09-06 15:18:55 -0700843 host->ios.bus_width = MMC_BUS_WIDTH_1;
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100844 host->ios.timing = MMC_TIMING_LEGACY;
Russell King920e70c2006-05-04 18:22:51 +0100845 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
848static int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
849{
850 struct mmc_command cmd;
851 int i, err = 0;
852
853 cmd.opcode = MMC_SEND_OP_COND;
854 cmd.arg = ocr;
Russell Kinge9225172006-02-02 12:23:12 +0000855 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 for (i = 100; i; i--) {
858 err = mmc_wait_for_cmd(host, &cmd, 0);
859 if (err != MMC_ERR_NONE)
860 break;
861
862 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
863 break;
864
865 err = MMC_ERR_TIMEOUT;
866
867 mmc_delay(10);
868 }
869
870 if (rocr)
871 *rocr = cmd.resp[0];
872
873 return err;
874}
875
Pierre Ossman335eadf2005-09-06 15:18:50 -0700876static int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
877{
878 struct mmc_command cmd;
879 int i, err = 0;
880
881 cmd.opcode = SD_APP_OP_COND;
882 cmd.arg = ocr;
Russell Kinge9225172006-02-02 12:23:12 +0000883 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
Pierre Ossman335eadf2005-09-06 15:18:50 -0700884
885 for (i = 100; i; i--) {
886 err = mmc_wait_for_app_cmd(host, 0, &cmd, CMD_RETRIES);
887 if (err != MMC_ERR_NONE)
888 break;
889
890 if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
891 break;
892
893 err = MMC_ERR_TIMEOUT;
894
895 mmc_delay(10);
896 }
897
898 if (rocr)
899 *rocr = cmd.resp[0];
900
901 return err;
902}
903
Philip Langdalefba68bd2007-01-04 06:57:32 -0800904static int mmc_send_if_cond(struct mmc_host *host, u32 ocr, int *rsd2)
905{
906 struct mmc_command cmd;
907 int err, sd2;
908 static const u8 test_pattern = 0xAA;
909
910 /*
911 * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND
912 * before SD_APP_OP_COND. This command will harmlessly fail for
913 * SD 1.0 cards.
914 */
915 cmd.opcode = SD_SEND_IF_COND;
916 cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | test_pattern;
917 cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
918
919 err = mmc_wait_for_cmd(host, &cmd, 0);
920 if (err == MMC_ERR_NONE) {
921 if ((cmd.resp[0] & 0xFF) == test_pattern) {
922 sd2 = 1;
923 } else {
924 sd2 = 0;
925 err = MMC_ERR_FAILED;
926 }
927 } else {
928 /*
929 * Treat errors as SD 1.0 card.
930 */
931 sd2 = 0;
932 err = MMC_ERR_NONE;
933 }
934 if (rsd2)
935 *rsd2 = sd2;
936 return err;
937}
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939/*
940 * Discover cards by requesting their CID. If this command
941 * times out, it is not an error; there are no further cards
942 * to be discovered. Add new cards to the list.
943 *
944 * Create a mmc_card entry for each discovered card, assigning
945 * it an RCA, and save the raw CID for decoding later.
946 */
947static void mmc_discover_cards(struct mmc_host *host)
948{
949 struct mmc_card *card;
950 unsigned int first_rca = 1, err;
951
952 while (1) {
953 struct mmc_command cmd;
954
955 cmd.opcode = MMC_ALL_SEND_CID;
956 cmd.arg = 0;
Russell Kinge9225172006-02-02 12:23:12 +0000957 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
960 if (err == MMC_ERR_TIMEOUT) {
961 err = MMC_ERR_NONE;
962 break;
963 }
964 if (err != MMC_ERR_NONE) {
965 printk(KERN_ERR "%s: error requesting CID: %d\n",
Russell Kingd366b642005-08-19 09:40:08 +0100966 mmc_hostname(host), err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 break;
968 }
969
970 card = mmc_find_card(host, cmd.resp);
971 if (!card) {
972 card = mmc_alloc_card(host, cmd.resp, &first_rca);
973 if (IS_ERR(card)) {
974 err = PTR_ERR(card);
975 break;
976 }
977 list_add(&card->node, &host->cards);
978 }
979
980 card->state &= ~MMC_STATE_DEAD;
981
Pierre Ossman335eadf2005-09-06 15:18:50 -0700982 if (host->mode == MMC_MODE_SD) {
983 mmc_card_set_sd(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Pierre Ossman335eadf2005-09-06 15:18:50 -0700985 cmd.opcode = SD_SEND_RELATIVE_ADDR;
986 cmd.arg = 0;
Russell Kinge9225172006-02-02 12:23:12 +0000987 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
Pierre Ossman335eadf2005-09-06 15:18:50 -0700988
989 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
990 if (err != MMC_ERR_NONE)
991 mmc_card_set_dead(card);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700992 else {
Pierre Ossman335eadf2005-09-06 15:18:50 -0700993 card->rca = cmd.resp[0] >> 16;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700994
995 if (!host->ops->get_ro) {
996 printk(KERN_WARNING "%s: host does not "
997 "support reading read-only "
998 "switch. assuming write-enable.\n",
999 mmc_hostname(host));
1000 } else {
1001 if (host->ops->get_ro(host))
1002 mmc_card_set_readonly(card);
1003 }
1004 }
1005 } else {
Pierre Ossman335eadf2005-09-06 15:18:50 -07001006 cmd.opcode = MMC_SET_RELATIVE_ADDR;
1007 cmd.arg = card->rca << 16;
Russell Kinge9225172006-02-02 12:23:12 +00001008 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossman335eadf2005-09-06 15:18:50 -07001009
1010 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1011 if (err != MMC_ERR_NONE)
1012 mmc_card_set_dead(card);
1013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015}
1016
1017static void mmc_read_csds(struct mmc_host *host)
1018{
1019 struct mmc_card *card;
1020
1021 list_for_each_entry(card, &host->cards, node) {
1022 struct mmc_command cmd;
1023 int err;
1024
1025 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
1026 continue;
1027
1028 cmd.opcode = MMC_SEND_CSD;
1029 cmd.arg = card->rca << 16;
Russell Kinge9225172006-02-02 12:23:12 +00001030 cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1033 if (err != MMC_ERR_NONE) {
1034 mmc_card_set_dead(card);
1035 continue;
1036 }
1037
1038 memcpy(card->raw_csd, cmd.resp, sizeof(card->raw_csd));
1039
1040 mmc_decode_csd(card);
1041 mmc_decode_cid(card);
1042 }
1043}
1044
Philip Langdalebce40a32006-10-21 12:35:02 +02001045static void mmc_process_ext_csds(struct mmc_host *host)
1046{
1047 int err;
1048 struct mmc_card *card;
1049
1050 struct mmc_request mrq;
1051 struct mmc_command cmd;
1052 struct mmc_data data;
1053
1054 struct scatterlist sg;
1055
1056 /*
1057 * As the ext_csd is so large and mostly unused, we don't store the
1058 * raw block in mmc_card.
1059 */
1060 u8 *ext_csd;
1061 ext_csd = kmalloc(512, GFP_KERNEL);
1062 if (!ext_csd) {
1063 printk("%s: could not allocate a buffer to receive the ext_csd."
1064 "mmc v4 cards will be treated as v3.\n",
1065 mmc_hostname(host));
1066 return;
1067 }
1068
1069 list_for_each_entry(card, &host->cards, node) {
1070 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
1071 continue;
1072 if (mmc_card_sd(card))
1073 continue;
1074 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
1075 continue;
1076
1077 err = mmc_select_card(host, card);
1078 if (err != MMC_ERR_NONE) {
1079 mmc_card_set_dead(card);
1080 continue;
1081 }
1082
1083 memset(&cmd, 0, sizeof(struct mmc_command));
1084
1085 cmd.opcode = MMC_SEND_EXT_CSD;
1086 cmd.arg = 0;
1087 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1088
1089 memset(&data, 0, sizeof(struct mmc_data));
1090
1091 mmc_set_data_timeout(&data, card, 0);
1092
1093 data.blksz = 512;
1094 data.blocks = 1;
1095 data.flags = MMC_DATA_READ;
1096 data.sg = &sg;
1097 data.sg_len = 1;
1098
1099 memset(&mrq, 0, sizeof(struct mmc_request));
1100
1101 mrq.cmd = &cmd;
1102 mrq.data = &data;
1103
1104 sg_init_one(&sg, ext_csd, 512);
1105
1106 mmc_wait_for_req(host, &mrq);
1107
1108 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
Pierre Ossmanae06eaf2007-01-07 16:59:06 +01001109 printk("%s: unable to read EXT_CSD, performance "
1110 "might suffer.\n", mmc_hostname(card->host));
Philip Langdalebce40a32006-10-21 12:35:02 +02001111 continue;
1112 }
1113
1114 switch (ext_csd[EXT_CSD_CARD_TYPE]) {
1115 case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
1116 card->ext_csd.hs_max_dtr = 52000000;
1117 break;
1118 case EXT_CSD_CARD_TYPE_26:
1119 card->ext_csd.hs_max_dtr = 26000000;
1120 break;
1121 default:
1122 /* MMC v4 spec says this cannot happen */
1123 printk("%s: card is mmc v4 but doesn't support "
1124 "any high-speed modes.\n",
1125 mmc_hostname(card->host));
Philip Langdalebce40a32006-10-21 12:35:02 +02001126 continue;
1127 }
1128
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001129 if (host->caps & MMC_CAP_MMC_HIGHSPEED) {
1130 /* Activate highspeed support. */
1131 cmd.opcode = MMC_SWITCH;
1132 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1133 (EXT_CSD_HS_TIMING << 16) |
1134 (1 << 8) |
1135 EXT_CSD_CMD_SET_NORMAL;
1136 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
Philip Langdalebce40a32006-10-21 12:35:02 +02001137
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001138 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1139 if (err != MMC_ERR_NONE) {
1140 printk("%s: failed to switch card to mmc v4 "
1141 "high-speed mode.\n",
1142 mmc_hostname(card->host));
1143 continue;
1144 }
1145
1146 mmc_card_set_highspeed(card);
1147
1148 host->ios.timing = MMC_TIMING_SD_HS;
1149 mmc_set_ios(host);
Philip Langdalebce40a32006-10-21 12:35:02 +02001150 }
1151
Philip Langdalee45a1bd2006-10-29 10:14:19 +01001152 /* Check for host support for wide-bus modes. */
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001153 if (host->caps & MMC_CAP_4_BIT_DATA) {
1154 /* Activate 4-bit support. */
1155 cmd.opcode = MMC_SWITCH;
1156 cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1157 (EXT_CSD_BUS_WIDTH << 16) |
1158 (EXT_CSD_BUS_WIDTH_4 << 8) |
1159 EXT_CSD_CMD_SET_NORMAL;
1160 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1161
1162 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1163 if (err != MMC_ERR_NONE) {
1164 printk("%s: failed to switch card to "
1165 "mmc v4 4-bit bus mode.\n",
1166 mmc_hostname(card->host));
1167 continue;
1168 }
1169
1170 host->ios.bus_width = MMC_BUS_WIDTH_4;
1171 mmc_set_ios(host);
Philip Langdalee45a1bd2006-10-29 10:14:19 +01001172 }
Philip Langdalebce40a32006-10-21 12:35:02 +02001173 }
1174
1175 kfree(ext_csd);
1176
1177 mmc_deselect_cards(host);
1178}
1179
Pierre Ossmanb57c43a2005-09-06 15:18:53 -07001180static void mmc_read_scrs(struct mmc_host *host)
1181{
1182 int err;
1183 struct mmc_card *card;
Pierre Ossmanb57c43a2005-09-06 15:18:53 -07001184 struct mmc_request mrq;
1185 struct mmc_command cmd;
1186 struct mmc_data data;
Pierre Ossmanb57c43a2005-09-06 15:18:53 -07001187 struct scatterlist sg;
1188
1189 list_for_each_entry(card, &host->cards, node) {
1190 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
1191 continue;
1192 if (!mmc_card_sd(card))
1193 continue;
1194
1195 err = mmc_select_card(host, card);
1196 if (err != MMC_ERR_NONE) {
1197 mmc_card_set_dead(card);
1198 continue;
1199 }
1200
1201 memset(&cmd, 0, sizeof(struct mmc_command));
1202
1203 cmd.opcode = MMC_APP_CMD;
1204 cmd.arg = card->rca << 16;
Russell Kinge9225172006-02-02 12:23:12 +00001205 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanb57c43a2005-09-06 15:18:53 -07001206
1207 err = mmc_wait_for_cmd(host, &cmd, 0);
1208 if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD)) {
1209 mmc_card_set_dead(card);
1210 continue;
1211 }
1212
1213 memset(&cmd, 0, sizeof(struct mmc_command));
1214
1215 cmd.opcode = SD_APP_SEND_SCR;
1216 cmd.arg = 0;
Russell Kinge9225172006-02-02 12:23:12 +00001217 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanb57c43a2005-09-06 15:18:53 -07001218
1219 memset(&data, 0, sizeof(struct mmc_data));
1220
Russell Kingd773d722006-09-07 15:57:12 +01001221 mmc_set_data_timeout(&data, card, 0);
Pierre Ossman385e3222006-06-18 14:34:37 +02001222
Pavel Pisa2c171bf2006-05-19 21:48:03 +01001223 data.blksz = 1 << 3;
Pierre Ossmanb57c43a2005-09-06 15:18:53 -07001224 data.blocks = 1;
1225 data.flags = MMC_DATA_READ;
1226 data.sg = &sg;
1227 data.sg_len = 1;
1228
1229 memset(&mrq, 0, sizeof(struct mmc_request));
1230
1231 mrq.cmd = &cmd;
1232 mrq.data = &data;
1233
1234 sg_init_one(&sg, (u8*)card->raw_scr, 8);
1235
Pierre Ossmane781de42005-12-05 10:00:50 +00001236 mmc_wait_for_req(host, &mrq);
1237
1238 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
Pierre Ossmanb57c43a2005-09-06 15:18:53 -07001239 mmc_card_set_dead(card);
1240 continue;
1241 }
1242
1243 card->raw_scr[0] = ntohl(card->raw_scr[0]);
1244 card->raw_scr[1] = ntohl(card->raw_scr[1]);
1245
1246 mmc_decode_scr(card);
1247 }
1248
1249 mmc_deselect_cards(host);
1250}
1251
Pierre Ossman7ccd2662006-11-08 23:03:10 +01001252static void mmc_read_switch_caps(struct mmc_host *host)
1253{
1254 int err;
1255 struct mmc_card *card;
1256 struct mmc_request mrq;
1257 struct mmc_command cmd;
1258 struct mmc_data data;
1259 unsigned char *status;
1260 struct scatterlist sg;
1261
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001262 if (!(host->caps & MMC_CAP_SD_HIGHSPEED))
1263 return;
1264
Pierre Ossman7ccd2662006-11-08 23:03:10 +01001265 status = kmalloc(64, GFP_KERNEL);
1266 if (!status) {
1267 printk(KERN_WARNING "%s: Unable to allocate buffer for "
1268 "reading switch capabilities.\n",
1269 mmc_hostname(host));
1270 return;
1271 }
1272
1273 list_for_each_entry(card, &host->cards, node) {
1274 if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
1275 continue;
1276 if (!mmc_card_sd(card))
1277 continue;
1278 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
1279 continue;
1280
1281 err = mmc_select_card(host, card);
1282 if (err != MMC_ERR_NONE) {
1283 mmc_card_set_dead(card);
1284 continue;
1285 }
1286
1287 memset(&cmd, 0, sizeof(struct mmc_command));
1288
1289 cmd.opcode = SD_SWITCH;
1290 cmd.arg = 0x00FFFFF1;
1291 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1292
1293 memset(&data, 0, sizeof(struct mmc_data));
1294
1295 mmc_set_data_timeout(&data, card, 0);
1296
1297 data.blksz = 64;
1298 data.blocks = 1;
1299 data.flags = MMC_DATA_READ;
1300 data.sg = &sg;
1301 data.sg_len = 1;
1302
1303 memset(&mrq, 0, sizeof(struct mmc_request));
1304
1305 mrq.cmd = &cmd;
1306 mrq.data = &data;
1307
1308 sg_init_one(&sg, status, 64);
1309
1310 mmc_wait_for_req(host, &mrq);
1311
1312 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
Pierre Ossmanae06eaf2007-01-07 16:59:06 +01001313 printk("%s: unable to read switch capabilities, "
1314 "performance might suffer.\n",
1315 mmc_hostname(card->host));
Pierre Ossman7ccd2662006-11-08 23:03:10 +01001316 continue;
1317 }
1318
1319 if (status[13] & 0x02)
1320 card->sw_caps.hs_max_dtr = 50000000;
1321
1322 memset(&cmd, 0, sizeof(struct mmc_command));
1323
1324 cmd.opcode = SD_SWITCH;
1325 cmd.arg = 0x80FFFFF1;
1326 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1327
1328 memset(&data, 0, sizeof(struct mmc_data));
1329
1330 mmc_set_data_timeout(&data, card, 0);
1331
1332 data.blksz = 64;
1333 data.blocks = 1;
1334 data.flags = MMC_DATA_READ;
1335 data.sg = &sg;
1336 data.sg_len = 1;
1337
1338 memset(&mrq, 0, sizeof(struct mmc_request));
1339
1340 mrq.cmd = &cmd;
1341 mrq.data = &data;
1342
1343 sg_init_one(&sg, status, 64);
1344
1345 mmc_wait_for_req(host, &mrq);
1346
Pierre Ossmanae06eaf2007-01-07 16:59:06 +01001347 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE ||
1348 (status[16] & 0xF) != 1) {
Pierre Ossman7ccd2662006-11-08 23:03:10 +01001349 printk(KERN_WARNING "%s: Problem switching card "
1350 "into high-speed mode!\n",
1351 mmc_hostname(host));
1352 continue;
1353 }
1354
1355 mmc_card_set_highspeed(card);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001356
1357 host->ios.timing = MMC_TIMING_SD_HS;
1358 mmc_set_ios(host);
Pierre Ossman7ccd2662006-11-08 23:03:10 +01001359 }
1360
1361 kfree(status);
1362
1363 mmc_deselect_cards(host);
1364}
1365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366static unsigned int mmc_calculate_clock(struct mmc_host *host)
1367{
1368 struct mmc_card *card;
1369 unsigned int max_dtr = host->f_max;
1370
1371 list_for_each_entry(card, &host->cards, node)
Philip Langdalebce40a32006-10-21 12:35:02 +02001372 if (!mmc_card_dead(card)) {
Pierre Ossman7ccd2662006-11-08 23:03:10 +01001373 if (mmc_card_highspeed(card) && mmc_card_sd(card)) {
1374 if (max_dtr > card->sw_caps.hs_max_dtr)
1375 max_dtr = card->sw_caps.hs_max_dtr;
1376 } else if (mmc_card_highspeed(card) && !mmc_card_sd(card)) {
Philip Langdalebce40a32006-10-21 12:35:02 +02001377 if (max_dtr > card->ext_csd.hs_max_dtr)
Pierre Ossman7ccd2662006-11-08 23:03:10 +01001378 max_dtr = card->ext_csd.hs_max_dtr;
Philip Langdalebce40a32006-10-21 12:35:02 +02001379 } else if (max_dtr > card->csd.max_dtr) {
1380 max_dtr = card->csd.max_dtr;
1381 }
1382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Russell King920e70c2006-05-04 18:22:51 +01001384 pr_debug("%s: selected %d.%03dMHz transfer rate\n",
1385 mmc_hostname(host),
Russell Kingc6563172006-03-29 09:30:20 +01001386 max_dtr / 1000000, (max_dtr / 1000) % 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 return max_dtr;
1389}
1390
1391/*
1392 * Check whether cards we already know about are still present.
1393 * We do this by requesting status, and checking whether a card
1394 * responds.
1395 *
1396 * A request for status does not cause a state change in data
1397 * transfer mode.
1398 */
1399static void mmc_check_cards(struct mmc_host *host)
1400{
1401 struct list_head *l, *n;
1402
1403 mmc_deselect_cards(host);
1404
1405 list_for_each_safe(l, n, &host->cards) {
1406 struct mmc_card *card = mmc_list_to_card(l);
1407 struct mmc_command cmd;
1408 int err;
1409
1410 cmd.opcode = MMC_SEND_STATUS;
1411 cmd.arg = card->rca << 16;
Russell Kinge9225172006-02-02 12:23:12 +00001412 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
1415 if (err == MMC_ERR_NONE)
1416 continue;
1417
1418 mmc_card_set_dead(card);
1419 }
1420}
1421
1422static void mmc_setup(struct mmc_host *host)
1423{
1424 if (host->ios.power_mode != MMC_POWER_ON) {
1425 int err;
1426 u32 ocr;
1427
Pierre Ossmana00fc092005-09-06 15:18:52 -07001428 host->mode = MMC_MODE_SD;
Pierre Ossman335eadf2005-09-06 15:18:50 -07001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 mmc_power_up(host);
1431 mmc_idle_cards(host);
1432
Philip Langdalefba68bd2007-01-04 06:57:32 -08001433 err = mmc_send_if_cond(host, host->ocr_avail, NULL);
1434 if (err != MMC_ERR_NONE) {
1435 return;
1436 }
Pierre Ossmana00fc092005-09-06 15:18:52 -07001437 err = mmc_send_app_op_cond(host, 0, &ocr);
Pierre Ossman335eadf2005-09-06 15:18:50 -07001438
1439 /*
Pierre Ossmana00fc092005-09-06 15:18:52 -07001440 * If we fail to detect any SD cards then try
1441 * searching for MMC cards.
Pierre Ossman335eadf2005-09-06 15:18:50 -07001442 */
Pierre Ossmana00fc092005-09-06 15:18:52 -07001443 if (err != MMC_ERR_NONE) {
1444 host->mode = MMC_MODE_MMC;
1445
1446 err = mmc_send_op_cond(host, 0, &ocr);
Pierre Ossman335eadf2005-09-06 15:18:50 -07001447 if (err != MMC_ERR_NONE)
1448 return;
Pierre Ossman335eadf2005-09-06 15:18:50 -07001449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 host->ocr = mmc_select_voltage(host, ocr);
1452
1453 /*
1454 * Since we're changing the OCR value, we seem to
1455 * need to tell some cards to go back to the idle
1456 * state. We wait 1ms to give cards time to
1457 * respond.
1458 */
1459 if (host->ocr)
1460 mmc_idle_cards(host);
1461 } else {
1462 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1463 host->ios.clock = host->f_min;
Russell King920e70c2006-05-04 18:22:51 +01001464 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 /*
1467 * We should remember the OCR mask from the existing
1468 * cards, and detect the new cards OCR mask, combine
1469 * the two and re-select the VDD. However, if we do
1470 * change VDD, we should do an idle, and then do a
1471 * full re-initialisation. We would need to notify
1472 * drivers so that they can re-setup the cards as
1473 * well, while keeping their queues at bay.
1474 *
1475 * For the moment, we take the easy way out - if the
1476 * new cards don't like our currently selected VDD,
1477 * they drop off the bus.
1478 */
1479 }
1480
1481 if (host->ocr == 0)
1482 return;
1483
1484 /*
1485 * Send the selected OCR multiple times... until the cards
1486 * all get the idea that they should be ready for CMD2.
1487 * (My SanDisk card seems to need this.)
1488 */
Philip Langdalefba68bd2007-01-04 06:57:32 -08001489 if (host->mode == MMC_MODE_SD) {
1490 int err, sd2;
1491 err = mmc_send_if_cond(host, host->ocr, &sd2);
1492 if (err == MMC_ERR_NONE) {
1493 /*
1494 * If SD_SEND_IF_COND indicates an SD 2.0
1495 * compliant card and we should set bit 30
1496 * of the ocr to indicate that we can handle
1497 * block-addressed SDHC cards.
1498 */
1499 mmc_send_app_op_cond(host, host->ocr | (sd2 << 30), NULL);
1500 }
1501 } else {
Pierre Ossman335eadf2005-09-06 15:18:50 -07001502 mmc_send_op_cond(host, host->ocr, NULL);
Philip Langdalefba68bd2007-01-04 06:57:32 -08001503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 mmc_discover_cards(host);
1506
1507 /*
1508 * Ok, now switch to push-pull mode.
1509 */
1510 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
Russell King920e70c2006-05-04 18:22:51 +01001511 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 mmc_read_csds(host);
Pierre Ossmanb57c43a2005-09-06 15:18:53 -07001514
Pierre Ossman7ccd2662006-11-08 23:03:10 +01001515 if (host->mode == MMC_MODE_SD) {
Pierre Ossmanb57c43a2005-09-06 15:18:53 -07001516 mmc_read_scrs(host);
Pierre Ossman7ccd2662006-11-08 23:03:10 +01001517 mmc_read_switch_caps(host);
1518 } else
Philip Langdalebce40a32006-10-21 12:35:02 +02001519 mmc_process_ext_csds(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
1521
1522
1523/**
1524 * mmc_detect_change - process change of state on a MMC socket
1525 * @host: host which changed state.
Richard Purdie8dc00332005-09-08 17:53:01 +01001526 * @delay: optional delay to wait before detection (jiffies)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 *
1528 * All we know is that card(s) have been inserted or removed
1529 * from the socket(s). We don't know which socket or cards.
1530 */
Richard Purdie8dc00332005-09-08 17:53:01 +01001531void mmc_detect_change(struct mmc_host *host, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
David Howellsc4028952006-11-22 14:57:56 +00001533 mmc_schedule_delayed_work(&host->detect, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
1536EXPORT_SYMBOL(mmc_detect_change);
1537
1538
David Howellsc4028952006-11-22 14:57:56 +00001539static void mmc_rescan(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
David Howellsc4028952006-11-22 14:57:56 +00001541 struct mmc_host *host =
1542 container_of(work, struct mmc_host, detect.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 struct list_head *l, *n;
Timo Teras25a122f2006-10-25 09:37:41 +03001544 unsigned char power_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546 mmc_claim_host(host);
1547
Timo Teras25a122f2006-10-25 09:37:41 +03001548 /*
1549 * Check for removed cards and newly inserted ones. We check for
1550 * removed cards first so we can intelligently re-select the VDD.
1551 */
1552 power_mode = host->ios.power_mode;
1553 if (power_mode == MMC_POWER_ON)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 mmc_check_cards(host);
1555
1556 mmc_setup(host);
1557
Timo Teras25a122f2006-10-25 09:37:41 +03001558 /*
1559 * Some broken cards process CMD1 even in stand-by state. There is
1560 * no reply, but an ILLEGAL_COMMAND error is cached and returned
1561 * after next command. We poll for card status here to clear any
1562 * possibly pending error.
1563 */
1564 if (power_mode == MMC_POWER_ON)
1565 mmc_check_cards(host);
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 if (!list_empty(&host->cards)) {
1568 /*
1569 * (Re-)calculate the fastest clock rate which the
1570 * attached cards and the host support.
1571 */
1572 host->ios.clock = mmc_calculate_clock(host);
Russell King920e70c2006-05-04 18:22:51 +01001573 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
1575
1576 mmc_release_host(host);
1577
1578 list_for_each_safe(l, n, &host->cards) {
1579 struct mmc_card *card = mmc_list_to_card(l);
1580
1581 /*
1582 * If this is a new and good card, register it.
1583 */
1584 if (!mmc_card_present(card) && !mmc_card_dead(card)) {
1585 if (mmc_register_card(card))
1586 mmc_card_set_dead(card);
1587 else
1588 mmc_card_set_present(card);
1589 }
1590
1591 /*
1592 * If this card is dead, destroy it.
1593 */
1594 if (mmc_card_dead(card)) {
1595 list_del(&card->node);
1596 mmc_remove_card(card);
1597 }
1598 }
1599
1600 /*
1601 * If we discover that there are no cards on the
1602 * bus, turn off the clock and power down.
1603 */
1604 if (list_empty(&host->cards))
1605 mmc_power_off(host);
1606}
1607
1608
1609/**
1610 * mmc_alloc_host - initialise the per-host structure.
1611 * @extra: sizeof private data structure
1612 * @dev: pointer to host device model structure
1613 *
1614 * Initialise the per-host structure.
1615 */
1616struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
1617{
1618 struct mmc_host *host;
1619
Russell King00b137c2005-08-19 09:41:24 +01001620 host = mmc_alloc_host_sysfs(extra, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 if (host) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 spin_lock_init(&host->lock);
1623 init_waitqueue_head(&host->wq);
1624 INIT_LIST_HEAD(&host->cards);
David Howellsc4028952006-11-22 14:57:56 +00001625 INIT_DELAYED_WORK(&host->detect, mmc_rescan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 /*
1628 * By default, hosts do not support SGIO or large requests.
1629 * They have to set these according to their abilities.
1630 */
1631 host->max_hw_segs = 1;
1632 host->max_phys_segs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 host->max_seg_size = PAGE_CACHE_SIZE;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001634
Pierre Ossman55db8902006-11-21 17:55:45 +01001635 host->max_req_size = PAGE_CACHE_SIZE;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001636 host->max_blk_size = 512;
Pierre Ossman55db8902006-11-21 17:55:45 +01001637 host->max_blk_count = PAGE_CACHE_SIZE / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
1639
1640 return host;
1641}
1642
1643EXPORT_SYMBOL(mmc_alloc_host);
1644
1645/**
1646 * mmc_add_host - initialise host hardware
1647 * @host: mmc host
1648 */
1649int mmc_add_host(struct mmc_host *host)
1650{
Russell King00b137c2005-08-19 09:41:24 +01001651 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Russell King00b137c2005-08-19 09:41:24 +01001653 ret = mmc_add_host_sysfs(host);
1654 if (ret == 0) {
1655 mmc_power_off(host);
Richard Purdie8dc00332005-09-08 17:53:01 +01001656 mmc_detect_change(host, 0);
Russell King00b137c2005-08-19 09:41:24 +01001657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Russell King00b137c2005-08-19 09:41:24 +01001659 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660}
1661
1662EXPORT_SYMBOL(mmc_add_host);
1663
1664/**
1665 * mmc_remove_host - remove host hardware
1666 * @host: mmc host
1667 *
1668 * Unregister and remove all cards associated with this host,
1669 * and power down the MMC bus.
1670 */
1671void mmc_remove_host(struct mmc_host *host)
1672{
1673 struct list_head *l, *n;
1674
1675 list_for_each_safe(l, n, &host->cards) {
1676 struct mmc_card *card = mmc_list_to_card(l);
1677
1678 mmc_remove_card(card);
1679 }
1680
1681 mmc_power_off(host);
Russell King00b137c2005-08-19 09:41:24 +01001682 mmc_remove_host_sysfs(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683}
1684
1685EXPORT_SYMBOL(mmc_remove_host);
1686
1687/**
1688 * mmc_free_host - free the host structure
1689 * @host: mmc host
1690 *
1691 * Free the host once all references to it have been dropped.
1692 */
1693void mmc_free_host(struct mmc_host *host)
1694{
Pierre Ossman7104e2d2006-10-04 02:15:41 -07001695 mmc_flush_scheduled_work();
Russell King00b137c2005-08-19 09:41:24 +01001696 mmc_free_host_sysfs(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
1698
1699EXPORT_SYMBOL(mmc_free_host);
1700
1701#ifdef CONFIG_PM
1702
1703/**
1704 * mmc_suspend_host - suspend a host
1705 * @host: mmc host
1706 * @state: suspend mode (PM_SUSPEND_xxx)
1707 */
Pavel Macheke5378ca2005-04-16 15:25:29 -07001708int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
1710 mmc_claim_host(host);
1711 mmc_deselect_cards(host);
1712 mmc_power_off(host);
1713 mmc_release_host(host);
1714
1715 return 0;
1716}
1717
1718EXPORT_SYMBOL(mmc_suspend_host);
1719
1720/**
1721 * mmc_resume_host - resume a previously suspended host
1722 * @host: mmc host
1723 */
1724int mmc_resume_host(struct mmc_host *host)
1725{
David Howellsc4028952006-11-22 14:57:56 +00001726 mmc_rescan(&host->detect.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728 return 0;
1729}
1730
1731EXPORT_SYMBOL(mmc_resume_host);
1732
1733#endif
1734
1735MODULE_LICENSE("GPL");