MMC/SD card driver learns SPI
[linux-block.git] / drivers / mmc / core / core.c
CommitLineData
1da177e4 1/*
aaac1b47 2 * linux/drivers/mmc/core/core.c
1da177e4
LT
3 *
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5b4fd9ae 5 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
b855885e 6 * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
bce40a36 7 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
1da177e4
LT
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 */
1da177e4
LT
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>
b57c43ad
PO
21#include <asm/scatterlist.h>
22#include <linux/scatterlist.h>
1da177e4
LT
23
24#include <linux/mmc/card.h>
25#include <linux/mmc/host.h>
da7fbe58
PO
26#include <linux/mmc/mmc.h>
27#include <linux/mmc/sd.h>
1da177e4 28
aaac1b47 29#include "core.h"
ffce2e7e
PO
30#include "bus.h"
31#include "host.h"
e29a7d73 32#include "sdio_bus.h"
da7fbe58
PO
33
34#include "mmc_ops.h"
35#include "sd_ops.h"
5c4e6f13 36#include "sdio_ops.h"
1da177e4 37
7ea239d9
PO
38extern int mmc_attach_mmc(struct mmc_host *host, u32 ocr);
39extern int mmc_attach_sd(struct mmc_host *host, u32 ocr);
5c4e6f13 40extern int mmc_attach_sdio(struct mmc_host *host, u32 ocr);
1da177e4 41
ffce2e7e
PO
42static struct workqueue_struct *workqueue;
43
44/*
45 * Internal function. Schedule delayed work in the MMC work queue.
46 */
47static int mmc_schedule_delayed_work(struct delayed_work *work,
48 unsigned long delay)
49{
50 return queue_delayed_work(workqueue, work, delay);
51}
52
53/*
54 * Internal function. Flush all scheduled work from the MMC work queue.
55 */
56static void mmc_flush_scheduled_work(void)
57{
58 flush_workqueue(workqueue);
59}
60
1da177e4 61/**
fe10c6ab
RK
62 * mmc_request_done - finish processing an MMC request
63 * @host: MMC host which completed request
64 * @mrq: MMC request which request
1da177e4
LT
65 *
66 * MMC drivers should call this function when they have completed
fe10c6ab 67 * their processing of a request.
1da177e4
LT
68 */
69void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
70{
71 struct mmc_command *cmd = mrq->cmd;
920e70c5
RK
72 int err = cmd->error;
73
1da177e4 74 if (err && cmd->retries) {
e4d21708
PO
75 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
76 mmc_hostname(host), cmd->opcode, err);
77
1da177e4
LT
78 cmd->retries--;
79 cmd->error = 0;
80 host->ops->request(host, mrq);
e4d21708
PO
81 } else {
82 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
83 mmc_hostname(host), cmd->opcode, err,
84 cmd->resp[0], cmd->resp[1],
85 cmd->resp[2], cmd->resp[3]);
86
87 if (mrq->data) {
88 pr_debug("%s: %d bytes transferred: %d\n",
89 mmc_hostname(host),
90 mrq->data->bytes_xfered, mrq->data->error);
91 }
92
93 if (mrq->stop) {
94 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
95 mmc_hostname(host), mrq->stop->opcode,
96 mrq->stop->error,
97 mrq->stop->resp[0], mrq->stop->resp[1],
98 mrq->stop->resp[2], mrq->stop->resp[3]);
99 }
100
101 if (mrq->done)
102 mrq->done(mrq);
1da177e4
LT
103 }
104}
105
106EXPORT_SYMBOL(mmc_request_done);
107
39361851 108static void
1da177e4
LT
109mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
110{
976d9276
PO
111#ifdef CONFIG_MMC_DEBUG
112 unsigned int i, sz;
113#endif
114
920e70c5
RK
115 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
116 mmc_hostname(host), mrq->cmd->opcode,
117 mrq->cmd->arg, mrq->cmd->flags);
1da177e4 118
e4d21708
PO
119 if (mrq->data) {
120 pr_debug("%s: blksz %d blocks %d flags %08x "
121 "tsac %d ms nsac %d\n",
122 mmc_hostname(host), mrq->data->blksz,
123 mrq->data->blocks, mrq->data->flags,
ce252edd 124 mrq->data->timeout_ns / 1000000,
e4d21708
PO
125 mrq->data->timeout_clks);
126 }
127
128 if (mrq->stop) {
129 pr_debug("%s: CMD%u arg %08x flags %08x\n",
130 mmc_hostname(host), mrq->stop->opcode,
131 mrq->stop->arg, mrq->stop->flags);
132 }
133
f22ee4ed 134 WARN_ON(!host->claimed);
1da177e4
LT
135
136 mrq->cmd->error = 0;
137 mrq->cmd->mrq = mrq;
138 if (mrq->data) {
fe4a3c7a 139 BUG_ON(mrq->data->blksz > host->max_blk_size);
55db890a
PO
140 BUG_ON(mrq->data->blocks > host->max_blk_count);
141 BUG_ON(mrq->data->blocks * mrq->data->blksz >
142 host->max_req_size);
fe4a3c7a 143
976d9276
PO
144#ifdef CONFIG_MMC_DEBUG
145 sz = 0;
146 for (i = 0;i < mrq->data->sg_len;i++)
147 sz += mrq->data->sg[i].length;
148 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
149#endif
150
1da177e4
LT
151 mrq->cmd->data = mrq->data;
152 mrq->data->error = 0;
153 mrq->data->mrq = mrq;
154 if (mrq->stop) {
155 mrq->data->stop = mrq->stop;
156 mrq->stop->error = 0;
157 mrq->stop->mrq = mrq;
158 }
159 }
160 host->ops->request(host, mrq);
161}
162
1da177e4
LT
163static void mmc_wait_done(struct mmc_request *mrq)
164{
165 complete(mrq->done_data);
166}
167
67a61c48
PO
168/**
169 * mmc_wait_for_req - start a request and wait for completion
170 * @host: MMC host to start command
171 * @mrq: MMC request to start
172 *
173 * Start a new MMC custom command request for a host, and wait
174 * for the command to complete. Does not attempt to parse the
175 * response.
176 */
177void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
1da177e4 178{
0afffc72 179 DECLARE_COMPLETION_ONSTACK(complete);
1da177e4
LT
180
181 mrq->done_data = &complete;
182 mrq->done = mmc_wait_done;
183
184 mmc_start_request(host, mrq);
185
186 wait_for_completion(&complete);
1da177e4
LT
187}
188
189EXPORT_SYMBOL(mmc_wait_for_req);
190
191/**
192 * mmc_wait_for_cmd - start a command and wait for completion
193 * @host: MMC host to start command
194 * @cmd: MMC command to start
195 * @retries: maximum number of retries
196 *
197 * Start a new MMC command for a host, and wait for the command
198 * to complete. Return any error that occurred while the command
199 * was executing. Do not attempt to parse the response.
200 */
201int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
202{
203 struct mmc_request mrq;
204
d84075c8 205 WARN_ON(!host->claimed);
1da177e4
LT
206
207 memset(&mrq, 0, sizeof(struct mmc_request));
208
209 memset(cmd->resp, 0, sizeof(cmd->resp));
210 cmd->retries = retries;
211
212 mrq.cmd = cmd;
213 cmd->data = NULL;
214
215 mmc_wait_for_req(host, &mrq);
216
217 return cmd->error;
218}
219
220EXPORT_SYMBOL(mmc_wait_for_cmd);
221
d773d725
RK
222/**
223 * mmc_set_data_timeout - set the timeout for a data command
224 * @data: data phase for command
225 * @card: the MMC card associated with the data transfer
67a61c48
PO
226 *
227 * Computes the data timeout parameters according to the
228 * correct algorithm given the card type.
d773d725 229 */
b146d26a 230void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
d773d725
RK
231{
232 unsigned int mult;
233
e6f918bf
PO
234 /*
235 * SDIO cards only define an upper 1 s limit on access.
236 */
237 if (mmc_card_sdio(card)) {
238 data->timeout_ns = 1000000000;
239 data->timeout_clks = 0;
240 return;
241 }
242
d773d725
RK
243 /*
244 * SD cards use a 100 multiplier rather than 10
245 */
246 mult = mmc_card_sd(card) ? 100 : 10;
247
248 /*
249 * Scale up the multiplier (and therefore the timeout) by
250 * the r2w factor for writes.
251 */
b146d26a 252 if (data->flags & MMC_DATA_WRITE)
d773d725
RK
253 mult <<= card->csd.r2w_factor;
254
255 data->timeout_ns = card->csd.tacc_ns * mult;
256 data->timeout_clks = card->csd.tacc_clks * mult;
257
258 /*
259 * SD cards also have an upper limit on the timeout.
260 */
261 if (mmc_card_sd(card)) {
262 unsigned int timeout_us, limit_us;
263
264 timeout_us = data->timeout_ns / 1000;
265 timeout_us += data->timeout_clks * 1000 /
266 (card->host->ios.clock / 1000);
267
b146d26a 268 if (data->flags & MMC_DATA_WRITE)
d773d725
RK
269 limit_us = 250000;
270 else
271 limit_us = 100000;
272
fba68bd2
PL
273 /*
274 * SDHC cards always use these fixed values.
275 */
276 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
d773d725
RK
277 data->timeout_ns = limit_us * 1000;
278 data->timeout_clks = 0;
279 }
280 }
281}
282EXPORT_SYMBOL(mmc_set_data_timeout);
283
1da177e4 284/**
2342f332 285 * __mmc_claim_host - exclusively claim a host
1da177e4 286 * @host: mmc host to claim
2342f332 287 * @abort: whether or not the operation should be aborted
1da177e4 288 *
2342f332
NP
289 * Claim a host for a set of operations. If @abort is non null and
290 * dereference a non-zero value then this will return prematurely with
291 * that non-zero value without acquiring the lock. Returns zero
292 * with the lock held otherwise.
1da177e4 293 */
2342f332 294int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
1da177e4
LT
295{
296 DECLARE_WAITQUEUE(wait, current);
297 unsigned long flags;
2342f332 298 int stop;
1da177e4 299
cf795bfb
PO
300 might_sleep();
301
1da177e4
LT
302 add_wait_queue(&host->wq, &wait);
303 spin_lock_irqsave(&host->lock, flags);
304 while (1) {
305 set_current_state(TASK_UNINTERRUPTIBLE);
2342f332
NP
306 stop = abort ? atomic_read(abort) : 0;
307 if (stop || !host->claimed)
1da177e4
LT
308 break;
309 spin_unlock_irqrestore(&host->lock, flags);
310 schedule();
311 spin_lock_irqsave(&host->lock, flags);
312 }
313 set_current_state(TASK_RUNNING);
2342f332
NP
314 if (!stop)
315 host->claimed = 1;
316 else
317 wake_up(&host->wq);
1da177e4
LT
318 spin_unlock_irqrestore(&host->lock, flags);
319 remove_wait_queue(&host->wq, &wait);
2342f332 320 return stop;
1da177e4
LT
321}
322
2342f332 323EXPORT_SYMBOL(__mmc_claim_host);
1da177e4
LT
324
325/**
326 * mmc_release_host - release a host
327 * @host: mmc host to release
328 *
329 * Release a MMC host, allowing others to claim the host
330 * for their operations.
331 */
332void mmc_release_host(struct mmc_host *host)
333{
334 unsigned long flags;
335
d84075c8 336 WARN_ON(!host->claimed);
1da177e4
LT
337
338 spin_lock_irqsave(&host->lock, flags);
f22ee4ed 339 host->claimed = 0;
1da177e4
LT
340 spin_unlock_irqrestore(&host->lock, flags);
341
342 wake_up(&host->wq);
343}
344
345EXPORT_SYMBOL(mmc_release_host);
346
7ea239d9
PO
347/*
348 * Internal function that does the actual ios call to the host driver,
349 * optionally printing some debug output.
350 */
920e70c5
RK
351static inline void mmc_set_ios(struct mmc_host *host)
352{
353 struct mmc_ios *ios = &host->ios;
354
cd9277c0
PO
355 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
356 "width %u timing %u\n",
920e70c5
RK
357 mmc_hostname(host), ios->clock, ios->bus_mode,
358 ios->power_mode, ios->chip_select, ios->vdd,
cd9277c0 359 ios->bus_width, ios->timing);
fba68bd2 360
920e70c5
RK
361 host->ops->set_ios(host, ios);
362}
363
7ea239d9
PO
364/*
365 * Control chip select pin on a host.
366 */
da7fbe58 367void mmc_set_chip_select(struct mmc_host *host, int mode)
1da177e4 368{
da7fbe58
PO
369 host->ios.chip_select = mode;
370 mmc_set_ios(host);
1da177e4
LT
371}
372
7ea239d9
PO
373/*
374 * Sets the host clock to the highest possible frequency that
375 * is below "hz".
376 */
377void mmc_set_clock(struct mmc_host *host, unsigned int hz)
378{
379 WARN_ON(hz < host->f_min);
380
381 if (hz > host->f_max)
382 hz = host->f_max;
383
384 host->ios.clock = hz;
385 mmc_set_ios(host);
386}
387
388/*
389 * Change the bus mode (open drain/push-pull) of a host.
390 */
391void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
392{
393 host->ios.bus_mode = mode;
394 mmc_set_ios(host);
395}
396
397/*
398 * Change data bus width of a host.
399 */
400void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
401{
402 host->ios.bus_width = width;
403 mmc_set_ios(host);
404}
405
1da177e4
LT
406/*
407 * Mask off any voltages we don't support and select
408 * the lowest voltage
409 */
7ea239d9 410u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
1da177e4
LT
411{
412 int bit;
413
414 ocr &= host->ocr_avail;
415
416 bit = ffs(ocr);
417 if (bit) {
418 bit -= 1;
419
63ef731a 420 ocr &= 3 << bit;
1da177e4
LT
421
422 host->ios.vdd = bit;
920e70c5 423 mmc_set_ios(host);
1da177e4
LT
424 } else {
425 ocr = 0;
426 }
427
428 return ocr;
429}
430
b57c43ad 431/*
7ea239d9 432 * Select timing parameters for host.
b57c43ad 433 */
7ea239d9 434void mmc_set_timing(struct mmc_host *host, unsigned int timing)
b57c43ad 435{
7ea239d9
PO
436 host->ios.timing = timing;
437 mmc_set_ios(host);
b57c43ad
PO
438}
439
1da177e4 440/*
45f8245b
RK
441 * Apply power to the MMC stack. This is a two-stage process.
442 * First, we enable power to the card without the clock running.
443 * We then wait a bit for the power to stabilise. Finally,
444 * enable the bus drivers and clock to the card.
445 *
446 * We must _NOT_ enable the clock prior to power stablising.
447 *
448 * If a host does all the power sequencing itself, ignore the
449 * initial MMC_POWER_UP stage.
1da177e4
LT
450 */
451static void mmc_power_up(struct mmc_host *host)
452{
453 int bit = fls(host->ocr_avail) - 1;
454
455 host->ios.vdd = bit;
456 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
865e9f13 457 host->ios.chip_select = MMC_CS_DONTCARE;
1da177e4 458 host->ios.power_mode = MMC_POWER_UP;
f218278a 459 host->ios.bus_width = MMC_BUS_WIDTH_1;
cd9277c0 460 host->ios.timing = MMC_TIMING_LEGACY;
920e70c5 461 mmc_set_ios(host);
1da177e4 462
f9996aee
PO
463 /*
464 * This delay should be sufficient to allow the power supply
465 * to reach the minimum voltage.
466 */
467 mmc_delay(2);
1da177e4
LT
468
469 host->ios.clock = host->f_min;
470 host->ios.power_mode = MMC_POWER_ON;
920e70c5 471 mmc_set_ios(host);
1da177e4 472
f9996aee
PO
473 /*
474 * This delay must be at least 74 clock sizes, or 1 ms, or the
475 * time required to reach a stable voltage.
476 */
1da177e4
LT
477 mmc_delay(2);
478}
479
480static void mmc_power_off(struct mmc_host *host)
481{
482 host->ios.clock = 0;
483 host->ios.vdd = 0;
484 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
865e9f13 485 host->ios.chip_select = MMC_CS_DONTCARE;
1da177e4 486 host->ios.power_mode = MMC_POWER_OFF;
f218278a 487 host->ios.bus_width = MMC_BUS_WIDTH_1;
cd9277c0 488 host->ios.timing = MMC_TIMING_LEGACY;
920e70c5 489 mmc_set_ios(host);
1da177e4
LT
490}
491
39361851
AB
492/*
493 * Cleanup when the last reference to the bus operator is dropped.
494 */
495void __mmc_release_bus(struct mmc_host *host)
496{
497 BUG_ON(!host);
498 BUG_ON(host->bus_refs);
499 BUG_ON(!host->bus_dead);
500
501 host->bus_ops = NULL;
502}
503
504/*
505 * Increase reference count of bus operator
506 */
507static inline void mmc_bus_get(struct mmc_host *host)
508{
509 unsigned long flags;
510
511 spin_lock_irqsave(&host->lock, flags);
512 host->bus_refs++;
513 spin_unlock_irqrestore(&host->lock, flags);
514}
515
516/*
517 * Decrease reference count of bus operator and free it if
518 * it is the last reference.
519 */
520static inline void mmc_bus_put(struct mmc_host *host)
521{
522 unsigned long flags;
523
524 spin_lock_irqsave(&host->lock, flags);
525 host->bus_refs--;
526 if ((host->bus_refs == 0) && host->bus_ops)
527 __mmc_release_bus(host);
528 spin_unlock_irqrestore(&host->lock, flags);
529}
530
1da177e4 531/*
7ea239d9
PO
532 * Assign a mmc bus handler to a host. Only one bus handler may control a
533 * host at any given time.
1da177e4 534 */
7ea239d9 535void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1da177e4 536{
7ea239d9 537 unsigned long flags;
e45a1bd2 538
7ea239d9
PO
539 BUG_ON(!host);
540 BUG_ON(!ops);
b855885e 541
d84075c8 542 WARN_ON(!host->claimed);
bce40a36 543
7ea239d9 544 spin_lock_irqsave(&host->lock, flags);
bce40a36 545
7ea239d9
PO
546 BUG_ON(host->bus_ops);
547 BUG_ON(host->bus_refs);
b57c43ad 548
7ea239d9
PO
549 host->bus_ops = ops;
550 host->bus_refs = 1;
551 host->bus_dead = 0;
b57c43ad 552
7ea239d9 553 spin_unlock_irqrestore(&host->lock, flags);
b57c43ad
PO
554}
555
7ea239d9
PO
556/*
557 * Remove the current bus handler from a host. Assumes that there are
558 * no interesting cards left, so the bus is powered down.
559 */
560void mmc_detach_bus(struct mmc_host *host)
7ccd266e 561{
7ea239d9 562 unsigned long flags;
7ccd266e 563
7ea239d9 564 BUG_ON(!host);
7ccd266e 565
d84075c8
PO
566 WARN_ON(!host->claimed);
567 WARN_ON(!host->bus_ops);
cd9277c0 568
7ea239d9 569 spin_lock_irqsave(&host->lock, flags);
7ccd266e 570
7ea239d9 571 host->bus_dead = 1;
7ccd266e 572
7ea239d9 573 spin_unlock_irqrestore(&host->lock, flags);
1da177e4 574
7ea239d9 575 mmc_power_off(host);
1da177e4 576
7ea239d9 577 mmc_bus_put(host);
1da177e4
LT
578}
579
1da177e4
LT
580/**
581 * mmc_detect_change - process change of state on a MMC socket
582 * @host: host which changed state.
8dc00335 583 * @delay: optional delay to wait before detection (jiffies)
1da177e4 584 *
67a61c48
PO
585 * MMC drivers should call this when they detect a card has been
586 * inserted or removed. The MMC layer will confirm that any
587 * present card is still functional, and initialize any newly
588 * inserted.
1da177e4 589 */
8dc00335 590void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1da177e4 591{
3b91e550 592#ifdef CONFIG_MMC_DEBUG
1efd48b3 593 unsigned long flags;
01f41ec7 594 spin_lock_irqsave(&host->lock, flags);
d84075c8 595 WARN_ON(host->removed);
01f41ec7 596 spin_unlock_irqrestore(&host->lock, flags);
3b91e550
PO
597#endif
598
c4028958 599 mmc_schedule_delayed_work(&host->detect, delay);
1da177e4
LT
600}
601
602EXPORT_SYMBOL(mmc_detect_change);
603
604
b93931a6 605void mmc_rescan(struct work_struct *work)
1da177e4 606{
c4028958
DH
607 struct mmc_host *host =
608 container_of(work, struct mmc_host, detect.work);
7ea239d9
PO
609 u32 ocr;
610 int err;
1da177e4 611
7ea239d9 612 mmc_bus_get(host);
b855885e 613
7ea239d9 614 if (host->bus_ops == NULL) {
1da177e4 615 /*
7ea239d9
PO
616 * Only we can add a new handler, so it's safe to
617 * release the lock here.
1da177e4 618 */
7ea239d9 619 mmc_bus_put(host);
1da177e4 620
7ea239d9 621 mmc_claim_host(host);
1da177e4 622
7ea239d9
PO
623 mmc_power_up(host);
624 mmc_go_idle(host);
1da177e4 625
7ea239d9 626 mmc_send_if_cond(host, host->ocr_avail);
1da177e4 627
5c4e6f13
PO
628 /*
629 * First we search for SDIO...
630 */
631 err = mmc_send_io_op_cond(host, 0, &ocr);
632 if (!err) {
633 if (mmc_attach_sdio(host, ocr))
634 mmc_power_off(host);
635 return;
636 }
637
638 /*
639 * ...then normal SD...
640 */
7ea239d9 641 err = mmc_send_app_op_cond(host, 0, &ocr);
17b0429d 642 if (!err) {
7ea239d9
PO
643 if (mmc_attach_sd(host, ocr))
644 mmc_power_off(host);
5c4e6f13
PO
645 return;
646 }
647
648 /*
649 * ...and finally MMC.
650 */
651 err = mmc_send_op_cond(host, 0, &ocr);
652 if (!err) {
653 if (mmc_attach_mmc(host, ocr))
7ea239d9 654 mmc_power_off(host);
5c4e6f13 655 return;
7ea239d9 656 }
5c4e6f13
PO
657
658 mmc_release_host(host);
659 mmc_power_off(host);
7ea239d9
PO
660 } else {
661 if (host->bus_ops->detect && !host->bus_dead)
662 host->bus_ops->detect(host);
663
664 mmc_bus_put(host);
665 }
1da177e4
LT
666}
667
b93931a6 668void mmc_start_host(struct mmc_host *host)
1da177e4 669{
b93931a6
PO
670 mmc_power_off(host);
671 mmc_detect_change(host, 0);
1da177e4
LT
672}
673
b93931a6 674void mmc_stop_host(struct mmc_host *host)
1da177e4 675{
3b91e550 676#ifdef CONFIG_MMC_DEBUG
1efd48b3
PO
677 unsigned long flags;
678 spin_lock_irqsave(&host->lock, flags);
3b91e550 679 host->removed = 1;
1efd48b3 680 spin_unlock_irqrestore(&host->lock, flags);
3b91e550
PO
681#endif
682
683 mmc_flush_scheduled_work();
684
7ea239d9
PO
685 mmc_bus_get(host);
686 if (host->bus_ops && !host->bus_dead) {
687 if (host->bus_ops->remove)
688 host->bus_ops->remove(host);
689
690 mmc_claim_host(host);
691 mmc_detach_bus(host);
692 mmc_release_host(host);
1da177e4 693 }
7ea239d9
PO
694 mmc_bus_put(host);
695
696 BUG_ON(host->card);
1da177e4
LT
697
698 mmc_power_off(host);
699}
700
1da177e4
LT
701#ifdef CONFIG_PM
702
703/**
704 * mmc_suspend_host - suspend a host
705 * @host: mmc host
706 * @state: suspend mode (PM_SUSPEND_xxx)
707 */
e5378ca8 708int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1da177e4 709{
b5af25be
PO
710 mmc_flush_scheduled_work();
711
7ea239d9
PO
712 mmc_bus_get(host);
713 if (host->bus_ops && !host->bus_dead) {
6abaa0c9
PO
714 if (host->bus_ops->suspend)
715 host->bus_ops->suspend(host);
716 if (!host->bus_ops->resume) {
717 if (host->bus_ops->remove)
718 host->bus_ops->remove(host);
719
720 mmc_claim_host(host);
721 mmc_detach_bus(host);
722 mmc_release_host(host);
723 }
b5af25be 724 }
7ea239d9
PO
725 mmc_bus_put(host);
726
1da177e4 727 mmc_power_off(host);
1da177e4
LT
728
729 return 0;
730}
731
732EXPORT_SYMBOL(mmc_suspend_host);
733
734/**
735 * mmc_resume_host - resume a previously suspended host
736 * @host: mmc host
737 */
738int mmc_resume_host(struct mmc_host *host)
739{
6abaa0c9
PO
740 mmc_bus_get(host);
741 if (host->bus_ops && !host->bus_dead) {
742 mmc_power_up(host);
743 BUG_ON(!host->bus_ops->resume);
744 host->bus_ops->resume(host);
745 }
746 mmc_bus_put(host);
747
748 /*
749 * We add a slight delay here so that resume can progress
750 * in parallel.
751 */
752 mmc_detect_change(host, 1);
1da177e4
LT
753
754 return 0;
755}
756
757EXPORT_SYMBOL(mmc_resume_host);
758
759#endif
760
ffce2e7e
PO
761static int __init mmc_init(void)
762{
763 int ret;
764
765 workqueue = create_singlethread_workqueue("kmmcd");
766 if (!workqueue)
767 return -ENOMEM;
768
769 ret = mmc_register_bus();
e29a7d73
PO
770 if (ret)
771 goto destroy_workqueue;
772
773 ret = mmc_register_host_class();
774 if (ret)
775 goto unregister_bus;
776
777 ret = sdio_register_bus();
778 if (ret)
779 goto unregister_host_class;
780
781 return 0;
782
783unregister_host_class:
784 mmc_unregister_host_class();
785unregister_bus:
786 mmc_unregister_bus();
787destroy_workqueue:
788 destroy_workqueue(workqueue);
789
ffce2e7e
PO
790 return ret;
791}
792
793static void __exit mmc_exit(void)
794{
e29a7d73 795 sdio_unregister_bus();
ffce2e7e
PO
796 mmc_unregister_host_class();
797 mmc_unregister_bus();
798 destroy_workqueue(workqueue);
799}
800
26074962 801subsys_initcall(mmc_init);
ffce2e7e
PO
802module_exit(mmc_exit);
803
1da177e4 804MODULE_LICENSE("GPL");