]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/mmc/core/core.c
Merge branch 'upstream-jgarzik' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-omap-h63xx.git] / drivers / mmc / core / core.c
1 /*
2  *  linux/drivers/mmc/core/core.c
3  *
4  *  Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5  *  SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
6  *  Copyright (C) 2005-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
33 #include "mmc_ops.h"
34 #include "sd_ops.h"
35
36 extern int mmc_attach_mmc(struct mmc_host *host, u32 ocr);
37 extern int mmc_attach_sd(struct mmc_host *host, u32 ocr);
38
39 static struct workqueue_struct *workqueue;
40
41 /*
42  * Internal function. Schedule delayed work in the MMC work queue.
43  */
44 static int mmc_schedule_delayed_work(struct delayed_work *work,
45                                      unsigned long delay)
46 {
47         return queue_delayed_work(workqueue, work, delay);
48 }
49
50 /*
51  * Internal function. Flush all scheduled work from the MMC work queue.
52  */
53 static void mmc_flush_scheduled_work(void)
54 {
55         flush_workqueue(workqueue);
56 }
57
58 /**
59  *      mmc_request_done - finish processing an MMC request
60  *      @host: MMC host which completed request
61  *      @mrq: MMC request which request
62  *
63  *      MMC drivers should call this function when they have completed
64  *      their processing of a request.
65  */
66 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
67 {
68         struct mmc_command *cmd = mrq->cmd;
69         int err = cmd->error;
70
71         pr_debug("%s: req done (CMD%u): %d/%d/%d: %08x %08x %08x %08x\n",
72                  mmc_hostname(host), cmd->opcode, err,
73                  mrq->data ? mrq->data->error : 0,
74                  mrq->stop ? mrq->stop->error : 0,
75                  cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
76
77         if (err && cmd->retries) {
78                 cmd->retries--;
79                 cmd->error = 0;
80                 host->ops->request(host, mrq);
81         } else if (mrq->done) {
82                 mrq->done(mrq);
83         }
84 }
85
86 EXPORT_SYMBOL(mmc_request_done);
87
88 /**
89  *      mmc_start_request - start a command on a host
90  *      @host: MMC host to start command on
91  *      @mrq: MMC request to start
92  *
93  *      Queue a command on the specified host.  We expect the
94  *      caller to be holding the host lock with interrupts disabled.
95  */
96 void
97 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
98 {
99 #ifdef CONFIG_MMC_DEBUG
100         unsigned int i, sz;
101 #endif
102
103         pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
104                  mmc_hostname(host), mrq->cmd->opcode,
105                  mrq->cmd->arg, mrq->cmd->flags);
106
107         WARN_ON(!host->claimed);
108
109         mrq->cmd->error = 0;
110         mrq->cmd->mrq = mrq;
111         if (mrq->data) {
112                 BUG_ON(mrq->data->blksz > host->max_blk_size);
113                 BUG_ON(mrq->data->blocks > host->max_blk_count);
114                 BUG_ON(mrq->data->blocks * mrq->data->blksz >
115                         host->max_req_size);
116
117 #ifdef CONFIG_MMC_DEBUG
118                 sz = 0;
119                 for (i = 0;i < mrq->data->sg_len;i++)
120                         sz += mrq->data->sg[i].length;
121                 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
122 #endif
123
124                 mrq->cmd->data = mrq->data;
125                 mrq->data->error = 0;
126                 mrq->data->mrq = mrq;
127                 if (mrq->stop) {
128                         mrq->data->stop = mrq->stop;
129                         mrq->stop->error = 0;
130                         mrq->stop->mrq = mrq;
131                 }
132         }
133         host->ops->request(host, mrq);
134 }
135
136 EXPORT_SYMBOL(mmc_start_request);
137
138 static void mmc_wait_done(struct mmc_request *mrq)
139 {
140         complete(mrq->done_data);
141 }
142
143 int mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
144 {
145         DECLARE_COMPLETION_ONSTACK(complete);
146
147         mrq->done_data = &complete;
148         mrq->done = mmc_wait_done;
149
150         mmc_start_request(host, mrq);
151
152         wait_for_completion(&complete);
153
154         return 0;
155 }
156
157 EXPORT_SYMBOL(mmc_wait_for_req);
158
159 /**
160  *      mmc_wait_for_cmd - start a command and wait for completion
161  *      @host: MMC host to start command
162  *      @cmd: MMC command to start
163  *      @retries: maximum number of retries
164  *
165  *      Start a new MMC command for a host, and wait for the command
166  *      to complete.  Return any error that occurred while the command
167  *      was executing.  Do not attempt to parse the response.
168  */
169 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
170 {
171         struct mmc_request mrq;
172
173         BUG_ON(!host->claimed);
174
175         memset(&mrq, 0, sizeof(struct mmc_request));
176
177         memset(cmd->resp, 0, sizeof(cmd->resp));
178         cmd->retries = retries;
179
180         mrq.cmd = cmd;
181         cmd->data = NULL;
182
183         mmc_wait_for_req(host, &mrq);
184
185         return cmd->error;
186 }
187
188 EXPORT_SYMBOL(mmc_wait_for_cmd);
189
190 /**
191  *      mmc_set_data_timeout - set the timeout for a data command
192  *      @data: data phase for command
193  *      @card: the MMC card associated with the data transfer
194  *      @write: flag to differentiate reads from writes
195  */
196 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
197                           int write)
198 {
199         unsigned int mult;
200
201         /*
202          * SD cards use a 100 multiplier rather than 10
203          */
204         mult = mmc_card_sd(card) ? 100 : 10;
205
206         /*
207          * Scale up the multiplier (and therefore the timeout) by
208          * the r2w factor for writes.
209          */
210         if (write)
211                 mult <<= card->csd.r2w_factor;
212
213         data->timeout_ns = card->csd.tacc_ns * mult;
214         data->timeout_clks = card->csd.tacc_clks * mult;
215
216         /*
217          * SD cards also have an upper limit on the timeout.
218          */
219         if (mmc_card_sd(card)) {
220                 unsigned int timeout_us, limit_us;
221
222                 timeout_us = data->timeout_ns / 1000;
223                 timeout_us += data->timeout_clks * 1000 /
224                         (card->host->ios.clock / 1000);
225
226                 if (write)
227                         limit_us = 250000;
228                 else
229                         limit_us = 100000;
230
231                 /*
232                  * SDHC cards always use these fixed values.
233                  */
234                 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
235                         data->timeout_ns = limit_us * 1000;
236                         data->timeout_clks = 0;
237                 }
238         }
239 }
240 EXPORT_SYMBOL(mmc_set_data_timeout);
241
242 /**
243  *      __mmc_claim_host - exclusively claim a host
244  *      @host: mmc host to claim
245  *      @card: mmc card to claim host for
246  *
247  *      Claim a host for a set of operations.  If a valid card
248  *      is passed and this wasn't the last card selected, select
249  *      the card before returning.
250  *
251  *      Note: you should use mmc_card_claim_host or mmc_claim_host.
252  */
253 void mmc_claim_host(struct mmc_host *host)
254 {
255         DECLARE_WAITQUEUE(wait, current);
256         unsigned long flags;
257
258         add_wait_queue(&host->wq, &wait);
259         spin_lock_irqsave(&host->lock, flags);
260         while (1) {
261                 set_current_state(TASK_UNINTERRUPTIBLE);
262                 if (!host->claimed)
263                         break;
264                 spin_unlock_irqrestore(&host->lock, flags);
265                 schedule();
266                 spin_lock_irqsave(&host->lock, flags);
267         }
268         set_current_state(TASK_RUNNING);
269         host->claimed = 1;
270         spin_unlock_irqrestore(&host->lock, flags);
271         remove_wait_queue(&host->wq, &wait);
272 }
273
274 EXPORT_SYMBOL(mmc_claim_host);
275
276 /**
277  *      mmc_release_host - release a host
278  *      @host: mmc host to release
279  *
280  *      Release a MMC host, allowing others to claim the host
281  *      for their operations.
282  */
283 void mmc_release_host(struct mmc_host *host)
284 {
285         unsigned long flags;
286
287         BUG_ON(!host->claimed);
288
289         spin_lock_irqsave(&host->lock, flags);
290         host->claimed = 0;
291         spin_unlock_irqrestore(&host->lock, flags);
292
293         wake_up(&host->wq);
294 }
295
296 EXPORT_SYMBOL(mmc_release_host);
297
298 /*
299  * Internal function that does the actual ios call to the host driver,
300  * optionally printing some debug output.
301  */
302 static inline void mmc_set_ios(struct mmc_host *host)
303 {
304         struct mmc_ios *ios = &host->ios;
305
306         pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
307                 "width %u timing %u\n",
308                  mmc_hostname(host), ios->clock, ios->bus_mode,
309                  ios->power_mode, ios->chip_select, ios->vdd,
310                  ios->bus_width, ios->timing);
311
312         host->ops->set_ios(host, ios);
313 }
314
315 /*
316  * Control chip select pin on a host.
317  */
318 void mmc_set_chip_select(struct mmc_host *host, int mode)
319 {
320         host->ios.chip_select = mode;
321         mmc_set_ios(host);
322 }
323
324 /*
325  * Sets the host clock to the highest possible frequency that
326  * is below "hz".
327  */
328 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
329 {
330         WARN_ON(hz < host->f_min);
331
332         if (hz > host->f_max)
333                 hz = host->f_max;
334
335         host->ios.clock = hz;
336         mmc_set_ios(host);
337 }
338
339 /*
340  * Change the bus mode (open drain/push-pull) of a host.
341  */
342 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
343 {
344         host->ios.bus_mode = mode;
345         mmc_set_ios(host);
346 }
347
348 /*
349  * Change data bus width of a host.
350  */
351 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
352 {
353         host->ios.bus_width = width;
354         mmc_set_ios(host);
355 }
356
357 /*
358  * Mask off any voltages we don't support and select
359  * the lowest voltage
360  */
361 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
362 {
363         int bit;
364
365         ocr &= host->ocr_avail;
366
367         bit = ffs(ocr);
368         if (bit) {
369                 bit -= 1;
370
371                 ocr &= 3 << bit;
372
373                 host->ios.vdd = bit;
374                 mmc_set_ios(host);
375         } else {
376                 ocr = 0;
377         }
378
379         return ocr;
380 }
381
382 /*
383  * Select timing parameters for host.
384  */
385 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
386 {
387         host->ios.timing = timing;
388         mmc_set_ios(host);
389 }
390
391 /*
392  * Apply power to the MMC stack.  This is a two-stage process.
393  * First, we enable power to the card without the clock running.
394  * We then wait a bit for the power to stabilise.  Finally,
395  * enable the bus drivers and clock to the card.
396  *
397  * We must _NOT_ enable the clock prior to power stablising.
398  *
399  * If a host does all the power sequencing itself, ignore the
400  * initial MMC_POWER_UP stage.
401  */
402 static void mmc_power_up(struct mmc_host *host)
403 {
404         int bit = fls(host->ocr_avail) - 1;
405
406         host->ios.vdd = bit;
407         host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
408         host->ios.chip_select = MMC_CS_DONTCARE;
409         host->ios.power_mode = MMC_POWER_UP;
410         host->ios.bus_width = MMC_BUS_WIDTH_1;
411         host->ios.timing = MMC_TIMING_LEGACY;
412         mmc_set_ios(host);
413
414         mmc_delay(1);
415
416         host->ios.clock = host->f_min;
417         host->ios.power_mode = MMC_POWER_ON;
418         mmc_set_ios(host);
419
420         mmc_delay(2);
421 }
422
423 static void mmc_power_off(struct mmc_host *host)
424 {
425         host->ios.clock = 0;
426         host->ios.vdd = 0;
427         host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
428         host->ios.chip_select = MMC_CS_DONTCARE;
429         host->ios.power_mode = MMC_POWER_OFF;
430         host->ios.bus_width = MMC_BUS_WIDTH_1;
431         host->ios.timing = MMC_TIMING_LEGACY;
432         mmc_set_ios(host);
433 }
434
435 /*
436  * Assign a mmc bus handler to a host. Only one bus handler may control a
437  * host at any given time.
438  */
439 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
440 {
441         unsigned long flags;
442
443         BUG_ON(!host);
444         BUG_ON(!ops);
445
446         BUG_ON(!host->claimed);
447
448         spin_lock_irqsave(&host->lock, flags);
449
450         BUG_ON(host->bus_ops);
451         BUG_ON(host->bus_refs);
452
453         host->bus_ops = ops;
454         host->bus_refs = 1;
455         host->bus_dead = 0;
456
457         spin_unlock_irqrestore(&host->lock, flags);
458 }
459
460 /*
461  * Remove the current bus handler from a host. Assumes that there are
462  * no interesting cards left, so the bus is powered down.
463  */
464 void mmc_detach_bus(struct mmc_host *host)
465 {
466         unsigned long flags;
467
468         BUG_ON(!host);
469
470         BUG_ON(!host->claimed);
471         BUG_ON(!host->bus_ops);
472
473         spin_lock_irqsave(&host->lock, flags);
474
475         host->bus_dead = 1;
476
477         spin_unlock_irqrestore(&host->lock, flags);
478
479         mmc_power_off(host);
480
481         mmc_bus_put(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  *      mmc_detect_change - process change of state on a MMC socket
498  *      @host: host which changed state.
499  *      @delay: optional delay to wait before detection (jiffies)
500  *
501  *      All we know is that card(s) have been inserted or removed
502  *      from the socket(s).  We don't know which socket or cards.
503  */
504 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
505 {
506 #ifdef CONFIG_MMC_DEBUG
507         unsigned long flags;
508         spin_lock_irqsave(&host->lock, flags);
509         BUG_ON(host->removed);
510         spin_unlock_irqrestore(&host->lock, flags);
511 #endif
512
513         mmc_schedule_delayed_work(&host->detect, delay);
514 }
515
516 EXPORT_SYMBOL(mmc_detect_change);
517
518
519 void mmc_rescan(struct work_struct *work)
520 {
521         struct mmc_host *host =
522                 container_of(work, struct mmc_host, detect.work);
523         u32 ocr;
524         int err;
525
526         mmc_bus_get(host);
527
528         if (host->bus_ops == NULL) {
529                 /*
530                  * Only we can add a new handler, so it's safe to
531                  * release the lock here.
532                  */
533                 mmc_bus_put(host);
534
535                 mmc_claim_host(host);
536
537                 mmc_power_up(host);
538                 mmc_go_idle(host);
539
540                 mmc_send_if_cond(host, host->ocr_avail);
541
542                 err = mmc_send_app_op_cond(host, 0, &ocr);
543                 if (err == MMC_ERR_NONE) {
544                         if (mmc_attach_sd(host, ocr))
545                                 mmc_power_off(host);
546                 } else {
547                         /*
548                          * If we fail to detect any SD cards then try
549                          * searching for MMC cards.
550                          */
551                         err = mmc_send_op_cond(host, 0, &ocr);
552                         if (err == MMC_ERR_NONE) {
553                                 if (mmc_attach_mmc(host, ocr))
554                                         mmc_power_off(host);
555                         } else {
556                                 mmc_power_off(host);
557                                 mmc_release_host(host);
558                         }
559                 }
560         } else {
561                 if (host->bus_ops->detect && !host->bus_dead)
562                         host->bus_ops->detect(host);
563
564                 mmc_bus_put(host);
565         }
566 }
567
568 void mmc_start_host(struct mmc_host *host)
569 {
570         mmc_power_off(host);
571         mmc_detect_change(host, 0);
572 }
573
574 void mmc_stop_host(struct mmc_host *host)
575 {
576 #ifdef CONFIG_MMC_DEBUG
577         unsigned long flags;
578         spin_lock_irqsave(&host->lock, flags);
579         host->removed = 1;
580         spin_unlock_irqrestore(&host->lock, flags);
581 #endif
582
583         mmc_flush_scheduled_work();
584
585         mmc_bus_get(host);
586         if (host->bus_ops && !host->bus_dead) {
587                 if (host->bus_ops->remove)
588                         host->bus_ops->remove(host);
589
590                 mmc_claim_host(host);
591                 mmc_detach_bus(host);
592                 mmc_release_host(host);
593         }
594         mmc_bus_put(host);
595
596         BUG_ON(host->card);
597
598         mmc_power_off(host);
599 }
600
601 #ifdef CONFIG_PM
602
603 /**
604  *      mmc_suspend_host - suspend a host
605  *      @host: mmc host
606  *      @state: suspend mode (PM_SUSPEND_xxx)
607  */
608 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
609 {
610         mmc_flush_scheduled_work();
611
612         mmc_bus_get(host);
613         if (host->bus_ops && !host->bus_dead) {
614                 if (host->bus_ops->suspend)
615                         host->bus_ops->suspend(host);
616                 if (!host->bus_ops->resume) {
617                         if (host->bus_ops->remove)
618                                 host->bus_ops->remove(host);
619
620                         mmc_claim_host(host);
621                         mmc_detach_bus(host);
622                         mmc_release_host(host);
623                 }
624         }
625         mmc_bus_put(host);
626
627         mmc_power_off(host);
628
629         return 0;
630 }
631
632 EXPORT_SYMBOL(mmc_suspend_host);
633
634 /**
635  *      mmc_resume_host - resume a previously suspended host
636  *      @host: mmc host
637  */
638 int mmc_resume_host(struct mmc_host *host)
639 {
640         mmc_bus_get(host);
641         if (host->bus_ops && !host->bus_dead) {
642                 mmc_power_up(host);
643                 BUG_ON(!host->bus_ops->resume);
644                 host->bus_ops->resume(host);
645         }
646         mmc_bus_put(host);
647
648         /*
649          * We add a slight delay here so that resume can progress
650          * in parallel.
651          */
652         mmc_detect_change(host, 1);
653
654         return 0;
655 }
656
657 EXPORT_SYMBOL(mmc_resume_host);
658
659 #endif
660
661 static int __init mmc_init(void)
662 {
663         int ret;
664
665         workqueue = create_singlethread_workqueue("kmmcd");
666         if (!workqueue)
667                 return -ENOMEM;
668
669         ret = mmc_register_bus();
670         if (ret == 0) {
671                 ret = mmc_register_host_class();
672                 if (ret)
673                         mmc_unregister_bus();
674         }
675         return ret;
676 }
677
678 static void __exit mmc_exit(void)
679 {
680         mmc_unregister_host_class();
681         mmc_unregister_bus();
682         destroy_workqueue(workqueue);
683 }
684
685 module_init(mmc_init);
686 module_exit(mmc_exit);
687
688 MODULE_LICENSE("GPL");