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