]> err.no Git - linux-2.6/blob - drivers/mmc/core/core.c
mmc: update kerneldoc
[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
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 /**
144  *      mmc_wait_for_req - start a request and wait for completion
145  *      @host: MMC host to start command
146  *      @mrq: MMC request to start
147  *
148  *      Start a new MMC custom command request for a host, and wait
149  *      for the command to complete. Does not attempt to parse the
150  *      response.
151  */
152 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
153 {
154         DECLARE_COMPLETION_ONSTACK(complete);
155
156         mrq->done_data = &complete;
157         mrq->done = mmc_wait_done;
158
159         mmc_start_request(host, mrq);
160
161         wait_for_completion(&complete);
162 }
163
164 EXPORT_SYMBOL(mmc_wait_for_req);
165
166 /**
167  *      mmc_wait_for_cmd - start a command and wait for completion
168  *      @host: MMC host to start command
169  *      @cmd: MMC command to start
170  *      @retries: maximum number of retries
171  *
172  *      Start a new MMC command for a host, and wait for the command
173  *      to complete.  Return any error that occurred while the command
174  *      was executing.  Do not attempt to parse the response.
175  */
176 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
177 {
178         struct mmc_request mrq;
179
180         BUG_ON(!host->claimed);
181
182         memset(&mrq, 0, sizeof(struct mmc_request));
183
184         memset(cmd->resp, 0, sizeof(cmd->resp));
185         cmd->retries = retries;
186
187         mrq.cmd = cmd;
188         cmd->data = NULL;
189
190         mmc_wait_for_req(host, &mrq);
191
192         return cmd->error;
193 }
194
195 EXPORT_SYMBOL(mmc_wait_for_cmd);
196
197 /**
198  *      mmc_set_data_timeout - set the timeout for a data command
199  *      @data: data phase for command
200  *      @card: the MMC card associated with the data transfer
201  *      @write: flag to differentiate reads from writes
202  *
203  *      Computes the data timeout parameters according to the
204  *      correct algorithm given the card type.
205  */
206 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
207                           int write)
208 {
209         unsigned int mult;
210
211         /*
212          * SD cards use a 100 multiplier rather than 10
213          */
214         mult = mmc_card_sd(card) ? 100 : 10;
215
216         /*
217          * Scale up the multiplier (and therefore the timeout) by
218          * the r2w factor for writes.
219          */
220         if (write)
221                 mult <<= card->csd.r2w_factor;
222
223         data->timeout_ns = card->csd.tacc_ns * mult;
224         data->timeout_clks = card->csd.tacc_clks * mult;
225
226         /*
227          * SD cards also have an upper limit on the timeout.
228          */
229         if (mmc_card_sd(card)) {
230                 unsigned int timeout_us, limit_us;
231
232                 timeout_us = data->timeout_ns / 1000;
233                 timeout_us += data->timeout_clks * 1000 /
234                         (card->host->ios.clock / 1000);
235
236                 if (write)
237                         limit_us = 250000;
238                 else
239                         limit_us = 100000;
240
241                 /*
242                  * SDHC cards always use these fixed values.
243                  */
244                 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
245                         data->timeout_ns = limit_us * 1000;
246                         data->timeout_clks = 0;
247                 }
248         }
249 }
250 EXPORT_SYMBOL(mmc_set_data_timeout);
251
252 /**
253  *      mmc_claim_host - exclusively claim a host
254  *      @host: mmc host to claim
255  *
256  *      Claim a host for a set of operations.
257  */
258 void mmc_claim_host(struct mmc_host *host)
259 {
260         DECLARE_WAITQUEUE(wait, current);
261         unsigned long flags;
262
263         add_wait_queue(&host->wq, &wait);
264         spin_lock_irqsave(&host->lock, flags);
265         while (1) {
266                 set_current_state(TASK_UNINTERRUPTIBLE);
267                 if (!host->claimed)
268                         break;
269                 spin_unlock_irqrestore(&host->lock, flags);
270                 schedule();
271                 spin_lock_irqsave(&host->lock, flags);
272         }
273         set_current_state(TASK_RUNNING);
274         host->claimed = 1;
275         spin_unlock_irqrestore(&host->lock, flags);
276         remove_wait_queue(&host->wq, &wait);
277 }
278
279 EXPORT_SYMBOL(mmc_claim_host);
280
281 /**
282  *      mmc_release_host - release a host
283  *      @host: mmc host to release
284  *
285  *      Release a MMC host, allowing others to claim the host
286  *      for their operations.
287  */
288 void mmc_release_host(struct mmc_host *host)
289 {
290         unsigned long flags;
291
292         BUG_ON(!host->claimed);
293
294         spin_lock_irqsave(&host->lock, flags);
295         host->claimed = 0;
296         spin_unlock_irqrestore(&host->lock, flags);
297
298         wake_up(&host->wq);
299 }
300
301 EXPORT_SYMBOL(mmc_release_host);
302
303 /*
304  * Internal function that does the actual ios call to the host driver,
305  * optionally printing some debug output.
306  */
307 static inline void mmc_set_ios(struct mmc_host *host)
308 {
309         struct mmc_ios *ios = &host->ios;
310
311         pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
312                 "width %u timing %u\n",
313                  mmc_hostname(host), ios->clock, ios->bus_mode,
314                  ios->power_mode, ios->chip_select, ios->vdd,
315                  ios->bus_width, ios->timing);
316
317         host->ops->set_ios(host, ios);
318 }
319
320 /*
321  * Control chip select pin on a host.
322  */
323 void mmc_set_chip_select(struct mmc_host *host, int mode)
324 {
325         host->ios.chip_select = mode;
326         mmc_set_ios(host);
327 }
328
329 /*
330  * Sets the host clock to the highest possible frequency that
331  * is below "hz".
332  */
333 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
334 {
335         WARN_ON(hz < host->f_min);
336
337         if (hz > host->f_max)
338                 hz = host->f_max;
339
340         host->ios.clock = hz;
341         mmc_set_ios(host);
342 }
343
344 /*
345  * Change the bus mode (open drain/push-pull) of a host.
346  */
347 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
348 {
349         host->ios.bus_mode = mode;
350         mmc_set_ios(host);
351 }
352
353 /*
354  * Change data bus width of a host.
355  */
356 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
357 {
358         host->ios.bus_width = width;
359         mmc_set_ios(host);
360 }
361
362 /*
363  * Mask off any voltages we don't support and select
364  * the lowest voltage
365  */
366 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
367 {
368         int bit;
369
370         ocr &= host->ocr_avail;
371
372         bit = ffs(ocr);
373         if (bit) {
374                 bit -= 1;
375
376                 ocr &= 3 << bit;
377
378                 host->ios.vdd = bit;
379                 mmc_set_ios(host);
380         } else {
381                 ocr = 0;
382         }
383
384         return ocr;
385 }
386
387 /*
388  * Select timing parameters for host.
389  */
390 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
391 {
392         host->ios.timing = timing;
393         mmc_set_ios(host);
394 }
395
396 /*
397  * Apply power to the MMC stack.  This is a two-stage process.
398  * First, we enable power to the card without the clock running.
399  * We then wait a bit for the power to stabilise.  Finally,
400  * enable the bus drivers and clock to the card.
401  *
402  * We must _NOT_ enable the clock prior to power stablising.
403  *
404  * If a host does all the power sequencing itself, ignore the
405  * initial MMC_POWER_UP stage.
406  */
407 static void mmc_power_up(struct mmc_host *host)
408 {
409         int bit = fls(host->ocr_avail) - 1;
410
411         host->ios.vdd = bit;
412         host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
413         host->ios.chip_select = MMC_CS_DONTCARE;
414         host->ios.power_mode = MMC_POWER_UP;
415         host->ios.bus_width = MMC_BUS_WIDTH_1;
416         host->ios.timing = MMC_TIMING_LEGACY;
417         mmc_set_ios(host);
418
419         mmc_delay(1);
420
421         host->ios.clock = host->f_min;
422         host->ios.power_mode = MMC_POWER_ON;
423         mmc_set_ios(host);
424
425         mmc_delay(2);
426 }
427
428 static void mmc_power_off(struct mmc_host *host)
429 {
430         host->ios.clock = 0;
431         host->ios.vdd = 0;
432         host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
433         host->ios.chip_select = MMC_CS_DONTCARE;
434         host->ios.power_mode = MMC_POWER_OFF;
435         host->ios.bus_width = MMC_BUS_WIDTH_1;
436         host->ios.timing = MMC_TIMING_LEGACY;
437         mmc_set_ios(host);
438 }
439
440 /*
441  * Assign a mmc bus handler to a host. Only one bus handler may control a
442  * host at any given time.
443  */
444 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
445 {
446         unsigned long flags;
447
448         BUG_ON(!host);
449         BUG_ON(!ops);
450
451         BUG_ON(!host->claimed);
452
453         spin_lock_irqsave(&host->lock, flags);
454
455         BUG_ON(host->bus_ops);
456         BUG_ON(host->bus_refs);
457
458         host->bus_ops = ops;
459         host->bus_refs = 1;
460         host->bus_dead = 0;
461
462         spin_unlock_irqrestore(&host->lock, flags);
463 }
464
465 /*
466  * Remove the current bus handler from a host. Assumes that there are
467  * no interesting cards left, so the bus is powered down.
468  */
469 void mmc_detach_bus(struct mmc_host *host)
470 {
471         unsigned long flags;
472
473         BUG_ON(!host);
474
475         BUG_ON(!host->claimed);
476         BUG_ON(!host->bus_ops);
477
478         spin_lock_irqsave(&host->lock, flags);
479
480         host->bus_dead = 1;
481
482         spin_unlock_irqrestore(&host->lock, flags);
483
484         mmc_power_off(host);
485
486         mmc_bus_put(host);
487 }
488
489 /*
490  * Cleanup when the last reference to the bus operator is dropped.
491  */
492 void __mmc_release_bus(struct mmc_host *host)
493 {
494         BUG_ON(!host);
495         BUG_ON(host->bus_refs);
496         BUG_ON(!host->bus_dead);
497
498         host->bus_ops = NULL;
499 }
500
501 /**
502  *      mmc_detect_change - process change of state on a MMC socket
503  *      @host: host which changed state.
504  *      @delay: optional delay to wait before detection (jiffies)
505  *
506  *      MMC drivers should call this when they detect a card has been
507  *      inserted or removed. The MMC layer will confirm that any
508  *      present card is still functional, and initialize any newly
509  *      inserted.
510  */
511 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
512 {
513 #ifdef CONFIG_MMC_DEBUG
514         unsigned long flags;
515         spin_lock_irqsave(&host->lock, flags);
516         BUG_ON(host->removed);
517         spin_unlock_irqrestore(&host->lock, flags);
518 #endif
519
520         mmc_schedule_delayed_work(&host->detect, delay);
521 }
522
523 EXPORT_SYMBOL(mmc_detect_change);
524
525
526 void mmc_rescan(struct work_struct *work)
527 {
528         struct mmc_host *host =
529                 container_of(work, struct mmc_host, detect.work);
530         u32 ocr;
531         int err;
532
533         mmc_bus_get(host);
534
535         if (host->bus_ops == NULL) {
536                 /*
537                  * Only we can add a new handler, so it's safe to
538                  * release the lock here.
539                  */
540                 mmc_bus_put(host);
541
542                 mmc_claim_host(host);
543
544                 mmc_power_up(host);
545                 mmc_go_idle(host);
546
547                 mmc_send_if_cond(host, host->ocr_avail);
548
549                 err = mmc_send_app_op_cond(host, 0, &ocr);
550                 if (err == MMC_ERR_NONE) {
551                         if (mmc_attach_sd(host, ocr))
552                                 mmc_power_off(host);
553                 } else {
554                         /*
555                          * If we fail to detect any SD cards then try
556                          * searching for MMC cards.
557                          */
558                         err = mmc_send_op_cond(host, 0, &ocr);
559                         if (err == MMC_ERR_NONE) {
560                                 if (mmc_attach_mmc(host, ocr))
561                                         mmc_power_off(host);
562                         } else {
563                                 mmc_power_off(host);
564                                 mmc_release_host(host);
565                         }
566                 }
567         } else {
568                 if (host->bus_ops->detect && !host->bus_dead)
569                         host->bus_ops->detect(host);
570
571                 mmc_bus_put(host);
572         }
573 }
574
575 void mmc_start_host(struct mmc_host *host)
576 {
577         mmc_power_off(host);
578         mmc_detect_change(host, 0);
579 }
580
581 void mmc_stop_host(struct mmc_host *host)
582 {
583 #ifdef CONFIG_MMC_DEBUG
584         unsigned long flags;
585         spin_lock_irqsave(&host->lock, flags);
586         host->removed = 1;
587         spin_unlock_irqrestore(&host->lock, flags);
588 #endif
589
590         mmc_flush_scheduled_work();
591
592         mmc_bus_get(host);
593         if (host->bus_ops && !host->bus_dead) {
594                 if (host->bus_ops->remove)
595                         host->bus_ops->remove(host);
596
597                 mmc_claim_host(host);
598                 mmc_detach_bus(host);
599                 mmc_release_host(host);
600         }
601         mmc_bus_put(host);
602
603         BUG_ON(host->card);
604
605         mmc_power_off(host);
606 }
607
608 #ifdef CONFIG_PM
609
610 /**
611  *      mmc_suspend_host - suspend a host
612  *      @host: mmc host
613  *      @state: suspend mode (PM_SUSPEND_xxx)
614  */
615 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
616 {
617         mmc_flush_scheduled_work();
618
619         mmc_bus_get(host);
620         if (host->bus_ops && !host->bus_dead) {
621                 if (host->bus_ops->suspend)
622                         host->bus_ops->suspend(host);
623                 if (!host->bus_ops->resume) {
624                         if (host->bus_ops->remove)
625                                 host->bus_ops->remove(host);
626
627                         mmc_claim_host(host);
628                         mmc_detach_bus(host);
629                         mmc_release_host(host);
630                 }
631         }
632         mmc_bus_put(host);
633
634         mmc_power_off(host);
635
636         return 0;
637 }
638
639 EXPORT_SYMBOL(mmc_suspend_host);
640
641 /**
642  *      mmc_resume_host - resume a previously suspended host
643  *      @host: mmc host
644  */
645 int mmc_resume_host(struct mmc_host *host)
646 {
647         mmc_bus_get(host);
648         if (host->bus_ops && !host->bus_dead) {
649                 mmc_power_up(host);
650                 BUG_ON(!host->bus_ops->resume);
651                 host->bus_ops->resume(host);
652         }
653         mmc_bus_put(host);
654
655         /*
656          * We add a slight delay here so that resume can progress
657          * in parallel.
658          */
659         mmc_detect_change(host, 1);
660
661         return 0;
662 }
663
664 EXPORT_SYMBOL(mmc_resume_host);
665
666 #endif
667
668 static int __init mmc_init(void)
669 {
670         int ret;
671
672         workqueue = create_singlethread_workqueue("kmmcd");
673         if (!workqueue)
674                 return -ENOMEM;
675
676         ret = mmc_register_bus();
677         if (ret == 0) {
678                 ret = mmc_register_host_class();
679                 if (ret)
680                         mmc_unregister_bus();
681         }
682         return ret;
683 }
684
685 static void __exit mmc_exit(void)
686 {
687         mmc_unregister_host_class();
688         mmc_unregister_bus();
689         destroy_workqueue(workqueue);
690 }
691
692 module_init(mmc_init);
693 module_exit(mmc_exit);
694
695 MODULE_LICENSE("GPL");