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