]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/mmc/core/core.c
mmc: skip detection of nonremovable cards on rescan
[mv-sheeva.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 #include <linux/regulator/consumer.h>
25
26 #include <linux/mmc/card.h>
27 #include <linux/mmc/host.h>
28 #include <linux/mmc/mmc.h>
29 #include <linux/mmc/sd.h>
30
31 #include "core.h"
32 #include "bus.h"
33 #include "host.h"
34 #include "sdio_bus.h"
35
36 #include "mmc_ops.h"
37 #include "sd_ops.h"
38 #include "sdio_ops.h"
39
40 static struct workqueue_struct *workqueue;
41
42 /*
43  * Enabling software CRCs on the data blocks can be a significant (30%)
44  * performance cost, and for other reasons may not always be desired.
45  * So we allow it it to be disabled.
46  */
47 int use_spi_crc = 1;
48 module_param(use_spi_crc, bool, 0);
49
50 /*
51  * We normally treat cards as removed during suspend if they are not
52  * known to be on a non-removable bus, to avoid the risk of writing
53  * back data to a different card after resume.  Allow this to be
54  * overridden if necessary.
55  */
56 #ifdef CONFIG_MMC_UNSAFE_RESUME
57 int mmc_assume_removable;
58 #else
59 int mmc_assume_removable = 1;
60 #endif
61 EXPORT_SYMBOL(mmc_assume_removable);
62 module_param_named(removable, mmc_assume_removable, bool, 0644);
63 MODULE_PARM_DESC(
64         removable,
65         "MMC/SD cards are removable and may be removed during suspend");
66
67 /*
68  * Internal function. Schedule delayed work in the MMC work queue.
69  */
70 static int mmc_schedule_delayed_work(struct delayed_work *work,
71                                      unsigned long delay)
72 {
73         return queue_delayed_work(workqueue, work, delay);
74 }
75
76 /*
77  * Internal function. Flush all scheduled work from the MMC work queue.
78  */
79 static void mmc_flush_scheduled_work(void)
80 {
81         flush_workqueue(workqueue);
82 }
83
84 /**
85  *      mmc_request_done - finish processing an MMC request
86  *      @host: MMC host which completed request
87  *      @mrq: MMC request which request
88  *
89  *      MMC drivers should call this function when they have completed
90  *      their processing of a request.
91  */
92 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
93 {
94         struct mmc_command *cmd = mrq->cmd;
95         int err = cmd->error;
96
97         if (err && cmd->retries && mmc_host_is_spi(host)) {
98                 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
99                         cmd->retries = 0;
100         }
101
102         if (err && cmd->retries) {
103                 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
104                         mmc_hostname(host), cmd->opcode, err);
105
106                 cmd->retries--;
107                 cmd->error = 0;
108                 host->ops->request(host, mrq);
109         } else {
110                 led_trigger_event(host->led, LED_OFF);
111
112                 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
113                         mmc_hostname(host), cmd->opcode, err,
114                         cmd->resp[0], cmd->resp[1],
115                         cmd->resp[2], cmd->resp[3]);
116
117                 if (mrq->data) {
118                         pr_debug("%s:     %d bytes transferred: %d\n",
119                                 mmc_hostname(host),
120                                 mrq->data->bytes_xfered, mrq->data->error);
121                 }
122
123                 if (mrq->stop) {
124                         pr_debug("%s:     (CMD%u): %d: %08x %08x %08x %08x\n",
125                                 mmc_hostname(host), mrq->stop->opcode,
126                                 mrq->stop->error,
127                                 mrq->stop->resp[0], mrq->stop->resp[1],
128                                 mrq->stop->resp[2], mrq->stop->resp[3]);
129                 }
130
131                 if (mrq->done)
132                         mrq->done(mrq);
133
134                 mmc_host_clk_gate(host);
135         }
136 }
137
138 EXPORT_SYMBOL(mmc_request_done);
139
140 static void
141 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
142 {
143 #ifdef CONFIG_MMC_DEBUG
144         unsigned int i, sz;
145         struct scatterlist *sg;
146 #endif
147
148         pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
149                  mmc_hostname(host), mrq->cmd->opcode,
150                  mrq->cmd->arg, mrq->cmd->flags);
151
152         if (mrq->data) {
153                 pr_debug("%s:     blksz %d blocks %d flags %08x "
154                         "tsac %d ms nsac %d\n",
155                         mmc_hostname(host), mrq->data->blksz,
156                         mrq->data->blocks, mrq->data->flags,
157                         mrq->data->timeout_ns / 1000000,
158                         mrq->data->timeout_clks);
159         }
160
161         if (mrq->stop) {
162                 pr_debug("%s:     CMD%u arg %08x flags %08x\n",
163                          mmc_hostname(host), mrq->stop->opcode,
164                          mrq->stop->arg, mrq->stop->flags);
165         }
166
167         WARN_ON(!host->claimed);
168
169         led_trigger_event(host->led, LED_FULL);
170
171         mrq->cmd->error = 0;
172         mrq->cmd->mrq = mrq;
173         if (mrq->data) {
174                 BUG_ON(mrq->data->blksz > host->max_blk_size);
175                 BUG_ON(mrq->data->blocks > host->max_blk_count);
176                 BUG_ON(mrq->data->blocks * mrq->data->blksz >
177                         host->max_req_size);
178
179 #ifdef CONFIG_MMC_DEBUG
180                 sz = 0;
181                 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
182                         sz += sg->length;
183                 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
184 #endif
185
186                 mrq->cmd->data = mrq->data;
187                 mrq->data->error = 0;
188                 mrq->data->mrq = mrq;
189                 if (mrq->stop) {
190                         mrq->data->stop = mrq->stop;
191                         mrq->stop->error = 0;
192                         mrq->stop->mrq = mrq;
193                 }
194         }
195         mmc_host_clk_ungate(host);
196         host->ops->request(host, mrq);
197 }
198
199 static void mmc_wait_done(struct mmc_request *mrq)
200 {
201         complete(mrq->done_data);
202 }
203
204 /**
205  *      mmc_wait_for_req - start a request and wait for completion
206  *      @host: MMC host to start command
207  *      @mrq: MMC request to start
208  *
209  *      Start a new MMC custom command request for a host, and wait
210  *      for the command to complete. Does not attempt to parse the
211  *      response.
212  */
213 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
214 {
215         DECLARE_COMPLETION_ONSTACK(complete);
216
217         mrq->done_data = &complete;
218         mrq->done = mmc_wait_done;
219
220         mmc_start_request(host, mrq);
221
222         wait_for_completion(&complete);
223 }
224
225 EXPORT_SYMBOL(mmc_wait_for_req);
226
227 /**
228  *      mmc_wait_for_cmd - start a command and wait for completion
229  *      @host: MMC host to start command
230  *      @cmd: MMC command to start
231  *      @retries: maximum number of retries
232  *
233  *      Start a new MMC command for a host, and wait for the command
234  *      to complete.  Return any error that occurred while the command
235  *      was executing.  Do not attempt to parse the response.
236  */
237 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
238 {
239         struct mmc_request mrq;
240
241         WARN_ON(!host->claimed);
242
243         memset(&mrq, 0, sizeof(struct mmc_request));
244
245         memset(cmd->resp, 0, sizeof(cmd->resp));
246         cmd->retries = retries;
247
248         mrq.cmd = cmd;
249         cmd->data = NULL;
250
251         mmc_wait_for_req(host, &mrq);
252
253         return cmd->error;
254 }
255
256 EXPORT_SYMBOL(mmc_wait_for_cmd);
257
258 /**
259  *      mmc_set_data_timeout - set the timeout for a data command
260  *      @data: data phase for command
261  *      @card: the MMC card associated with the data transfer
262  *
263  *      Computes the data timeout parameters according to the
264  *      correct algorithm given the card type.
265  */
266 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
267 {
268         unsigned int mult;
269
270         /*
271          * SDIO cards only define an upper 1 s limit on access.
272          */
273         if (mmc_card_sdio(card)) {
274                 data->timeout_ns = 1000000000;
275                 data->timeout_clks = 0;
276                 return;
277         }
278
279         /*
280          * SD cards use a 100 multiplier rather than 10
281          */
282         mult = mmc_card_sd(card) ? 100 : 10;
283
284         /*
285          * Scale up the multiplier (and therefore the timeout) by
286          * the r2w factor for writes.
287          */
288         if (data->flags & MMC_DATA_WRITE)
289                 mult <<= card->csd.r2w_factor;
290
291         data->timeout_ns = card->csd.tacc_ns * mult;
292         data->timeout_clks = card->csd.tacc_clks * mult;
293
294         /*
295          * SD cards also have an upper limit on the timeout.
296          */
297         if (mmc_card_sd(card)) {
298                 unsigned int timeout_us, limit_us;
299
300                 timeout_us = data->timeout_ns / 1000;
301                 timeout_us += data->timeout_clks * 1000 /
302                         (mmc_host_clk_rate(card->host) / 1000);
303
304                 if (data->flags & MMC_DATA_WRITE)
305                         /*
306                          * The limit is really 250 ms, but that is
307                          * insufficient for some crappy cards.
308                          */
309                         limit_us = 300000;
310                 else
311                         limit_us = 100000;
312
313                 /*
314                  * SDHC cards always use these fixed values.
315                  */
316                 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
317                         data->timeout_ns = limit_us * 1000;
318                         data->timeout_clks = 0;
319                 }
320         }
321         /*
322          * Some cards need very high timeouts if driven in SPI mode.
323          * The worst observed timeout was 900ms after writing a
324          * continuous stream of data until the internal logic
325          * overflowed.
326          */
327         if (mmc_host_is_spi(card->host)) {
328                 if (data->flags & MMC_DATA_WRITE) {
329                         if (data->timeout_ns < 1000000000)
330                                 data->timeout_ns = 1000000000;  /* 1s */
331                 } else {
332                         if (data->timeout_ns < 100000000)
333                                 data->timeout_ns =  100000000;  /* 100ms */
334                 }
335         }
336 }
337 EXPORT_SYMBOL(mmc_set_data_timeout);
338
339 /**
340  *      mmc_align_data_size - pads a transfer size to a more optimal value
341  *      @card: the MMC card associated with the data transfer
342  *      @sz: original transfer size
343  *
344  *      Pads the original data size with a number of extra bytes in
345  *      order to avoid controller bugs and/or performance hits
346  *      (e.g. some controllers revert to PIO for certain sizes).
347  *
348  *      Returns the improved size, which might be unmodified.
349  *
350  *      Note that this function is only relevant when issuing a
351  *      single scatter gather entry.
352  */
353 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
354 {
355         /*
356          * FIXME: We don't have a system for the controller to tell
357          * the core about its problems yet, so for now we just 32-bit
358          * align the size.
359          */
360         sz = ((sz + 3) / 4) * 4;
361
362         return sz;
363 }
364 EXPORT_SYMBOL(mmc_align_data_size);
365
366 /**
367  *      mmc_host_enable - enable a host.
368  *      @host: mmc host to enable
369  *
370  *      Hosts that support power saving can use the 'enable' and 'disable'
371  *      methods to exit and enter power saving states. For more information
372  *      see comments for struct mmc_host_ops.
373  */
374 int mmc_host_enable(struct mmc_host *host)
375 {
376         if (!(host->caps & MMC_CAP_DISABLE))
377                 return 0;
378
379         if (host->en_dis_recurs)
380                 return 0;
381
382         if (host->nesting_cnt++)
383                 return 0;
384
385         cancel_delayed_work_sync(&host->disable);
386
387         if (host->enabled)
388                 return 0;
389
390         if (host->ops->enable) {
391                 int err;
392
393                 host->en_dis_recurs = 1;
394                 err = host->ops->enable(host);
395                 host->en_dis_recurs = 0;
396
397                 if (err) {
398                         pr_debug("%s: enable error %d\n",
399                                  mmc_hostname(host), err);
400                         return err;
401                 }
402         }
403         host->enabled = 1;
404         return 0;
405 }
406 EXPORT_SYMBOL(mmc_host_enable);
407
408 static int mmc_host_do_disable(struct mmc_host *host, int lazy)
409 {
410         if (host->ops->disable) {
411                 int err;
412
413                 host->en_dis_recurs = 1;
414                 err = host->ops->disable(host, lazy);
415                 host->en_dis_recurs = 0;
416
417                 if (err < 0) {
418                         pr_debug("%s: disable error %d\n",
419                                  mmc_hostname(host), err);
420                         return err;
421                 }
422                 if (err > 0) {
423                         unsigned long delay = msecs_to_jiffies(err);
424
425                         mmc_schedule_delayed_work(&host->disable, delay);
426                 }
427         }
428         host->enabled = 0;
429         return 0;
430 }
431
432 /**
433  *      mmc_host_disable - disable a host.
434  *      @host: mmc host to disable
435  *
436  *      Hosts that support power saving can use the 'enable' and 'disable'
437  *      methods to exit and enter power saving states. For more information
438  *      see comments for struct mmc_host_ops.
439  */
440 int mmc_host_disable(struct mmc_host *host)
441 {
442         int err;
443
444         if (!(host->caps & MMC_CAP_DISABLE))
445                 return 0;
446
447         if (host->en_dis_recurs)
448                 return 0;
449
450         if (--host->nesting_cnt)
451                 return 0;
452
453         if (!host->enabled)
454                 return 0;
455
456         err = mmc_host_do_disable(host, 0);
457         return err;
458 }
459 EXPORT_SYMBOL(mmc_host_disable);
460
461 /**
462  *      __mmc_claim_host - exclusively claim a host
463  *      @host: mmc host to claim
464  *      @abort: whether or not the operation should be aborted
465  *
466  *      Claim a host for a set of operations.  If @abort is non null and
467  *      dereference a non-zero value then this will return prematurely with
468  *      that non-zero value without acquiring the lock.  Returns zero
469  *      with the lock held otherwise.
470  */
471 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
472 {
473         DECLARE_WAITQUEUE(wait, current);
474         unsigned long flags;
475         int stop;
476
477         might_sleep();
478
479         add_wait_queue(&host->wq, &wait);
480         spin_lock_irqsave(&host->lock, flags);
481         while (1) {
482                 set_current_state(TASK_UNINTERRUPTIBLE);
483                 stop = abort ? atomic_read(abort) : 0;
484                 if (stop || !host->claimed || host->claimer == current)
485                         break;
486                 spin_unlock_irqrestore(&host->lock, flags);
487                 schedule();
488                 spin_lock_irqsave(&host->lock, flags);
489         }
490         set_current_state(TASK_RUNNING);
491         if (!stop) {
492                 host->claimed = 1;
493                 host->claimer = current;
494                 host->claim_cnt += 1;
495         } else
496                 wake_up(&host->wq);
497         spin_unlock_irqrestore(&host->lock, flags);
498         remove_wait_queue(&host->wq, &wait);
499         if (!stop)
500                 mmc_host_enable(host);
501         return stop;
502 }
503
504 EXPORT_SYMBOL(__mmc_claim_host);
505
506 /**
507  *      mmc_try_claim_host - try exclusively to claim a host
508  *      @host: mmc host to claim
509  *
510  *      Returns %1 if the host is claimed, %0 otherwise.
511  */
512 int mmc_try_claim_host(struct mmc_host *host)
513 {
514         int claimed_host = 0;
515         unsigned long flags;
516
517         spin_lock_irqsave(&host->lock, flags);
518         if (!host->claimed || host->claimer == current) {
519                 host->claimed = 1;
520                 host->claimer = current;
521                 host->claim_cnt += 1;
522                 claimed_host = 1;
523         }
524         spin_unlock_irqrestore(&host->lock, flags);
525         return claimed_host;
526 }
527 EXPORT_SYMBOL(mmc_try_claim_host);
528
529 static void mmc_do_release_host(struct mmc_host *host)
530 {
531         unsigned long flags;
532
533         spin_lock_irqsave(&host->lock, flags);
534         if (--host->claim_cnt) {
535                 /* Release for nested claim */
536                 spin_unlock_irqrestore(&host->lock, flags);
537         } else {
538                 host->claimed = 0;
539                 host->claimer = NULL;
540                 spin_unlock_irqrestore(&host->lock, flags);
541                 wake_up(&host->wq);
542         }
543 }
544
545 void mmc_host_deeper_disable(struct work_struct *work)
546 {
547         struct mmc_host *host =
548                 container_of(work, struct mmc_host, disable.work);
549
550         /* If the host is claimed then we do not want to disable it anymore */
551         if (!mmc_try_claim_host(host))
552                 return;
553         mmc_host_do_disable(host, 1);
554         mmc_do_release_host(host);
555 }
556
557 /**
558  *      mmc_host_lazy_disable - lazily disable a host.
559  *      @host: mmc host to disable
560  *
561  *      Hosts that support power saving can use the 'enable' and 'disable'
562  *      methods to exit and enter power saving states. For more information
563  *      see comments for struct mmc_host_ops.
564  */
565 int mmc_host_lazy_disable(struct mmc_host *host)
566 {
567         if (!(host->caps & MMC_CAP_DISABLE))
568                 return 0;
569
570         if (host->en_dis_recurs)
571                 return 0;
572
573         if (--host->nesting_cnt)
574                 return 0;
575
576         if (!host->enabled)
577                 return 0;
578
579         if (host->disable_delay) {
580                 mmc_schedule_delayed_work(&host->disable,
581                                 msecs_to_jiffies(host->disable_delay));
582                 return 0;
583         } else
584                 return mmc_host_do_disable(host, 1);
585 }
586 EXPORT_SYMBOL(mmc_host_lazy_disable);
587
588 /**
589  *      mmc_release_host - release a host
590  *      @host: mmc host to release
591  *
592  *      Release a MMC host, allowing others to claim the host
593  *      for their operations.
594  */
595 void mmc_release_host(struct mmc_host *host)
596 {
597         WARN_ON(!host->claimed);
598
599         mmc_host_lazy_disable(host);
600
601         mmc_do_release_host(host);
602 }
603
604 EXPORT_SYMBOL(mmc_release_host);
605
606 /*
607  * Internal function that does the actual ios call to the host driver,
608  * optionally printing some debug output.
609  */
610 static inline void mmc_set_ios(struct mmc_host *host)
611 {
612         struct mmc_ios *ios = &host->ios;
613
614         pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
615                 "width %u timing %u\n",
616                  mmc_hostname(host), ios->clock, ios->bus_mode,
617                  ios->power_mode, ios->chip_select, ios->vdd,
618                  ios->bus_width, ios->timing);
619
620         if (ios->clock > 0)
621                 mmc_set_ungated(host);
622         host->ops->set_ios(host, ios);
623 }
624
625 /*
626  * Control chip select pin on a host.
627  */
628 void mmc_set_chip_select(struct mmc_host *host, int mode)
629 {
630         host->ios.chip_select = mode;
631         mmc_set_ios(host);
632 }
633
634 /*
635  * Sets the host clock to the highest possible frequency that
636  * is below "hz".
637  */
638 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
639 {
640         WARN_ON(hz < host->f_min);
641
642         if (hz > host->f_max)
643                 hz = host->f_max;
644
645         host->ios.clock = hz;
646         mmc_set_ios(host);
647 }
648
649 #ifdef CONFIG_MMC_CLKGATE
650 /*
651  * This gates the clock by setting it to 0 Hz.
652  */
653 void mmc_gate_clock(struct mmc_host *host)
654 {
655         unsigned long flags;
656
657         spin_lock_irqsave(&host->clk_lock, flags);
658         host->clk_old = host->ios.clock;
659         host->ios.clock = 0;
660         host->clk_gated = true;
661         spin_unlock_irqrestore(&host->clk_lock, flags);
662         mmc_set_ios(host);
663 }
664
665 /*
666  * This restores the clock from gating by using the cached
667  * clock value.
668  */
669 void mmc_ungate_clock(struct mmc_host *host)
670 {
671         /*
672          * We should previously have gated the clock, so the clock shall
673          * be 0 here! The clock may however be 0 during initialization,
674          * when some request operations are performed before setting
675          * the frequency. When ungate is requested in that situation
676          * we just ignore the call.
677          */
678         if (host->clk_old) {
679                 BUG_ON(host->ios.clock);
680                 /* This call will also set host->clk_gated to false */
681                 mmc_set_clock(host, host->clk_old);
682         }
683 }
684
685 void mmc_set_ungated(struct mmc_host *host)
686 {
687         unsigned long flags;
688
689         /*
690          * We've been given a new frequency while the clock is gated,
691          * so make sure we regard this as ungating it.
692          */
693         spin_lock_irqsave(&host->clk_lock, flags);
694         host->clk_gated = false;
695         spin_unlock_irqrestore(&host->clk_lock, flags);
696 }
697
698 #else
699 void mmc_set_ungated(struct mmc_host *host)
700 {
701 }
702 #endif
703
704 /*
705  * Change the bus mode (open drain/push-pull) of a host.
706  */
707 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
708 {
709         host->ios.bus_mode = mode;
710         mmc_set_ios(host);
711 }
712
713 /*
714  * Change data bus width and DDR mode of a host.
715  */
716 void mmc_set_bus_width_ddr(struct mmc_host *host, unsigned int width,
717                            unsigned int ddr)
718 {
719         host->ios.bus_width = width;
720         host->ios.ddr = ddr;
721         mmc_set_ios(host);
722 }
723
724 /*
725  * Change data bus width of a host.
726  */
727 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
728 {
729         mmc_set_bus_width_ddr(host, width, MMC_SDR_MODE);
730 }
731
732 /**
733  * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
734  * @vdd:        voltage (mV)
735  * @low_bits:   prefer low bits in boundary cases
736  *
737  * This function returns the OCR bit number according to the provided @vdd
738  * value. If conversion is not possible a negative errno value returned.
739  *
740  * Depending on the @low_bits flag the function prefers low or high OCR bits
741  * on boundary voltages. For example,
742  * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
743  * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
744  *
745  * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
746  */
747 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
748 {
749         const int max_bit = ilog2(MMC_VDD_35_36);
750         int bit;
751
752         if (vdd < 1650 || vdd > 3600)
753                 return -EINVAL;
754
755         if (vdd >= 1650 && vdd <= 1950)
756                 return ilog2(MMC_VDD_165_195);
757
758         if (low_bits)
759                 vdd -= 1;
760
761         /* Base 2000 mV, step 100 mV, bit's base 8. */
762         bit = (vdd - 2000) / 100 + 8;
763         if (bit > max_bit)
764                 return max_bit;
765         return bit;
766 }
767
768 /**
769  * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
770  * @vdd_min:    minimum voltage value (mV)
771  * @vdd_max:    maximum voltage value (mV)
772  *
773  * This function returns the OCR mask bits according to the provided @vdd_min
774  * and @vdd_max values. If conversion is not possible the function returns 0.
775  *
776  * Notes wrt boundary cases:
777  * This function sets the OCR bits for all boundary voltages, for example
778  * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
779  * MMC_VDD_34_35 mask.
780  */
781 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
782 {
783         u32 mask = 0;
784
785         if (vdd_max < vdd_min)
786                 return 0;
787
788         /* Prefer high bits for the boundary vdd_max values. */
789         vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
790         if (vdd_max < 0)
791                 return 0;
792
793         /* Prefer low bits for the boundary vdd_min values. */
794         vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
795         if (vdd_min < 0)
796                 return 0;
797
798         /* Fill the mask, from max bit to min bit. */
799         while (vdd_max >= vdd_min)
800                 mask |= 1 << vdd_max--;
801
802         return mask;
803 }
804 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
805
806 #ifdef CONFIG_REGULATOR
807
808 /**
809  * mmc_regulator_get_ocrmask - return mask of supported voltages
810  * @supply: regulator to use
811  *
812  * This returns either a negative errno, or a mask of voltages that
813  * can be provided to MMC/SD/SDIO devices using the specified voltage
814  * regulator.  This would normally be called before registering the
815  * MMC host adapter.
816  */
817 int mmc_regulator_get_ocrmask(struct regulator *supply)
818 {
819         int                     result = 0;
820         int                     count;
821         int                     i;
822
823         count = regulator_count_voltages(supply);
824         if (count < 0)
825                 return count;
826
827         for (i = 0; i < count; i++) {
828                 int             vdd_uV;
829                 int             vdd_mV;
830
831                 vdd_uV = regulator_list_voltage(supply, i);
832                 if (vdd_uV <= 0)
833                         continue;
834
835                 vdd_mV = vdd_uV / 1000;
836                 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
837         }
838
839         return result;
840 }
841 EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
842
843 /**
844  * mmc_regulator_set_ocr - set regulator to match host->ios voltage
845  * @mmc: the host to regulate
846  * @supply: regulator to use
847  * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
848  *
849  * Returns zero on success, else negative errno.
850  *
851  * MMC host drivers may use this to enable or disable a regulator using
852  * a particular supply voltage.  This would normally be called from the
853  * set_ios() method.
854  */
855 int mmc_regulator_set_ocr(struct mmc_host *mmc,
856                         struct regulator *supply,
857                         unsigned short vdd_bit)
858 {
859         int                     result = 0;
860         int                     min_uV, max_uV;
861
862         if (vdd_bit) {
863                 int             tmp;
864                 int             voltage;
865
866                 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
867                  * bits this regulator doesn't quite support ... don't
868                  * be too picky, most cards and regulators are OK with
869                  * a 0.1V range goof (it's a small error percentage).
870                  */
871                 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
872                 if (tmp == 0) {
873                         min_uV = 1650 * 1000;
874                         max_uV = 1950 * 1000;
875                 } else {
876                         min_uV = 1900 * 1000 + tmp * 100 * 1000;
877                         max_uV = min_uV + 100 * 1000;
878                 }
879
880                 /* avoid needless changes to this voltage; the regulator
881                  * might not allow this operation
882                  */
883                 voltage = regulator_get_voltage(supply);
884                 if (voltage < 0)
885                         result = voltage;
886                 else if (voltage < min_uV || voltage > max_uV)
887                         result = regulator_set_voltage(supply, min_uV, max_uV);
888                 else
889                         result = 0;
890
891                 if (result == 0 && !mmc->regulator_enabled) {
892                         result = regulator_enable(supply);
893                         if (!result)
894                                 mmc->regulator_enabled = true;
895                 }
896         } else if (mmc->regulator_enabled) {
897                 result = regulator_disable(supply);
898                 if (result == 0)
899                         mmc->regulator_enabled = false;
900         }
901
902         if (result)
903                 dev_err(mmc_dev(mmc),
904                         "could not set regulator OCR (%d)\n", result);
905         return result;
906 }
907 EXPORT_SYMBOL(mmc_regulator_set_ocr);
908
909 #endif /* CONFIG_REGULATOR */
910
911 /*
912  * Mask off any voltages we don't support and select
913  * the lowest voltage
914  */
915 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
916 {
917         int bit;
918
919         ocr &= host->ocr_avail;
920
921         bit = ffs(ocr);
922         if (bit) {
923                 bit -= 1;
924
925                 ocr &= 3 << bit;
926
927                 host->ios.vdd = bit;
928                 mmc_set_ios(host);
929         } else {
930                 pr_warning("%s: host doesn't support card's voltages\n",
931                                 mmc_hostname(host));
932                 ocr = 0;
933         }
934
935         return ocr;
936 }
937
938 /*
939  * Select timing parameters for host.
940  */
941 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
942 {
943         host->ios.timing = timing;
944         mmc_set_ios(host);
945 }
946
947 /*
948  * Apply power to the MMC stack.  This is a two-stage process.
949  * First, we enable power to the card without the clock running.
950  * We then wait a bit for the power to stabilise.  Finally,
951  * enable the bus drivers and clock to the card.
952  *
953  * We must _NOT_ enable the clock prior to power stablising.
954  *
955  * If a host does all the power sequencing itself, ignore the
956  * initial MMC_POWER_UP stage.
957  */
958 static void mmc_power_up(struct mmc_host *host)
959 {
960         int bit;
961
962         /* If ocr is set, we use it */
963         if (host->ocr)
964                 bit = ffs(host->ocr) - 1;
965         else
966                 bit = fls(host->ocr_avail) - 1;
967
968         host->ios.vdd = bit;
969         if (mmc_host_is_spi(host)) {
970                 host->ios.chip_select = MMC_CS_HIGH;
971                 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
972         } else {
973                 host->ios.chip_select = MMC_CS_DONTCARE;
974                 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
975         }
976         host->ios.power_mode = MMC_POWER_UP;
977         host->ios.bus_width = MMC_BUS_WIDTH_1;
978         host->ios.timing = MMC_TIMING_LEGACY;
979         mmc_set_ios(host);
980
981         /*
982          * This delay should be sufficient to allow the power supply
983          * to reach the minimum voltage.
984          */
985         mmc_delay(10);
986
987         host->ios.clock = host->f_init;
988
989         host->ios.power_mode = MMC_POWER_ON;
990         mmc_set_ios(host);
991
992         /*
993          * This delay must be at least 74 clock sizes, or 1 ms, or the
994          * time required to reach a stable voltage.
995          */
996         mmc_delay(10);
997 }
998
999 static void mmc_power_off(struct mmc_host *host)
1000 {
1001         host->ios.clock = 0;
1002         host->ios.vdd = 0;
1003         if (!mmc_host_is_spi(host)) {
1004                 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1005                 host->ios.chip_select = MMC_CS_DONTCARE;
1006         }
1007         host->ios.power_mode = MMC_POWER_OFF;
1008         host->ios.bus_width = MMC_BUS_WIDTH_1;
1009         host->ios.timing = MMC_TIMING_LEGACY;
1010         mmc_set_ios(host);
1011 }
1012
1013 /*
1014  * Cleanup when the last reference to the bus operator is dropped.
1015  */
1016 static void __mmc_release_bus(struct mmc_host *host)
1017 {
1018         BUG_ON(!host);
1019         BUG_ON(host->bus_refs);
1020         BUG_ON(!host->bus_dead);
1021
1022         host->bus_ops = NULL;
1023 }
1024
1025 /*
1026  * Increase reference count of bus operator
1027  */
1028 static inline void mmc_bus_get(struct mmc_host *host)
1029 {
1030         unsigned long flags;
1031
1032         spin_lock_irqsave(&host->lock, flags);
1033         host->bus_refs++;
1034         spin_unlock_irqrestore(&host->lock, flags);
1035 }
1036
1037 /*
1038  * Decrease reference count of bus operator and free it if
1039  * it is the last reference.
1040  */
1041 static inline void mmc_bus_put(struct mmc_host *host)
1042 {
1043         unsigned long flags;
1044
1045         spin_lock_irqsave(&host->lock, flags);
1046         host->bus_refs--;
1047         if ((host->bus_refs == 0) && host->bus_ops)
1048                 __mmc_release_bus(host);
1049         spin_unlock_irqrestore(&host->lock, flags);
1050 }
1051
1052 /*
1053  * Assign a mmc bus handler to a host. Only one bus handler may control a
1054  * host at any given time.
1055  */
1056 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1057 {
1058         unsigned long flags;
1059
1060         BUG_ON(!host);
1061         BUG_ON(!ops);
1062
1063         WARN_ON(!host->claimed);
1064
1065         spin_lock_irqsave(&host->lock, flags);
1066
1067         BUG_ON(host->bus_ops);
1068         BUG_ON(host->bus_refs);
1069
1070         host->bus_ops = ops;
1071         host->bus_refs = 1;
1072         host->bus_dead = 0;
1073
1074         spin_unlock_irqrestore(&host->lock, flags);
1075 }
1076
1077 /*
1078  * Remove the current bus handler from a host. Assumes that there are
1079  * no interesting cards left, so the bus is powered down.
1080  */
1081 void mmc_detach_bus(struct mmc_host *host)
1082 {
1083         unsigned long flags;
1084
1085         BUG_ON(!host);
1086
1087         WARN_ON(!host->claimed);
1088         WARN_ON(!host->bus_ops);
1089
1090         spin_lock_irqsave(&host->lock, flags);
1091
1092         host->bus_dead = 1;
1093
1094         spin_unlock_irqrestore(&host->lock, flags);
1095
1096         mmc_power_off(host);
1097
1098         mmc_bus_put(host);
1099 }
1100
1101 /**
1102  *      mmc_detect_change - process change of state on a MMC socket
1103  *      @host: host which changed state.
1104  *      @delay: optional delay to wait before detection (jiffies)
1105  *
1106  *      MMC drivers should call this when they detect a card has been
1107  *      inserted or removed. The MMC layer will confirm that any
1108  *      present card is still functional, and initialize any newly
1109  *      inserted.
1110  */
1111 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1112 {
1113 #ifdef CONFIG_MMC_DEBUG
1114         unsigned long flags;
1115         spin_lock_irqsave(&host->lock, flags);
1116         WARN_ON(host->removed);
1117         spin_unlock_irqrestore(&host->lock, flags);
1118 #endif
1119
1120         mmc_schedule_delayed_work(&host->detect, delay);
1121 }
1122
1123 EXPORT_SYMBOL(mmc_detect_change);
1124
1125 void mmc_init_erase(struct mmc_card *card)
1126 {
1127         unsigned int sz;
1128
1129         if (is_power_of_2(card->erase_size))
1130                 card->erase_shift = ffs(card->erase_size) - 1;
1131         else
1132                 card->erase_shift = 0;
1133
1134         /*
1135          * It is possible to erase an arbitrarily large area of an SD or MMC
1136          * card.  That is not desirable because it can take a long time
1137          * (minutes) potentially delaying more important I/O, and also the
1138          * timeout calculations become increasingly hugely over-estimated.
1139          * Consequently, 'pref_erase' is defined as a guide to limit erases
1140          * to that size and alignment.
1141          *
1142          * For SD cards that define Allocation Unit size, limit erases to one
1143          * Allocation Unit at a time.  For MMC cards that define High Capacity
1144          * Erase Size, whether it is switched on or not, limit to that size.
1145          * Otherwise just have a stab at a good value.  For modern cards it
1146          * will end up being 4MiB.  Note that if the value is too small, it
1147          * can end up taking longer to erase.
1148          */
1149         if (mmc_card_sd(card) && card->ssr.au) {
1150                 card->pref_erase = card->ssr.au;
1151                 card->erase_shift = ffs(card->ssr.au) - 1;
1152         } else if (card->ext_csd.hc_erase_size) {
1153                 card->pref_erase = card->ext_csd.hc_erase_size;
1154         } else {
1155                 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1156                 if (sz < 128)
1157                         card->pref_erase = 512 * 1024 / 512;
1158                 else if (sz < 512)
1159                         card->pref_erase = 1024 * 1024 / 512;
1160                 else if (sz < 1024)
1161                         card->pref_erase = 2 * 1024 * 1024 / 512;
1162                 else
1163                         card->pref_erase = 4 * 1024 * 1024 / 512;
1164                 if (card->pref_erase < card->erase_size)
1165                         card->pref_erase = card->erase_size;
1166                 else {
1167                         sz = card->pref_erase % card->erase_size;
1168                         if (sz)
1169                                 card->pref_erase += card->erase_size - sz;
1170                 }
1171         }
1172 }
1173
1174 static void mmc_set_mmc_erase_timeout(struct mmc_card *card,
1175                                       struct mmc_command *cmd,
1176                                       unsigned int arg, unsigned int qty)
1177 {
1178         unsigned int erase_timeout;
1179
1180         if (card->ext_csd.erase_group_def & 1) {
1181                 /* High Capacity Erase Group Size uses HC timeouts */
1182                 if (arg == MMC_TRIM_ARG)
1183                         erase_timeout = card->ext_csd.trim_timeout;
1184                 else
1185                         erase_timeout = card->ext_csd.hc_erase_timeout;
1186         } else {
1187                 /* CSD Erase Group Size uses write timeout */
1188                 unsigned int mult = (10 << card->csd.r2w_factor);
1189                 unsigned int timeout_clks = card->csd.tacc_clks * mult;
1190                 unsigned int timeout_us;
1191
1192                 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1193                 if (card->csd.tacc_ns < 1000000)
1194                         timeout_us = (card->csd.tacc_ns * mult) / 1000;
1195                 else
1196                         timeout_us = (card->csd.tacc_ns / 1000) * mult;
1197
1198                 /*
1199                  * ios.clock is only a target.  The real clock rate might be
1200                  * less but not that much less, so fudge it by multiplying by 2.
1201                  */
1202                 timeout_clks <<= 1;
1203                 timeout_us += (timeout_clks * 1000) /
1204                               (card->host->ios.clock / 1000);
1205
1206                 erase_timeout = timeout_us / 1000;
1207
1208                 /*
1209                  * Theoretically, the calculation could underflow so round up
1210                  * to 1ms in that case.
1211                  */
1212                 if (!erase_timeout)
1213                         erase_timeout = 1;
1214         }
1215
1216         /* Multiplier for secure operations */
1217         if (arg & MMC_SECURE_ARGS) {
1218                 if (arg == MMC_SECURE_ERASE_ARG)
1219                         erase_timeout *= card->ext_csd.sec_erase_mult;
1220                 else
1221                         erase_timeout *= card->ext_csd.sec_trim_mult;
1222         }
1223
1224         erase_timeout *= qty;
1225
1226         /*
1227          * Ensure at least a 1 second timeout for SPI as per
1228          * 'mmc_set_data_timeout()'
1229          */
1230         if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1231                 erase_timeout = 1000;
1232
1233         cmd->erase_timeout = erase_timeout;
1234 }
1235
1236 static void mmc_set_sd_erase_timeout(struct mmc_card *card,
1237                                      struct mmc_command *cmd, unsigned int arg,
1238                                      unsigned int qty)
1239 {
1240         if (card->ssr.erase_timeout) {
1241                 /* Erase timeout specified in SD Status Register (SSR) */
1242                 cmd->erase_timeout = card->ssr.erase_timeout * qty +
1243                                      card->ssr.erase_offset;
1244         } else {
1245                 /*
1246                  * Erase timeout not specified in SD Status Register (SSR) so
1247                  * use 250ms per write block.
1248                  */
1249                 cmd->erase_timeout = 250 * qty;
1250         }
1251
1252         /* Must not be less than 1 second */
1253         if (cmd->erase_timeout < 1000)
1254                 cmd->erase_timeout = 1000;
1255 }
1256
1257 static void mmc_set_erase_timeout(struct mmc_card *card,
1258                                   struct mmc_command *cmd, unsigned int arg,
1259                                   unsigned int qty)
1260 {
1261         if (mmc_card_sd(card))
1262                 mmc_set_sd_erase_timeout(card, cmd, arg, qty);
1263         else
1264                 mmc_set_mmc_erase_timeout(card, cmd, arg, qty);
1265 }
1266
1267 static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1268                         unsigned int to, unsigned int arg)
1269 {
1270         struct mmc_command cmd;
1271         unsigned int qty = 0;
1272         int err;
1273
1274         /*
1275          * qty is used to calculate the erase timeout which depends on how many
1276          * erase groups (or allocation units in SD terminology) are affected.
1277          * We count erasing part of an erase group as one erase group.
1278          * For SD, the allocation units are always a power of 2.  For MMC, the
1279          * erase group size is almost certainly also power of 2, but it does not
1280          * seem to insist on that in the JEDEC standard, so we fall back to
1281          * division in that case.  SD may not specify an allocation unit size,
1282          * in which case the timeout is based on the number of write blocks.
1283          *
1284          * Note that the timeout for secure trim 2 will only be correct if the
1285          * number of erase groups specified is the same as the total of all
1286          * preceding secure trim 1 commands.  Since the power may have been
1287          * lost since the secure trim 1 commands occurred, it is generally
1288          * impossible to calculate the secure trim 2 timeout correctly.
1289          */
1290         if (card->erase_shift)
1291                 qty += ((to >> card->erase_shift) -
1292                         (from >> card->erase_shift)) + 1;
1293         else if (mmc_card_sd(card))
1294                 qty += to - from + 1;
1295         else
1296                 qty += ((to / card->erase_size) -
1297                         (from / card->erase_size)) + 1;
1298
1299         if (!mmc_card_blockaddr(card)) {
1300                 from <<= 9;
1301                 to <<= 9;
1302         }
1303
1304         memset(&cmd, 0, sizeof(struct mmc_command));
1305         if (mmc_card_sd(card))
1306                 cmd.opcode = SD_ERASE_WR_BLK_START;
1307         else
1308                 cmd.opcode = MMC_ERASE_GROUP_START;
1309         cmd.arg = from;
1310         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1311         err = mmc_wait_for_cmd(card->host, &cmd, 0);
1312         if (err) {
1313                 printk(KERN_ERR "mmc_erase: group start error %d, "
1314                        "status %#x\n", err, cmd.resp[0]);
1315                 err = -EINVAL;
1316                 goto out;
1317         }
1318
1319         memset(&cmd, 0, sizeof(struct mmc_command));
1320         if (mmc_card_sd(card))
1321                 cmd.opcode = SD_ERASE_WR_BLK_END;
1322         else
1323                 cmd.opcode = MMC_ERASE_GROUP_END;
1324         cmd.arg = to;
1325         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1326         err = mmc_wait_for_cmd(card->host, &cmd, 0);
1327         if (err) {
1328                 printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n",
1329                        err, cmd.resp[0]);
1330                 err = -EINVAL;
1331                 goto out;
1332         }
1333
1334         memset(&cmd, 0, sizeof(struct mmc_command));
1335         cmd.opcode = MMC_ERASE;
1336         cmd.arg = arg;
1337         cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1338         mmc_set_erase_timeout(card, &cmd, arg, qty);
1339         err = mmc_wait_for_cmd(card->host, &cmd, 0);
1340         if (err) {
1341                 printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n",
1342                        err, cmd.resp[0]);
1343                 err = -EIO;
1344                 goto out;
1345         }
1346
1347         if (mmc_host_is_spi(card->host))
1348                 goto out;
1349
1350         do {
1351                 memset(&cmd, 0, sizeof(struct mmc_command));
1352                 cmd.opcode = MMC_SEND_STATUS;
1353                 cmd.arg = card->rca << 16;
1354                 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1355                 /* Do not retry else we can't see errors */
1356                 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1357                 if (err || (cmd.resp[0] & 0xFDF92000)) {
1358                         printk(KERN_ERR "error %d requesting status %#x\n",
1359                                 err, cmd.resp[0]);
1360                         err = -EIO;
1361                         goto out;
1362                 }
1363         } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
1364                  R1_CURRENT_STATE(cmd.resp[0]) == 7);
1365 out:
1366         return err;
1367 }
1368
1369 /**
1370  * mmc_erase - erase sectors.
1371  * @card: card to erase
1372  * @from: first sector to erase
1373  * @nr: number of sectors to erase
1374  * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
1375  *
1376  * Caller must claim host before calling this function.
1377  */
1378 int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
1379               unsigned int arg)
1380 {
1381         unsigned int rem, to = from + nr;
1382
1383         if (!(card->host->caps & MMC_CAP_ERASE) ||
1384             !(card->csd.cmdclass & CCC_ERASE))
1385                 return -EOPNOTSUPP;
1386
1387         if (!card->erase_size)
1388                 return -EOPNOTSUPP;
1389
1390         if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
1391                 return -EOPNOTSUPP;
1392
1393         if ((arg & MMC_SECURE_ARGS) &&
1394             !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
1395                 return -EOPNOTSUPP;
1396
1397         if ((arg & MMC_TRIM_ARGS) &&
1398             !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
1399                 return -EOPNOTSUPP;
1400
1401         if (arg == MMC_SECURE_ERASE_ARG) {
1402                 if (from % card->erase_size || nr % card->erase_size)
1403                         return -EINVAL;
1404         }
1405
1406         if (arg == MMC_ERASE_ARG) {
1407                 rem = from % card->erase_size;
1408                 if (rem) {
1409                         rem = card->erase_size - rem;
1410                         from += rem;
1411                         if (nr > rem)
1412                                 nr -= rem;
1413                         else
1414                                 return 0;
1415                 }
1416                 rem = nr % card->erase_size;
1417                 if (rem)
1418                         nr -= rem;
1419         }
1420
1421         if (nr == 0)
1422                 return 0;
1423
1424         to = from + nr;
1425
1426         if (to <= from)
1427                 return -EINVAL;
1428
1429         /* 'from' and 'to' are inclusive */
1430         to -= 1;
1431
1432         return mmc_do_erase(card, from, to, arg);
1433 }
1434 EXPORT_SYMBOL(mmc_erase);
1435
1436 int mmc_can_erase(struct mmc_card *card)
1437 {
1438         if ((card->host->caps & MMC_CAP_ERASE) &&
1439             (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
1440                 return 1;
1441         return 0;
1442 }
1443 EXPORT_SYMBOL(mmc_can_erase);
1444
1445 int mmc_can_trim(struct mmc_card *card)
1446 {
1447         if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
1448                 return 1;
1449         return 0;
1450 }
1451 EXPORT_SYMBOL(mmc_can_trim);
1452
1453 int mmc_can_secure_erase_trim(struct mmc_card *card)
1454 {
1455         if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
1456                 return 1;
1457         return 0;
1458 }
1459 EXPORT_SYMBOL(mmc_can_secure_erase_trim);
1460
1461 int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
1462                             unsigned int nr)
1463 {
1464         if (!card->erase_size)
1465                 return 0;
1466         if (from % card->erase_size || nr % card->erase_size)
1467                 return 0;
1468         return 1;
1469 }
1470 EXPORT_SYMBOL(mmc_erase_group_aligned);
1471
1472 int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
1473 {
1474         struct mmc_command cmd;
1475
1476         if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
1477                 return 0;
1478
1479         memset(&cmd, 0, sizeof(struct mmc_command));
1480         cmd.opcode = MMC_SET_BLOCKLEN;
1481         cmd.arg = blocklen;
1482         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1483         return mmc_wait_for_cmd(card->host, &cmd, 5);
1484 }
1485 EXPORT_SYMBOL(mmc_set_blocklen);
1486
1487 void mmc_rescan(struct work_struct *work)
1488 {
1489         struct mmc_host *host =
1490                 container_of(work, struct mmc_host, detect.work);
1491         u32 ocr;
1492         int err;
1493         unsigned long flags;
1494         int i;
1495         const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
1496
1497         spin_lock_irqsave(&host->lock, flags);
1498
1499         if (host->rescan_disable) {
1500                 spin_unlock_irqrestore(&host->lock, flags);
1501                 return;
1502         }
1503
1504         spin_unlock_irqrestore(&host->lock, flags);
1505
1506
1507         mmc_bus_get(host);
1508
1509         /*
1510          * if there is a _removable_ card registered, check whether it is
1511          * still present
1512          */
1513         if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
1514             && mmc_card_is_removable(host))
1515                 host->bus_ops->detect(host);
1516
1517         mmc_bus_put(host);
1518
1519
1520         mmc_bus_get(host);
1521
1522         /* if there still is a card present, stop here */
1523         if (host->bus_ops != NULL) {
1524                 mmc_bus_put(host);
1525                 goto out;
1526         }
1527
1528         /* detect a newly inserted card */
1529
1530         /*
1531          * Only we can add a new handler, so it's safe to
1532          * release the lock here.
1533          */
1534         mmc_bus_put(host);
1535
1536         if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1537                 goto out;
1538
1539         for (i = 0; i < ARRAY_SIZE(freqs); i++) {
1540                 mmc_claim_host(host);
1541
1542                 if (freqs[i] >= host->f_min)
1543                         host->f_init = freqs[i];
1544                 else if (!i || freqs[i-1] > host->f_min)
1545                         host->f_init = host->f_min;
1546                 else {
1547                         mmc_release_host(host);
1548                         goto out;
1549                 }
1550 #ifdef CONFIG_MMC_DEBUG
1551                 pr_info("%s: %s: trying to init card at %u Hz\n",
1552                         mmc_hostname(host), __func__, host->f_init);
1553 #endif
1554                 mmc_power_up(host);
1555                 sdio_reset(host);
1556                 mmc_go_idle(host);
1557
1558                 mmc_send_if_cond(host, host->ocr_avail);
1559
1560                 /*
1561                  * First we search for SDIO...
1562                  */
1563                 err = mmc_send_io_op_cond(host, 0, &ocr);
1564                 if (!err) {
1565                         if (mmc_attach_sdio(host, ocr)) {
1566                                 mmc_claim_host(host);
1567                                 /*
1568                                  * Try SDMEM (but not MMC) even if SDIO
1569                                  * is broken.
1570                                  */
1571                                 if (mmc_send_app_op_cond(host, 0, &ocr))
1572                                         goto out_fail;
1573
1574                                 if (mmc_attach_sd(host, ocr))
1575                                         mmc_power_off(host);
1576                         }
1577                         goto out;
1578                 }
1579
1580                 /*
1581                  * ...then normal SD...
1582                  */
1583                 err = mmc_send_app_op_cond(host, 0, &ocr);
1584                 if (!err) {
1585                         if (mmc_attach_sd(host, ocr))
1586                                 mmc_power_off(host);
1587                         goto out;
1588                 }
1589
1590                 /*
1591                  * ...and finally MMC.
1592                  */
1593                 err = mmc_send_op_cond(host, 0, &ocr);
1594                 if (!err) {
1595                         if (mmc_attach_mmc(host, ocr))
1596                                 mmc_power_off(host);
1597                         goto out;
1598                 }
1599
1600 out_fail:
1601                 mmc_release_host(host);
1602                 mmc_power_off(host);
1603         }
1604 out:
1605         if (host->caps & MMC_CAP_NEEDS_POLL)
1606                 mmc_schedule_delayed_work(&host->detect, HZ);
1607 }
1608
1609 void mmc_start_host(struct mmc_host *host)
1610 {
1611         mmc_power_off(host);
1612         mmc_detect_change(host, 0);
1613 }
1614
1615 void mmc_stop_host(struct mmc_host *host)
1616 {
1617 #ifdef CONFIG_MMC_DEBUG
1618         unsigned long flags;
1619         spin_lock_irqsave(&host->lock, flags);
1620         host->removed = 1;
1621         spin_unlock_irqrestore(&host->lock, flags);
1622 #endif
1623
1624         if (host->caps & MMC_CAP_DISABLE)
1625                 cancel_delayed_work(&host->disable);
1626         cancel_delayed_work_sync(&host->detect);
1627         mmc_flush_scheduled_work();
1628
1629         /* clear pm flags now and let card drivers set them as needed */
1630         host->pm_flags = 0;
1631
1632         mmc_bus_get(host);
1633         if (host->bus_ops && !host->bus_dead) {
1634                 if (host->bus_ops->remove)
1635                         host->bus_ops->remove(host);
1636
1637                 mmc_claim_host(host);
1638                 mmc_detach_bus(host);
1639                 mmc_release_host(host);
1640                 mmc_bus_put(host);
1641                 return;
1642         }
1643         mmc_bus_put(host);
1644
1645         BUG_ON(host->card);
1646
1647         mmc_power_off(host);
1648 }
1649
1650 int mmc_power_save_host(struct mmc_host *host)
1651 {
1652         int ret = 0;
1653
1654         mmc_bus_get(host);
1655
1656         if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1657                 mmc_bus_put(host);
1658                 return -EINVAL;
1659         }
1660
1661         if (host->bus_ops->power_save)
1662                 ret = host->bus_ops->power_save(host);
1663
1664         mmc_bus_put(host);
1665
1666         mmc_power_off(host);
1667
1668         return ret;
1669 }
1670 EXPORT_SYMBOL(mmc_power_save_host);
1671
1672 int mmc_power_restore_host(struct mmc_host *host)
1673 {
1674         int ret;
1675
1676         mmc_bus_get(host);
1677
1678         if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1679                 mmc_bus_put(host);
1680                 return -EINVAL;
1681         }
1682
1683         mmc_power_up(host);
1684         ret = host->bus_ops->power_restore(host);
1685
1686         mmc_bus_put(host);
1687
1688         return ret;
1689 }
1690 EXPORT_SYMBOL(mmc_power_restore_host);
1691
1692 int mmc_card_awake(struct mmc_host *host)
1693 {
1694         int err = -ENOSYS;
1695
1696         mmc_bus_get(host);
1697
1698         if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1699                 err = host->bus_ops->awake(host);
1700
1701         mmc_bus_put(host);
1702
1703         return err;
1704 }
1705 EXPORT_SYMBOL(mmc_card_awake);
1706
1707 int mmc_card_sleep(struct mmc_host *host)
1708 {
1709         int err = -ENOSYS;
1710
1711         mmc_bus_get(host);
1712
1713         if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1714                 err = host->bus_ops->sleep(host);
1715
1716         mmc_bus_put(host);
1717
1718         return err;
1719 }
1720 EXPORT_SYMBOL(mmc_card_sleep);
1721
1722 int mmc_card_can_sleep(struct mmc_host *host)
1723 {
1724         struct mmc_card *card = host->card;
1725
1726         if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
1727                 return 1;
1728         return 0;
1729 }
1730 EXPORT_SYMBOL(mmc_card_can_sleep);
1731
1732 #ifdef CONFIG_PM
1733
1734 /**
1735  *      mmc_suspend_host - suspend a host
1736  *      @host: mmc host
1737  */
1738 int mmc_suspend_host(struct mmc_host *host)
1739 {
1740         int err = 0;
1741
1742         if (host->caps & MMC_CAP_DISABLE)
1743                 cancel_delayed_work(&host->disable);
1744         cancel_delayed_work(&host->detect);
1745         mmc_flush_scheduled_work();
1746
1747         mmc_bus_get(host);
1748         if (host->bus_ops && !host->bus_dead) {
1749                 if (host->bus_ops->suspend)
1750                         err = host->bus_ops->suspend(host);
1751                 if (err == -ENOSYS || !host->bus_ops->resume) {
1752                         /*
1753                          * We simply "remove" the card in this case.
1754                          * It will be redetected on resume.
1755                          */
1756                         if (host->bus_ops->remove)
1757                                 host->bus_ops->remove(host);
1758                         mmc_claim_host(host);
1759                         mmc_detach_bus(host);
1760                         mmc_release_host(host);
1761                         host->pm_flags = 0;
1762                         err = 0;
1763                 }
1764         }
1765         mmc_bus_put(host);
1766
1767         if (!err && !(host->pm_flags & MMC_PM_KEEP_POWER))
1768                 mmc_power_off(host);
1769
1770         return err;
1771 }
1772
1773 EXPORT_SYMBOL(mmc_suspend_host);
1774
1775 /**
1776  *      mmc_resume_host - resume a previously suspended host
1777  *      @host: mmc host
1778  */
1779 int mmc_resume_host(struct mmc_host *host)
1780 {
1781         int err = 0;
1782
1783         mmc_bus_get(host);
1784         if (host->bus_ops && !host->bus_dead) {
1785                 if (!(host->pm_flags & MMC_PM_KEEP_POWER)) {
1786                         mmc_power_up(host);
1787                         mmc_select_voltage(host, host->ocr);
1788                 }
1789                 BUG_ON(!host->bus_ops->resume);
1790                 err = host->bus_ops->resume(host);
1791                 if (err) {
1792                         printk(KERN_WARNING "%s: error %d during resume "
1793                                             "(card was removed?)\n",
1794                                             mmc_hostname(host), err);
1795                         err = 0;
1796                 }
1797         }
1798         mmc_bus_put(host);
1799
1800         return err;
1801 }
1802 EXPORT_SYMBOL(mmc_resume_host);
1803
1804 /* Do the card removal on suspend if card is assumed removeable
1805  * Do that in pm notifier while userspace isn't yet frozen, so we will be able
1806    to sync the card.
1807 */
1808 int mmc_pm_notify(struct notifier_block *notify_block,
1809                                         unsigned long mode, void *unused)
1810 {
1811         struct mmc_host *host = container_of(
1812                 notify_block, struct mmc_host, pm_notify);
1813         unsigned long flags;
1814
1815
1816         switch (mode) {
1817         case PM_HIBERNATION_PREPARE:
1818         case PM_SUSPEND_PREPARE:
1819
1820                 spin_lock_irqsave(&host->lock, flags);
1821                 host->rescan_disable = 1;
1822                 spin_unlock_irqrestore(&host->lock, flags);
1823                 cancel_delayed_work_sync(&host->detect);
1824
1825                 if (!host->bus_ops || host->bus_ops->suspend)
1826                         break;
1827
1828                 mmc_claim_host(host);
1829
1830                 if (host->bus_ops->remove)
1831                         host->bus_ops->remove(host);
1832
1833                 mmc_detach_bus(host);
1834                 mmc_release_host(host);
1835                 host->pm_flags = 0;
1836                 break;
1837
1838         case PM_POST_SUSPEND:
1839         case PM_POST_HIBERNATION:
1840         case PM_POST_RESTORE:
1841
1842                 spin_lock_irqsave(&host->lock, flags);
1843                 host->rescan_disable = 0;
1844                 spin_unlock_irqrestore(&host->lock, flags);
1845                 mmc_detect_change(host, 0);
1846
1847         }
1848
1849         return 0;
1850 }
1851 #endif
1852
1853 static int __init mmc_init(void)
1854 {
1855         int ret;
1856
1857         workqueue = alloc_ordered_workqueue("kmmcd", 0);
1858         if (!workqueue)
1859                 return -ENOMEM;
1860
1861         ret = mmc_register_bus();
1862         if (ret)
1863                 goto destroy_workqueue;
1864
1865         ret = mmc_register_host_class();
1866         if (ret)
1867                 goto unregister_bus;
1868
1869         ret = sdio_register_bus();
1870         if (ret)
1871                 goto unregister_host_class;
1872
1873         return 0;
1874
1875 unregister_host_class:
1876         mmc_unregister_host_class();
1877 unregister_bus:
1878         mmc_unregister_bus();
1879 destroy_workqueue:
1880         destroy_workqueue(workqueue);
1881
1882         return ret;
1883 }
1884
1885 static void __exit mmc_exit(void)
1886 {
1887         sdio_unregister_bus();
1888         mmc_unregister_host_class();
1889         mmc_unregister_bus();
1890         destroy_workqueue(workqueue);
1891 }
1892
1893 subsys_initcall(mmc_init);
1894 module_exit(mmc_exit);
1895
1896 MODULE_LICENSE("GPL");