sdio: allow for mmc_claim_host to be aborted
[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 / 10000000,
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          * SD cards use a 100 multiplier rather than 10
236          */
237         mult = mmc_card_sd(card) ? 100 : 10;
238
239         /*
240          * Scale up the multiplier (and therefore the timeout) by
241          * the r2w factor for writes.
242          */
243         if (data->flags & MMC_DATA_WRITE)
244                 mult <<= card->csd.r2w_factor;
245
246         data->timeout_ns = card->csd.tacc_ns * mult;
247         data->timeout_clks = card->csd.tacc_clks * mult;
248
249         /*
250          * SD cards also have an upper limit on the timeout.
251          */
252         if (mmc_card_sd(card)) {
253                 unsigned int timeout_us, limit_us;
254
255                 timeout_us = data->timeout_ns / 1000;
256                 timeout_us += data->timeout_clks * 1000 /
257                         (card->host->ios.clock / 1000);
258
259                 if (data->flags & MMC_DATA_WRITE)
260                         limit_us = 250000;
261                 else
262                         limit_us = 100000;
263
264                 /*
265                  * SDHC cards always use these fixed values.
266                  */
267                 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
268                         data->timeout_ns = limit_us * 1000;
269                         data->timeout_clks = 0;
270                 }
271         }
272 }
273 EXPORT_SYMBOL(mmc_set_data_timeout);
274
275 /**
276  *      __mmc_claim_host - exclusively claim a host
277  *      @host: mmc host to claim
278  *      @abort: whether or not the operation should be aborted
279  *
280  *      Claim a host for a set of operations.  If @abort is non null and
281  *      dereference a non-zero value then this will return prematurely with
282  *      that non-zero value without acquiring the lock.  Returns zero
283  *      with the lock held otherwise.
284  */
285 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
286 {
287         DECLARE_WAITQUEUE(wait, current);
288         unsigned long flags;
289         int stop;
290
291         might_sleep();
292
293         add_wait_queue(&host->wq, &wait);
294         spin_lock_irqsave(&host->lock, flags);
295         while (1) {
296                 set_current_state(TASK_UNINTERRUPTIBLE);
297                 stop = abort ? atomic_read(abort) : 0;
298                 if (stop || !host->claimed)
299                         break;
300                 spin_unlock_irqrestore(&host->lock, flags);
301                 schedule();
302                 spin_lock_irqsave(&host->lock, flags);
303         }
304         set_current_state(TASK_RUNNING);
305         if (!stop)
306                 host->claimed = 1;
307         else
308                 wake_up(&host->wq);
309         spin_unlock_irqrestore(&host->lock, flags);
310         remove_wait_queue(&host->wq, &wait);
311         return stop;
312 }
313
314 EXPORT_SYMBOL(__mmc_claim_host);
315
316 /**
317  *      mmc_release_host - release a host
318  *      @host: mmc host to release
319  *
320  *      Release a MMC host, allowing others to claim the host
321  *      for their operations.
322  */
323 void mmc_release_host(struct mmc_host *host)
324 {
325         unsigned long flags;
326
327         BUG_ON(!host->claimed);
328
329         spin_lock_irqsave(&host->lock, flags);
330         host->claimed = 0;
331         spin_unlock_irqrestore(&host->lock, flags);
332
333         wake_up(&host->wq);
334 }
335
336 EXPORT_SYMBOL(mmc_release_host);
337
338 /*
339  * Internal function that does the actual ios call to the host driver,
340  * optionally printing some debug output.
341  */
342 static inline void mmc_set_ios(struct mmc_host *host)
343 {
344         struct mmc_ios *ios = &host->ios;
345
346         pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
347                 "width %u timing %u\n",
348                  mmc_hostname(host), ios->clock, ios->bus_mode,
349                  ios->power_mode, ios->chip_select, ios->vdd,
350                  ios->bus_width, ios->timing);
351
352         host->ops->set_ios(host, ios);
353 }
354
355 /*
356  * Control chip select pin on a host.
357  */
358 void mmc_set_chip_select(struct mmc_host *host, int mode)
359 {
360         host->ios.chip_select = mode;
361         mmc_set_ios(host);
362 }
363
364 /*
365  * Sets the host clock to the highest possible frequency that
366  * is below "hz".
367  */
368 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
369 {
370         WARN_ON(hz < host->f_min);
371
372         if (hz > host->f_max)
373                 hz = host->f_max;
374
375         host->ios.clock = hz;
376         mmc_set_ios(host);
377 }
378
379 /*
380  * Change the bus mode (open drain/push-pull) of a host.
381  */
382 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
383 {
384         host->ios.bus_mode = mode;
385         mmc_set_ios(host);
386 }
387
388 /*
389  * Change data bus width of a host.
390  */
391 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
392 {
393         host->ios.bus_width = width;
394         mmc_set_ios(host);
395 }
396
397 /*
398  * Mask off any voltages we don't support and select
399  * the lowest voltage
400  */
401 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
402 {
403         int bit;
404
405         ocr &= host->ocr_avail;
406
407         bit = ffs(ocr);
408         if (bit) {
409                 bit -= 1;
410
411                 ocr &= 3 << bit;
412
413                 host->ios.vdd = bit;
414                 mmc_set_ios(host);
415         } else {
416                 ocr = 0;
417         }
418
419         return ocr;
420 }
421
422 /*
423  * Select timing parameters for host.
424  */
425 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
426 {
427         host->ios.timing = timing;
428         mmc_set_ios(host);
429 }
430
431 /*
432  * Apply power to the MMC stack.  This is a two-stage process.
433  * First, we enable power to the card without the clock running.
434  * We then wait a bit for the power to stabilise.  Finally,
435  * enable the bus drivers and clock to the card.
436  *
437  * We must _NOT_ enable the clock prior to power stablising.
438  *
439  * If a host does all the power sequencing itself, ignore the
440  * initial MMC_POWER_UP stage.
441  */
442 static void mmc_power_up(struct mmc_host *host)
443 {
444         int bit = fls(host->ocr_avail) - 1;
445
446         host->ios.vdd = bit;
447         host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
448         host->ios.chip_select = MMC_CS_DONTCARE;
449         host->ios.power_mode = MMC_POWER_UP;
450         host->ios.bus_width = MMC_BUS_WIDTH_1;
451         host->ios.timing = MMC_TIMING_LEGACY;
452         mmc_set_ios(host);
453
454         mmc_delay(1);
455
456         host->ios.clock = host->f_min;
457         host->ios.power_mode = MMC_POWER_ON;
458         mmc_set_ios(host);
459
460         mmc_delay(2);
461 }
462
463 static void mmc_power_off(struct mmc_host *host)
464 {
465         host->ios.clock = 0;
466         host->ios.vdd = 0;
467         host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
468         host->ios.chip_select = MMC_CS_DONTCARE;
469         host->ios.power_mode = MMC_POWER_OFF;
470         host->ios.bus_width = MMC_BUS_WIDTH_1;
471         host->ios.timing = MMC_TIMING_LEGACY;
472         mmc_set_ios(host);
473 }
474
475 /*
476  * Cleanup when the last reference to the bus operator is dropped.
477  */
478 void __mmc_release_bus(struct mmc_host *host)
479 {
480         BUG_ON(!host);
481         BUG_ON(host->bus_refs);
482         BUG_ON(!host->bus_dead);
483
484         host->bus_ops = NULL;
485 }
486
487 /*
488  * Increase reference count of bus operator
489  */
490 static inline void mmc_bus_get(struct mmc_host *host)
491 {
492         unsigned long flags;
493
494         spin_lock_irqsave(&host->lock, flags);
495         host->bus_refs++;
496         spin_unlock_irqrestore(&host->lock, flags);
497 }
498
499 /*
500  * Decrease reference count of bus operator and free it if
501  * it is the last reference.
502  */
503 static inline void mmc_bus_put(struct mmc_host *host)
504 {
505         unsigned long flags;
506
507         spin_lock_irqsave(&host->lock, flags);
508         host->bus_refs--;
509         if ((host->bus_refs == 0) && host->bus_ops)
510                 __mmc_release_bus(host);
511         spin_unlock_irqrestore(&host->lock, flags);
512 }
513
514 /*
515  * Assign a mmc bus handler to a host. Only one bus handler may control a
516  * host at any given time.
517  */
518 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
519 {
520         unsigned long flags;
521
522         BUG_ON(!host);
523         BUG_ON(!ops);
524
525         BUG_ON(!host->claimed);
526
527         spin_lock_irqsave(&host->lock, flags);
528
529         BUG_ON(host->bus_ops);
530         BUG_ON(host->bus_refs);
531
532         host->bus_ops = ops;
533         host->bus_refs = 1;
534         host->bus_dead = 0;
535
536         spin_unlock_irqrestore(&host->lock, flags);
537 }
538
539 /*
540  * Remove the current bus handler from a host. Assumes that there are
541  * no interesting cards left, so the bus is powered down.
542  */
543 void mmc_detach_bus(struct mmc_host *host)
544 {
545         unsigned long flags;
546
547         BUG_ON(!host);
548
549         BUG_ON(!host->claimed);
550         BUG_ON(!host->bus_ops);
551
552         spin_lock_irqsave(&host->lock, flags);
553
554         host->bus_dead = 1;
555
556         spin_unlock_irqrestore(&host->lock, flags);
557
558         mmc_power_off(host);
559
560         mmc_bus_put(host);
561 }
562
563 /**
564  *      mmc_detect_change - process change of state on a MMC socket
565  *      @host: host which changed state.
566  *      @delay: optional delay to wait before detection (jiffies)
567  *
568  *      MMC drivers should call this when they detect a card has been
569  *      inserted or removed. The MMC layer will confirm that any
570  *      present card is still functional, and initialize any newly
571  *      inserted.
572  */
573 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
574 {
575 #ifdef CONFIG_MMC_DEBUG
576         unsigned long flags;
577         spin_lock_irqsave(&host->lock, flags);
578         BUG_ON(host->removed);
579         spin_unlock_irqrestore(&host->lock, flags);
580 #endif
581
582         mmc_schedule_delayed_work(&host->detect, delay);
583 }
584
585 EXPORT_SYMBOL(mmc_detect_change);
586
587
588 void mmc_rescan(struct work_struct *work)
589 {
590         struct mmc_host *host =
591                 container_of(work, struct mmc_host, detect.work);
592         u32 ocr;
593         int err;
594
595         mmc_bus_get(host);
596
597         if (host->bus_ops == NULL) {
598                 /*
599                  * Only we can add a new handler, so it's safe to
600                  * release the lock here.
601                  */
602                 mmc_bus_put(host);
603
604                 mmc_claim_host(host);
605
606                 mmc_power_up(host);
607                 mmc_go_idle(host);
608
609                 mmc_send_if_cond(host, host->ocr_avail);
610
611                 /*
612                  * First we search for SDIO...
613                  */
614                 err = mmc_send_io_op_cond(host, 0, &ocr);
615                 if (!err) {
616                         if (mmc_attach_sdio(host, ocr))
617                                 mmc_power_off(host);
618                         return;
619                 }
620
621                 /*
622                  * ...then normal SD...
623                  */
624                 err = mmc_send_app_op_cond(host, 0, &ocr);
625                 if (!err) {
626                         if (mmc_attach_sd(host, ocr))
627                                 mmc_power_off(host);
628                         return;
629                 }
630
631                 /*
632                  * ...and finally MMC.
633                  */
634                 err = mmc_send_op_cond(host, 0, &ocr);
635                 if (!err) {
636                         if (mmc_attach_mmc(host, ocr))
637                                 mmc_power_off(host);
638                         return;
639                 }
640
641                 mmc_release_host(host);
642                 mmc_power_off(host);
643         } else {
644                 if (host->bus_ops->detect && !host->bus_dead)
645                         host->bus_ops->detect(host);
646
647                 mmc_bus_put(host);
648         }
649 }
650
651 void mmc_start_host(struct mmc_host *host)
652 {
653         mmc_power_off(host);
654         mmc_detect_change(host, 0);
655 }
656
657 void mmc_stop_host(struct mmc_host *host)
658 {
659 #ifdef CONFIG_MMC_DEBUG
660         unsigned long flags;
661         spin_lock_irqsave(&host->lock, flags);
662         host->removed = 1;
663         spin_unlock_irqrestore(&host->lock, flags);
664 #endif
665
666         mmc_flush_scheduled_work();
667
668         mmc_bus_get(host);
669         if (host->bus_ops && !host->bus_dead) {
670                 if (host->bus_ops->remove)
671                         host->bus_ops->remove(host);
672
673                 mmc_claim_host(host);
674                 mmc_detach_bus(host);
675                 mmc_release_host(host);
676         }
677         mmc_bus_put(host);
678
679         BUG_ON(host->card);
680
681         mmc_power_off(host);
682 }
683
684 #ifdef CONFIG_PM
685
686 /**
687  *      mmc_suspend_host - suspend a host
688  *      @host: mmc host
689  *      @state: suspend mode (PM_SUSPEND_xxx)
690  */
691 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
692 {
693         mmc_flush_scheduled_work();
694
695         mmc_bus_get(host);
696         if (host->bus_ops && !host->bus_dead) {
697                 if (host->bus_ops->suspend)
698                         host->bus_ops->suspend(host);
699                 if (!host->bus_ops->resume) {
700                         if (host->bus_ops->remove)
701                                 host->bus_ops->remove(host);
702
703                         mmc_claim_host(host);
704                         mmc_detach_bus(host);
705                         mmc_release_host(host);
706                 }
707         }
708         mmc_bus_put(host);
709
710         mmc_power_off(host);
711
712         return 0;
713 }
714
715 EXPORT_SYMBOL(mmc_suspend_host);
716
717 /**
718  *      mmc_resume_host - resume a previously suspended host
719  *      @host: mmc host
720  */
721 int mmc_resume_host(struct mmc_host *host)
722 {
723         mmc_bus_get(host);
724         if (host->bus_ops && !host->bus_dead) {
725                 mmc_power_up(host);
726                 BUG_ON(!host->bus_ops->resume);
727                 host->bus_ops->resume(host);
728         }
729         mmc_bus_put(host);
730
731         /*
732          * We add a slight delay here so that resume can progress
733          * in parallel.
734          */
735         mmc_detect_change(host, 1);
736
737         return 0;
738 }
739
740 EXPORT_SYMBOL(mmc_resume_host);
741
742 #endif
743
744 static int __init mmc_init(void)
745 {
746         int ret;
747
748         workqueue = create_singlethread_workqueue("kmmcd");
749         if (!workqueue)
750                 return -ENOMEM;
751
752         ret = mmc_register_bus();
753         if (ret)
754                 goto destroy_workqueue;
755
756         ret = mmc_register_host_class();
757         if (ret)
758                 goto unregister_bus;
759
760         ret = sdio_register_bus();
761         if (ret)
762                 goto unregister_host_class;
763
764         return 0;
765
766 unregister_host_class:
767         mmc_unregister_host_class();
768 unregister_bus:
769         mmc_unregister_bus();
770 destroy_workqueue:
771         destroy_workqueue(workqueue);
772
773         return ret;
774 }
775
776 static void __exit mmc_exit(void)
777 {
778         sdio_unregister_bus();
779         mmc_unregister_host_class();
780         mmc_unregister_bus();
781         destroy_workqueue(workqueue);
782 }
783
784 subsys_initcall(mmc_init);
785 module_exit(mmc_exit);
786
787 MODULE_LICENSE("GPL");