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