]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/mmc/core/core.c
atmel-mci: fix sdc_reg typo
[linux-2.6-omap-h63xx.git] / drivers / mmc / core / core.c
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-2008 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 <linux/leds.h>
22 #include <linux/scatterlist.h>
23 #include <linux/log2.h>
24
25 #include <linux/mmc/card.h>
26 #include <linux/mmc/host.h>
27 #include <linux/mmc/mmc.h>
28 #include <linux/mmc/sd.h>
29
30 #include "core.h"
31 #include "bus.h"
32 #include "host.h"
33 #include "sdio_bus.h"
34
35 #include "mmc_ops.h"
36 #include "sd_ops.h"
37 #include "sdio_ops.h"
38
39 static struct workqueue_struct *workqueue;
40
41 /*
42  * Enabling software CRCs on the data blocks can be a significant (30%)
43  * performance cost, and for other reasons may not always be desired.
44  * So we allow it it to be disabled.
45  */
46 int use_spi_crc = 1;
47 module_param(use_spi_crc, bool, 0);
48
49 /*
50  * Internal function. Schedule delayed work in the MMC work queue.
51  */
52 static int mmc_schedule_delayed_work(struct delayed_work *work,
53                                      unsigned long delay)
54 {
55         return queue_delayed_work(workqueue, work, delay);
56 }
57
58 /*
59  * Internal function. Flush all scheduled work from the MMC work queue.
60  */
61 static void mmc_flush_scheduled_work(void)
62 {
63         flush_workqueue(workqueue);
64 }
65
66 /**
67  *      mmc_request_done - finish processing an MMC request
68  *      @host: MMC host which completed request
69  *      @mrq: MMC request which request
70  *
71  *      MMC drivers should call this function when they have completed
72  *      their processing of a request.
73  */
74 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
75 {
76         struct mmc_command *cmd = mrq->cmd;
77         int err = cmd->error;
78
79         if (err && cmd->retries && mmc_host_is_spi(host)) {
80                 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
81                         cmd->retries = 0;
82         }
83
84         if (err && cmd->retries) {
85                 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
86                         mmc_hostname(host), cmd->opcode, err);
87
88                 cmd->retries--;
89                 cmd->error = 0;
90                 host->ops->request(host, mrq);
91         } else {
92                 led_trigger_event(host->led, LED_OFF);
93
94                 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
95                         mmc_hostname(host), cmd->opcode, err,
96                         cmd->resp[0], cmd->resp[1],
97                         cmd->resp[2], cmd->resp[3]);
98
99                 if (mrq->data) {
100                         pr_debug("%s:     %d bytes transferred: %d\n",
101                                 mmc_hostname(host),
102                                 mrq->data->bytes_xfered, mrq->data->error);
103                 }
104
105                 if (mrq->stop) {
106                         pr_debug("%s:     (CMD%u): %d: %08x %08x %08x %08x\n",
107                                 mmc_hostname(host), mrq->stop->opcode,
108                                 mrq->stop->error,
109                                 mrq->stop->resp[0], mrq->stop->resp[1],
110                                 mrq->stop->resp[2], mrq->stop->resp[3]);
111                 }
112
113                 if (mrq->done)
114                         mrq->done(mrq);
115         }
116 }
117
118 EXPORT_SYMBOL(mmc_request_done);
119
120 static void
121 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
122 {
123 #ifdef CONFIG_MMC_DEBUG
124         unsigned int i, sz;
125         struct scatterlist *sg;
126 #endif
127
128         pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
129                  mmc_hostname(host), mrq->cmd->opcode,
130                  mrq->cmd->arg, mrq->cmd->flags);
131
132         if (mrq->data) {
133                 pr_debug("%s:     blksz %d blocks %d flags %08x "
134                         "tsac %d ms nsac %d\n",
135                         mmc_hostname(host), mrq->data->blksz,
136                         mrq->data->blocks, mrq->data->flags,
137                         mrq->data->timeout_ns / 1000000,
138                         mrq->data->timeout_clks);
139         }
140
141         if (mrq->stop) {
142                 pr_debug("%s:     CMD%u arg %08x flags %08x\n",
143                          mmc_hostname(host), mrq->stop->opcode,
144                          mrq->stop->arg, mrq->stop->flags);
145         }
146
147         WARN_ON(!host->claimed);
148
149         led_trigger_event(host->led, LED_FULL);
150
151         mrq->cmd->error = 0;
152         mrq->cmd->mrq = mrq;
153         if (mrq->data) {
154                 BUG_ON(mrq->data->blksz > host->max_blk_size);
155                 BUG_ON(mrq->data->blocks > host->max_blk_count);
156                 BUG_ON(mrq->data->blocks * mrq->data->blksz >
157                         host->max_req_size);
158
159 #ifdef CONFIG_MMC_DEBUG
160                 sz = 0;
161                 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
162                         sz += sg->length;
163                 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
164 #endif
165
166                 mrq->cmd->data = mrq->data;
167                 mrq->data->error = 0;
168                 mrq->data->mrq = mrq;
169                 if (mrq->stop) {
170                         mrq->data->stop = mrq->stop;
171                         mrq->stop->error = 0;
172                         mrq->stop->mrq = mrq;
173                 }
174         }
175         host->ops->request(host, mrq);
176 }
177
178 static void mmc_wait_done(struct mmc_request *mrq)
179 {
180         complete(mrq->done_data);
181 }
182
183 /**
184  *      mmc_wait_for_req - start a request and wait for completion
185  *      @host: MMC host to start command
186  *      @mrq: MMC request to start
187  *
188  *      Start a new MMC custom command request for a host, and wait
189  *      for the command to complete. Does not attempt to parse the
190  *      response.
191  */
192 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
193 {
194         DECLARE_COMPLETION_ONSTACK(complete);
195
196         mrq->done_data = &complete;
197         mrq->done = mmc_wait_done;
198
199         mmc_start_request(host, mrq);
200
201         wait_for_completion(&complete);
202 }
203
204 EXPORT_SYMBOL(mmc_wait_for_req);
205
206 /**
207  *      mmc_wait_for_cmd - start a command and wait for completion
208  *      @host: MMC host to start command
209  *      @cmd: MMC command to start
210  *      @retries: maximum number of retries
211  *
212  *      Start a new MMC command for a host, and wait for the command
213  *      to complete.  Return any error that occurred while the command
214  *      was executing.  Do not attempt to parse the response.
215  */
216 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
217 {
218         struct mmc_request mrq;
219
220         WARN_ON(!host->claimed);
221
222         memset(&mrq, 0, sizeof(struct mmc_request));
223
224         memset(cmd->resp, 0, sizeof(cmd->resp));
225         cmd->retries = retries;
226
227         mrq.cmd = cmd;
228         cmd->data = NULL;
229
230         mmc_wait_for_req(host, &mrq);
231
232         return cmd->error;
233 }
234
235 EXPORT_SYMBOL(mmc_wait_for_cmd);
236
237 /**
238  *      mmc_set_data_timeout - set the timeout for a data command
239  *      @data: data phase for command
240  *      @card: the MMC card associated with the data transfer
241  *
242  *      Computes the data timeout parameters according to the
243  *      correct algorithm given the card type.
244  */
245 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
246 {
247         unsigned int mult;
248
249         /*
250          * SDIO cards only define an upper 1 s limit on access.
251          */
252         if (mmc_card_sdio(card)) {
253                 data->timeout_ns = 1000000000;
254                 data->timeout_clks = 0;
255                 return;
256         }
257
258         /*
259          * SD cards use a 100 multiplier rather than 10
260          */
261         mult = mmc_card_sd(card) ? 100 : 10;
262
263         /*
264          * Scale up the multiplier (and therefore the timeout) by
265          * the r2w factor for writes.
266          */
267         if (data->flags & MMC_DATA_WRITE)
268                 mult <<= card->csd.r2w_factor;
269
270         data->timeout_ns = card->csd.tacc_ns * mult;
271         data->timeout_clks = card->csd.tacc_clks * mult;
272
273         /*
274          * SD cards also have an upper limit on the timeout.
275          */
276         if (mmc_card_sd(card)) {
277                 unsigned int timeout_us, limit_us;
278
279                 timeout_us = data->timeout_ns / 1000;
280                 timeout_us += data->timeout_clks * 1000 /
281                         (card->host->ios.clock / 1000);
282
283                 if (data->flags & MMC_DATA_WRITE)
284                         /*
285                          * The limit is really 250 ms, but that is
286                          * insufficient for some crappy cards.
287                          */
288                         limit_us = 300000;
289                 else
290                         limit_us = 100000;
291
292                 /*
293                  * SDHC cards always use these fixed values.
294                  */
295                 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
296                         data->timeout_ns = limit_us * 1000;
297                         data->timeout_clks = 0;
298                 }
299         }
300         /*
301          * Some cards need very high timeouts if driven in SPI mode.
302          * The worst observed timeout was 900ms after writing a
303          * continuous stream of data until the internal logic
304          * overflowed.
305          */
306         if (mmc_host_is_spi(card->host)) {
307                 if (data->flags & MMC_DATA_WRITE) {
308                         if (data->timeout_ns < 1000000000)
309                                 data->timeout_ns = 1000000000;  /* 1s */
310                 } else {
311                         if (data->timeout_ns < 100000000)
312                                 data->timeout_ns =  100000000;  /* 100ms */
313                 }
314         }
315 }
316 EXPORT_SYMBOL(mmc_set_data_timeout);
317
318 /**
319  *      mmc_align_data_size - pads a transfer size to a more optimal value
320  *      @card: the MMC card associated with the data transfer
321  *      @sz: original transfer size
322  *
323  *      Pads the original data size with a number of extra bytes in
324  *      order to avoid controller bugs and/or performance hits
325  *      (e.g. some controllers revert to PIO for certain sizes).
326  *
327  *      Returns the improved size, which might be unmodified.
328  *
329  *      Note that this function is only relevant when issuing a
330  *      single scatter gather entry.
331  */
332 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
333 {
334         /*
335          * FIXME: We don't have a system for the controller to tell
336          * the core about its problems yet, so for now we just 32-bit
337          * align the size.
338          */
339         sz = ((sz + 3) / 4) * 4;
340
341         return sz;
342 }
343 EXPORT_SYMBOL(mmc_align_data_size);
344
345 /**
346  *      __mmc_claim_host - exclusively claim a host
347  *      @host: mmc host to claim
348  *      @abort: whether or not the operation should be aborted
349  *
350  *      Claim a host for a set of operations.  If @abort is non null and
351  *      dereference a non-zero value then this will return prematurely with
352  *      that non-zero value without acquiring the lock.  Returns zero
353  *      with the lock held otherwise.
354  */
355 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
356 {
357         DECLARE_WAITQUEUE(wait, current);
358         unsigned long flags;
359         int stop;
360
361         might_sleep();
362
363         add_wait_queue(&host->wq, &wait);
364         spin_lock_irqsave(&host->lock, flags);
365         while (1) {
366                 set_current_state(TASK_UNINTERRUPTIBLE);
367                 stop = abort ? atomic_read(abort) : 0;
368                 if (stop || !host->claimed)
369                         break;
370                 spin_unlock_irqrestore(&host->lock, flags);
371                 schedule();
372                 spin_lock_irqsave(&host->lock, flags);
373         }
374         set_current_state(TASK_RUNNING);
375         if (!stop)
376                 host->claimed = 1;
377         else
378                 wake_up(&host->wq);
379         spin_unlock_irqrestore(&host->lock, flags);
380         remove_wait_queue(&host->wq, &wait);
381         return stop;
382 }
383
384 EXPORT_SYMBOL(__mmc_claim_host);
385
386 /**
387  *      mmc_release_host - release a host
388  *      @host: mmc host to release
389  *
390  *      Release a MMC host, allowing others to claim the host
391  *      for their operations.
392  */
393 void mmc_release_host(struct mmc_host *host)
394 {
395         unsigned long flags;
396
397         WARN_ON(!host->claimed);
398
399         spin_lock_irqsave(&host->lock, flags);
400         host->claimed = 0;
401         spin_unlock_irqrestore(&host->lock, flags);
402
403         wake_up(&host->wq);
404 }
405
406 EXPORT_SYMBOL(mmc_release_host);
407
408 /*
409  * Internal function that does the actual ios call to the host driver,
410  * optionally printing some debug output.
411  */
412 static inline void mmc_set_ios(struct mmc_host *host)
413 {
414         struct mmc_ios *ios = &host->ios;
415
416         pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
417                 "width %u timing %u\n",
418                  mmc_hostname(host), ios->clock, ios->bus_mode,
419                  ios->power_mode, ios->chip_select, ios->vdd,
420                  ios->bus_width, ios->timing);
421
422         host->ops->set_ios(host, ios);
423 }
424
425 /*
426  * Control chip select pin on a host.
427  */
428 void mmc_set_chip_select(struct mmc_host *host, int mode)
429 {
430         host->ios.chip_select = mode;
431         mmc_set_ios(host);
432 }
433
434 /*
435  * Sets the host clock to the highest possible frequency that
436  * is below "hz".
437  */
438 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
439 {
440         WARN_ON(hz < host->f_min);
441
442         if (hz > host->f_max)
443                 hz = host->f_max;
444
445         host->ios.clock = hz;
446         mmc_set_ios(host);
447 }
448
449 /*
450  * Change the bus mode (open drain/push-pull) of a host.
451  */
452 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
453 {
454         host->ios.bus_mode = mode;
455         mmc_set_ios(host);
456 }
457
458 /*
459  * Change data bus width of a host.
460  */
461 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
462 {
463         host->ios.bus_width = width;
464         mmc_set_ios(host);
465 }
466
467 /**
468  * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
469  * @vdd:        voltage (mV)
470  * @low_bits:   prefer low bits in boundary cases
471  *
472  * This function returns the OCR bit number according to the provided @vdd
473  * value. If conversion is not possible a negative errno value returned.
474  *
475  * Depending on the @low_bits flag the function prefers low or high OCR bits
476  * on boundary voltages. For example,
477  * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
478  * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
479  *
480  * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
481  */
482 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
483 {
484         const int max_bit = ilog2(MMC_VDD_35_36);
485         int bit;
486
487         if (vdd < 1650 || vdd > 3600)
488                 return -EINVAL;
489
490         if (vdd >= 1650 && vdd <= 1950)
491                 return ilog2(MMC_VDD_165_195);
492
493         if (low_bits)
494                 vdd -= 1;
495
496         /* Base 2000 mV, step 100 mV, bit's base 8. */
497         bit = (vdd - 2000) / 100 + 8;
498         if (bit > max_bit)
499                 return max_bit;
500         return bit;
501 }
502
503 /**
504  * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
505  * @vdd_min:    minimum voltage value (mV)
506  * @vdd_max:    maximum voltage value (mV)
507  *
508  * This function returns the OCR mask bits according to the provided @vdd_min
509  * and @vdd_max values. If conversion is not possible the function returns 0.
510  *
511  * Notes wrt boundary cases:
512  * This function sets the OCR bits for all boundary voltages, for example
513  * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
514  * MMC_VDD_34_35 mask.
515  */
516 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
517 {
518         u32 mask = 0;
519
520         if (vdd_max < vdd_min)
521                 return 0;
522
523         /* Prefer high bits for the boundary vdd_max values. */
524         vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
525         if (vdd_max < 0)
526                 return 0;
527
528         /* Prefer low bits for the boundary vdd_min values. */
529         vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
530         if (vdd_min < 0)
531                 return 0;
532
533         /* Fill the mask, from max bit to min bit. */
534         while (vdd_max >= vdd_min)
535                 mask |= 1 << vdd_max--;
536
537         return mask;
538 }
539 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
540
541 /*
542  * Mask off any voltages we don't support and select
543  * the lowest voltage
544  */
545 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
546 {
547         int bit;
548
549         ocr &= host->ocr_avail;
550
551         bit = ffs(ocr);
552         if (bit) {
553                 bit -= 1;
554
555                 ocr &= 3 << bit;
556
557                 host->ios.vdd = bit;
558                 mmc_set_ios(host);
559         } else {
560                 pr_warning("%s: host doesn't support card's voltages\n",
561                                 mmc_hostname(host));
562                 ocr = 0;
563         }
564
565         return ocr;
566 }
567
568 /*
569  * Select timing parameters for host.
570  */
571 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
572 {
573         host->ios.timing = timing;
574         mmc_set_ios(host);
575 }
576
577 /*
578  * Apply power to the MMC stack.  This is a two-stage process.
579  * First, we enable power to the card without the clock running.
580  * We then wait a bit for the power to stabilise.  Finally,
581  * enable the bus drivers and clock to the card.
582  *
583  * We must _NOT_ enable the clock prior to power stablising.
584  *
585  * If a host does all the power sequencing itself, ignore the
586  * initial MMC_POWER_UP stage.
587  */
588 static void mmc_power_up(struct mmc_host *host)
589 {
590         int bit = fls(host->ocr_avail) - 1;
591
592         host->ios.vdd = bit;
593         if (mmc_host_is_spi(host)) {
594                 host->ios.chip_select = MMC_CS_HIGH;
595                 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
596         } else {
597                 host->ios.chip_select = MMC_CS_DONTCARE;
598                 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
599         }
600         host->ios.power_mode = MMC_POWER_UP;
601         host->ios.bus_width = MMC_BUS_WIDTH_1;
602         host->ios.timing = MMC_TIMING_LEGACY;
603         mmc_set_ios(host);
604
605         /*
606          * This delay should be sufficient to allow the power supply
607          * to reach the minimum voltage.
608          */
609         mmc_delay(2);
610
611         host->ios.clock = host->f_min;
612         host->ios.power_mode = MMC_POWER_ON;
613         mmc_set_ios(host);
614
615         /*
616          * This delay must be at least 74 clock sizes, or 1 ms, or the
617          * time required to reach a stable voltage.
618          */
619         mmc_delay(2);
620 }
621
622 static void mmc_power_off(struct mmc_host *host)
623 {
624         host->ios.clock = 0;
625         host->ios.vdd = 0;
626         if (!mmc_host_is_spi(host)) {
627                 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
628                 host->ios.chip_select = MMC_CS_DONTCARE;
629         }
630         host->ios.power_mode = MMC_POWER_OFF;
631         host->ios.bus_width = MMC_BUS_WIDTH_1;
632         host->ios.timing = MMC_TIMING_LEGACY;
633         mmc_set_ios(host);
634 }
635
636 /*
637  * Cleanup when the last reference to the bus operator is dropped.
638  */
639 static void __mmc_release_bus(struct mmc_host *host)
640 {
641         BUG_ON(!host);
642         BUG_ON(host->bus_refs);
643         BUG_ON(!host->bus_dead);
644
645         host->bus_ops = NULL;
646 }
647
648 /*
649  * Increase reference count of bus operator
650  */
651 static inline void mmc_bus_get(struct mmc_host *host)
652 {
653         unsigned long flags;
654
655         spin_lock_irqsave(&host->lock, flags);
656         host->bus_refs++;
657         spin_unlock_irqrestore(&host->lock, flags);
658 }
659
660 /*
661  * Decrease reference count of bus operator and free it if
662  * it is the last reference.
663  */
664 static inline void mmc_bus_put(struct mmc_host *host)
665 {
666         unsigned long flags;
667
668         spin_lock_irqsave(&host->lock, flags);
669         host->bus_refs--;
670         if ((host->bus_refs == 0) && host->bus_ops)
671                 __mmc_release_bus(host);
672         spin_unlock_irqrestore(&host->lock, flags);
673 }
674
675 /*
676  * Assign a mmc bus handler to a host. Only one bus handler may control a
677  * host at any given time.
678  */
679 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
680 {
681         unsigned long flags;
682
683         BUG_ON(!host);
684         BUG_ON(!ops);
685
686         WARN_ON(!host->claimed);
687
688         spin_lock_irqsave(&host->lock, flags);
689
690         BUG_ON(host->bus_ops);
691         BUG_ON(host->bus_refs);
692
693         host->bus_ops = ops;
694         host->bus_refs = 1;
695         host->bus_dead = 0;
696
697         spin_unlock_irqrestore(&host->lock, flags);
698 }
699
700 /*
701  * Remove the current bus handler from a host. Assumes that there are
702  * no interesting cards left, so the bus is powered down.
703  */
704 void mmc_detach_bus(struct mmc_host *host)
705 {
706         unsigned long flags;
707
708         BUG_ON(!host);
709
710         WARN_ON(!host->claimed);
711         WARN_ON(!host->bus_ops);
712
713         spin_lock_irqsave(&host->lock, flags);
714
715         host->bus_dead = 1;
716
717         spin_unlock_irqrestore(&host->lock, flags);
718
719         mmc_power_off(host);
720
721         mmc_bus_put(host);
722 }
723
724 /**
725  *      mmc_detect_change - process change of state on a MMC socket
726  *      @host: host which changed state.
727  *      @delay: optional delay to wait before detection (jiffies)
728  *
729  *      MMC drivers should call this when they detect a card has been
730  *      inserted or removed. The MMC layer will confirm that any
731  *      present card is still functional, and initialize any newly
732  *      inserted.
733  */
734 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
735 {
736 #ifdef CONFIG_MMC_DEBUG
737         unsigned long flags;
738         spin_lock_irqsave(&host->lock, flags);
739         WARN_ON(host->removed);
740         spin_unlock_irqrestore(&host->lock, flags);
741 #endif
742
743         mmc_schedule_delayed_work(&host->detect, delay);
744 }
745
746 EXPORT_SYMBOL(mmc_detect_change);
747
748
749 void mmc_rescan(struct work_struct *work)
750 {
751         struct mmc_host *host =
752                 container_of(work, struct mmc_host, detect.work);
753         u32 ocr;
754         int err;
755
756         mmc_bus_get(host);
757
758         if (host->bus_ops == NULL) {
759                 /*
760                  * Only we can add a new handler, so it's safe to
761                  * release the lock here.
762                  */
763                 mmc_bus_put(host);
764
765                 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
766                         goto out;
767
768                 mmc_claim_host(host);
769
770                 mmc_power_up(host);
771                 mmc_go_idle(host);
772
773                 mmc_send_if_cond(host, host->ocr_avail);
774
775                 /*
776                  * First we search for SDIO...
777                  */
778                 err = mmc_send_io_op_cond(host, 0, &ocr);
779                 if (!err) {
780                         if (mmc_attach_sdio(host, ocr))
781                                 mmc_power_off(host);
782                         goto out;
783                 }
784
785                 /*
786                  * ...then normal SD...
787                  */
788                 err = mmc_send_app_op_cond(host, 0, &ocr);
789                 if (!err) {
790                         if (mmc_attach_sd(host, ocr))
791                                 mmc_power_off(host);
792                         goto out;
793                 }
794
795                 /*
796                  * ...and finally MMC.
797                  */
798                 err = mmc_send_op_cond(host, 0, &ocr);
799                 if (!err) {
800                         if (mmc_attach_mmc(host, ocr))
801                                 mmc_power_off(host);
802                         goto out;
803                 }
804
805                 mmc_release_host(host);
806                 mmc_power_off(host);
807         } else {
808                 if (host->bus_ops->detect && !host->bus_dead)
809                         host->bus_ops->detect(host);
810
811                 mmc_bus_put(host);
812         }
813 out:
814         if (host->caps & MMC_CAP_NEEDS_POLL)
815                 mmc_schedule_delayed_work(&host->detect, HZ);
816 }
817
818 void mmc_start_host(struct mmc_host *host)
819 {
820         mmc_power_off(host);
821         mmc_detect_change(host, 0);
822 }
823
824 void mmc_stop_host(struct mmc_host *host)
825 {
826 #ifdef CONFIG_MMC_DEBUG
827         unsigned long flags;
828         spin_lock_irqsave(&host->lock, flags);
829         host->removed = 1;
830         spin_unlock_irqrestore(&host->lock, flags);
831 #endif
832
833         cancel_delayed_work(&host->detect);
834         mmc_flush_scheduled_work();
835
836         mmc_bus_get(host);
837         if (host->bus_ops && !host->bus_dead) {
838                 if (host->bus_ops->remove)
839                         host->bus_ops->remove(host);
840
841                 mmc_claim_host(host);
842                 mmc_detach_bus(host);
843                 mmc_release_host(host);
844         }
845         mmc_bus_put(host);
846
847         BUG_ON(host->card);
848
849         mmc_power_off(host);
850 }
851
852 #ifdef CONFIG_PM
853
854 /**
855  *      mmc_suspend_host - suspend a host
856  *      @host: mmc host
857  *      @state: suspend mode (PM_SUSPEND_xxx)
858  */
859 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
860 {
861         cancel_delayed_work(&host->detect);
862         mmc_flush_scheduled_work();
863
864         mmc_bus_get(host);
865         if (host->bus_ops && !host->bus_dead) {
866                 if (host->bus_ops->suspend)
867                         host->bus_ops->suspend(host);
868                 if (!host->bus_ops->resume) {
869                         if (host->bus_ops->remove)
870                                 host->bus_ops->remove(host);
871
872                         mmc_claim_host(host);
873                         mmc_detach_bus(host);
874                         mmc_release_host(host);
875                 }
876         }
877         mmc_bus_put(host);
878
879         mmc_power_off(host);
880
881         return 0;
882 }
883
884 EXPORT_SYMBOL(mmc_suspend_host);
885
886 /**
887  *      mmc_resume_host - resume a previously suspended host
888  *      @host: mmc host
889  */
890 int mmc_resume_host(struct mmc_host *host)
891 {
892         mmc_bus_get(host);
893         if (host->bus_ops && !host->bus_dead) {
894                 mmc_power_up(host);
895                 mmc_select_voltage(host, host->ocr);
896                 BUG_ON(!host->bus_ops->resume);
897                 host->bus_ops->resume(host);
898         }
899         mmc_bus_put(host);
900
901         /*
902          * We add a slight delay here so that resume can progress
903          * in parallel.
904          */
905         mmc_detect_change(host, 1);
906
907         return 0;
908 }
909
910 EXPORT_SYMBOL(mmc_resume_host);
911
912 #endif
913
914 static int __init mmc_init(void)
915 {
916         int ret;
917
918         workqueue = create_singlethread_workqueue("kmmcd");
919         if (!workqueue)
920                 return -ENOMEM;
921
922         ret = mmc_register_bus();
923         if (ret)
924                 goto destroy_workqueue;
925
926         ret = mmc_register_host_class();
927         if (ret)
928                 goto unregister_bus;
929
930         ret = sdio_register_bus();
931         if (ret)
932                 goto unregister_host_class;
933
934         return 0;
935
936 unregister_host_class:
937         mmc_unregister_host_class();
938 unregister_bus:
939         mmc_unregister_bus();
940 destroy_workqueue:
941         destroy_workqueue(workqueue);
942
943         return ret;
944 }
945
946 static void __exit mmc_exit(void)
947 {
948         sdio_unregister_bus();
949         mmc_unregister_host_class();
950         mmc_unregister_bus();
951         destroy_workqueue(workqueue);
952 }
953
954 subsys_initcall(mmc_init);
955 module_exit(mmc_exit);
956
957 MODULE_LICENSE("GPL");