]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/mmc/core/core.c
mmc: proper debugging output in core
[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         if (err && cmd->retries) {
72                 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
73                         mmc_hostname(host), cmd->opcode, err);
74
75                 cmd->retries--;
76                 cmd->error = 0;
77                 host->ops->request(host, mrq);
78         } else {
79                 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
80                         mmc_hostname(host), cmd->opcode, err,
81                         cmd->resp[0], cmd->resp[1],
82                         cmd->resp[2], cmd->resp[3]);
83
84                 if (mrq->data) {
85                         pr_debug("%s:     %d bytes transferred: %d\n",
86                                 mmc_hostname(host),
87                                 mrq->data->bytes_xfered, mrq->data->error);
88                 }
89
90                 if (mrq->stop) {
91                         pr_debug("%s:     (CMD%u): %d: %08x %08x %08x %08x\n",
92                                 mmc_hostname(host), mrq->stop->opcode,
93                                 mrq->stop->error,
94                                 mrq->stop->resp[0], mrq->stop->resp[1],
95                                 mrq->stop->resp[2], mrq->stop->resp[3]);
96                 }
97
98                 if (mrq->done)
99                         mrq->done(mrq);
100         }
101 }
102
103 EXPORT_SYMBOL(mmc_request_done);
104
105 /**
106  *      mmc_start_request - start a command on a host
107  *      @host: MMC host to start command on
108  *      @mrq: MMC request to start
109  *
110  *      Queue a command on the specified host.  We expect the
111  *      caller to be holding the host lock with interrupts disabled.
112  */
113 void
114 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
115 {
116 #ifdef CONFIG_MMC_DEBUG
117         unsigned int i, sz;
118 #endif
119
120         pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
121                  mmc_hostname(host), mrq->cmd->opcode,
122                  mrq->cmd->arg, mrq->cmd->flags);
123
124         if (mrq->data) {
125                 pr_debug("%s:     blksz %d blocks %d flags %08x "
126                         "tsac %d ms nsac %d\n",
127                         mmc_hostname(host), mrq->data->blksz,
128                         mrq->data->blocks, mrq->data->flags,
129                         mrq->data->timeout_ns / 10000000,
130                         mrq->data->timeout_clks);
131         }
132
133         if (mrq->stop) {
134                 pr_debug("%s:     CMD%u arg %08x flags %08x\n",
135                          mmc_hostname(host), mrq->stop->opcode,
136                          mrq->stop->arg, mrq->stop->flags);
137         }
138
139         WARN_ON(!host->claimed);
140
141         mrq->cmd->error = 0;
142         mrq->cmd->mrq = mrq;
143         if (mrq->data) {
144                 BUG_ON(mrq->data->blksz > host->max_blk_size);
145                 BUG_ON(mrq->data->blocks > host->max_blk_count);
146                 BUG_ON(mrq->data->blocks * mrq->data->blksz >
147                         host->max_req_size);
148
149 #ifdef CONFIG_MMC_DEBUG
150                 sz = 0;
151                 for (i = 0;i < mrq->data->sg_len;i++)
152                         sz += mrq->data->sg[i].length;
153                 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
154 #endif
155
156                 mrq->cmd->data = mrq->data;
157                 mrq->data->error = 0;
158                 mrq->data->mrq = mrq;
159                 if (mrq->stop) {
160                         mrq->data->stop = mrq->stop;
161                         mrq->stop->error = 0;
162                         mrq->stop->mrq = mrq;
163                 }
164         }
165         host->ops->request(host, mrq);
166 }
167
168 EXPORT_SYMBOL(mmc_start_request);
169
170 static void mmc_wait_done(struct mmc_request *mrq)
171 {
172         complete(mrq->done_data);
173 }
174
175 /**
176  *      mmc_wait_for_req - start a request and wait for completion
177  *      @host: MMC host to start command
178  *      @mrq: MMC request to start
179  *
180  *      Start a new MMC custom command request for a host, and wait
181  *      for the command to complete. Does not attempt to parse the
182  *      response.
183  */
184 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
185 {
186         DECLARE_COMPLETION_ONSTACK(complete);
187
188         mrq->done_data = &complete;
189         mrq->done = mmc_wait_done;
190
191         mmc_start_request(host, mrq);
192
193         wait_for_completion(&complete);
194 }
195
196 EXPORT_SYMBOL(mmc_wait_for_req);
197
198 /**
199  *      mmc_wait_for_cmd - start a command and wait for completion
200  *      @host: MMC host to start command
201  *      @cmd: MMC command to start
202  *      @retries: maximum number of retries
203  *
204  *      Start a new MMC command for a host, and wait for the command
205  *      to complete.  Return any error that occurred while the command
206  *      was executing.  Do not attempt to parse the response.
207  */
208 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
209 {
210         struct mmc_request mrq;
211
212         BUG_ON(!host->claimed);
213
214         memset(&mrq, 0, sizeof(struct mmc_request));
215
216         memset(cmd->resp, 0, sizeof(cmd->resp));
217         cmd->retries = retries;
218
219         mrq.cmd = cmd;
220         cmd->data = NULL;
221
222         mmc_wait_for_req(host, &mrq);
223
224         return cmd->error;
225 }
226
227 EXPORT_SYMBOL(mmc_wait_for_cmd);
228
229 /**
230  *      mmc_set_data_timeout - set the timeout for a data command
231  *      @data: data phase for command
232  *      @card: the MMC card associated with the data transfer
233  *      @write: flag to differentiate reads from writes
234  *
235  *      Computes the data timeout parameters according to the
236  *      correct algorithm given the card type.
237  */
238 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
239                           int write)
240 {
241         unsigned int mult;
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 (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 (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  *
288  *      Claim a host for a set of operations.
289  */
290 void mmc_claim_host(struct mmc_host *host)
291 {
292         DECLARE_WAITQUEUE(wait, current);
293         unsigned long flags;
294
295         might_sleep();
296
297         add_wait_queue(&host->wq, &wait);
298         spin_lock_irqsave(&host->lock, flags);
299         while (1) {
300                 set_current_state(TASK_UNINTERRUPTIBLE);
301                 if (!host->claimed)
302                         break;
303                 spin_unlock_irqrestore(&host->lock, flags);
304                 schedule();
305                 spin_lock_irqsave(&host->lock, flags);
306         }
307         set_current_state(TASK_RUNNING);
308         host->claimed = 1;
309         spin_unlock_irqrestore(&host->lock, flags);
310         remove_wait_queue(&host->wq, &wait);
311 }
312
313 EXPORT_SYMBOL(mmc_claim_host);
314
315 /**
316  *      mmc_release_host - release a host
317  *      @host: mmc host to release
318  *
319  *      Release a MMC host, allowing others to claim the host
320  *      for their operations.
321  */
322 void mmc_release_host(struct mmc_host *host)
323 {
324         unsigned long flags;
325
326         BUG_ON(!host->claimed);
327
328         spin_lock_irqsave(&host->lock, flags);
329         host->claimed = 0;
330         spin_unlock_irqrestore(&host->lock, flags);
331
332         wake_up(&host->wq);
333 }
334
335 EXPORT_SYMBOL(mmc_release_host);
336
337 /*
338  * Internal function that does the actual ios call to the host driver,
339  * optionally printing some debug output.
340  */
341 static inline void mmc_set_ios(struct mmc_host *host)
342 {
343         struct mmc_ios *ios = &host->ios;
344
345         pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
346                 "width %u timing %u\n",
347                  mmc_hostname(host), ios->clock, ios->bus_mode,
348                  ios->power_mode, ios->chip_select, ios->vdd,
349                  ios->bus_width, ios->timing);
350
351         host->ops->set_ios(host, ios);
352 }
353
354 /*
355  * Control chip select pin on a host.
356  */
357 void mmc_set_chip_select(struct mmc_host *host, int mode)
358 {
359         host->ios.chip_select = mode;
360         mmc_set_ios(host);
361 }
362
363 /*
364  * Sets the host clock to the highest possible frequency that
365  * is below "hz".
366  */
367 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
368 {
369         WARN_ON(hz < host->f_min);
370
371         if (hz > host->f_max)
372                 hz = host->f_max;
373
374         host->ios.clock = hz;
375         mmc_set_ios(host);
376 }
377
378 /*
379  * Change the bus mode (open drain/push-pull) of a host.
380  */
381 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
382 {
383         host->ios.bus_mode = mode;
384         mmc_set_ios(host);
385 }
386
387 /*
388  * Change data bus width of a host.
389  */
390 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
391 {
392         host->ios.bus_width = width;
393         mmc_set_ios(host);
394 }
395
396 /*
397  * Mask off any voltages we don't support and select
398  * the lowest voltage
399  */
400 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
401 {
402         int bit;
403
404         ocr &= host->ocr_avail;
405
406         bit = ffs(ocr);
407         if (bit) {
408                 bit -= 1;
409
410                 ocr &= 3 << bit;
411
412                 host->ios.vdd = bit;
413                 mmc_set_ios(host);
414         } else {
415                 ocr = 0;
416         }
417
418         return ocr;
419 }
420
421 /*
422  * Select timing parameters for host.
423  */
424 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
425 {
426         host->ios.timing = timing;
427         mmc_set_ios(host);
428 }
429
430 /*
431  * Apply power to the MMC stack.  This is a two-stage process.
432  * First, we enable power to the card without the clock running.
433  * We then wait a bit for the power to stabilise.  Finally,
434  * enable the bus drivers and clock to the card.
435  *
436  * We must _NOT_ enable the clock prior to power stablising.
437  *
438  * If a host does all the power sequencing itself, ignore the
439  * initial MMC_POWER_UP stage.
440  */
441 static void mmc_power_up(struct mmc_host *host)
442 {
443         int bit = fls(host->ocr_avail) - 1;
444
445         host->ios.vdd = bit;
446         host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
447         host->ios.chip_select = MMC_CS_DONTCARE;
448         host->ios.power_mode = MMC_POWER_UP;
449         host->ios.bus_width = MMC_BUS_WIDTH_1;
450         host->ios.timing = MMC_TIMING_LEGACY;
451         mmc_set_ios(host);
452
453         mmc_delay(1);
454
455         host->ios.clock = host->f_min;
456         host->ios.power_mode = MMC_POWER_ON;
457         mmc_set_ios(host);
458
459         mmc_delay(2);
460 }
461
462 static void mmc_power_off(struct mmc_host *host)
463 {
464         host->ios.clock = 0;
465         host->ios.vdd = 0;
466         host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
467         host->ios.chip_select = MMC_CS_DONTCARE;
468         host->ios.power_mode = MMC_POWER_OFF;
469         host->ios.bus_width = MMC_BUS_WIDTH_1;
470         host->ios.timing = MMC_TIMING_LEGACY;
471         mmc_set_ios(host);
472 }
473
474 /*
475  * Assign a mmc bus handler to a host. Only one bus handler may control a
476  * host at any given time.
477  */
478 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
479 {
480         unsigned long flags;
481
482         BUG_ON(!host);
483         BUG_ON(!ops);
484
485         BUG_ON(!host->claimed);
486
487         spin_lock_irqsave(&host->lock, flags);
488
489         BUG_ON(host->bus_ops);
490         BUG_ON(host->bus_refs);
491
492         host->bus_ops = ops;
493         host->bus_refs = 1;
494         host->bus_dead = 0;
495
496         spin_unlock_irqrestore(&host->lock, flags);
497 }
498
499 /*
500  * Remove the current bus handler from a host. Assumes that there are
501  * no interesting cards left, so the bus is powered down.
502  */
503 void mmc_detach_bus(struct mmc_host *host)
504 {
505         unsigned long flags;
506
507         BUG_ON(!host);
508
509         BUG_ON(!host->claimed);
510         BUG_ON(!host->bus_ops);
511
512         spin_lock_irqsave(&host->lock, flags);
513
514         host->bus_dead = 1;
515
516         spin_unlock_irqrestore(&host->lock, flags);
517
518         mmc_power_off(host);
519
520         mmc_bus_put(host);
521 }
522
523 /*
524  * Cleanup when the last reference to the bus operator is dropped.
525  */
526 void __mmc_release_bus(struct mmc_host *host)
527 {
528         BUG_ON(!host);
529         BUG_ON(host->bus_refs);
530         BUG_ON(!host->bus_dead);
531
532         host->bus_ops = NULL;
533 }
534
535 /**
536  *      mmc_detect_change - process change of state on a MMC socket
537  *      @host: host which changed state.
538  *      @delay: optional delay to wait before detection (jiffies)
539  *
540  *      MMC drivers should call this when they detect a card has been
541  *      inserted or removed. The MMC layer will confirm that any
542  *      present card is still functional, and initialize any newly
543  *      inserted.
544  */
545 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
546 {
547 #ifdef CONFIG_MMC_DEBUG
548         unsigned long flags;
549         spin_lock_irqsave(&host->lock, flags);
550         BUG_ON(host->removed);
551         spin_unlock_irqrestore(&host->lock, flags);
552 #endif
553
554         mmc_schedule_delayed_work(&host->detect, delay);
555 }
556
557 EXPORT_SYMBOL(mmc_detect_change);
558
559
560 void mmc_rescan(struct work_struct *work)
561 {
562         struct mmc_host *host =
563                 container_of(work, struct mmc_host, detect.work);
564         u32 ocr;
565         int err;
566
567         mmc_bus_get(host);
568
569         if (host->bus_ops == NULL) {
570                 /*
571                  * Only we can add a new handler, so it's safe to
572                  * release the lock here.
573                  */
574                 mmc_bus_put(host);
575
576                 mmc_claim_host(host);
577
578                 mmc_power_up(host);
579                 mmc_go_idle(host);
580
581                 mmc_send_if_cond(host, host->ocr_avail);
582
583                 err = mmc_send_app_op_cond(host, 0, &ocr);
584                 if (err == MMC_ERR_NONE) {
585                         if (mmc_attach_sd(host, ocr))
586                                 mmc_power_off(host);
587                 } else {
588                         /*
589                          * If we fail to detect any SD cards then try
590                          * searching for MMC cards.
591                          */
592                         err = mmc_send_op_cond(host, 0, &ocr);
593                         if (err == MMC_ERR_NONE) {
594                                 if (mmc_attach_mmc(host, ocr))
595                                         mmc_power_off(host);
596                         } else {
597                                 mmc_power_off(host);
598                                 mmc_release_host(host);
599                         }
600                 }
601         } else {
602                 if (host->bus_ops->detect && !host->bus_dead)
603                         host->bus_ops->detect(host);
604
605                 mmc_bus_put(host);
606         }
607 }
608
609 void mmc_start_host(struct mmc_host *host)
610 {
611         mmc_power_off(host);
612         mmc_detect_change(host, 0);
613 }
614
615 void mmc_stop_host(struct mmc_host *host)
616 {
617 #ifdef CONFIG_MMC_DEBUG
618         unsigned long flags;
619         spin_lock_irqsave(&host->lock, flags);
620         host->removed = 1;
621         spin_unlock_irqrestore(&host->lock, flags);
622 #endif
623
624         mmc_flush_scheduled_work();
625
626         mmc_bus_get(host);
627         if (host->bus_ops && !host->bus_dead) {
628                 if (host->bus_ops->remove)
629                         host->bus_ops->remove(host);
630
631                 mmc_claim_host(host);
632                 mmc_detach_bus(host);
633                 mmc_release_host(host);
634         }
635         mmc_bus_put(host);
636
637         BUG_ON(host->card);
638
639         mmc_power_off(host);
640 }
641
642 #ifdef CONFIG_PM
643
644 /**
645  *      mmc_suspend_host - suspend a host
646  *      @host: mmc host
647  *      @state: suspend mode (PM_SUSPEND_xxx)
648  */
649 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
650 {
651         mmc_flush_scheduled_work();
652
653         mmc_bus_get(host);
654         if (host->bus_ops && !host->bus_dead) {
655                 if (host->bus_ops->suspend)
656                         host->bus_ops->suspend(host);
657                 if (!host->bus_ops->resume) {
658                         if (host->bus_ops->remove)
659                                 host->bus_ops->remove(host);
660
661                         mmc_claim_host(host);
662                         mmc_detach_bus(host);
663                         mmc_release_host(host);
664                 }
665         }
666         mmc_bus_put(host);
667
668         mmc_power_off(host);
669
670         return 0;
671 }
672
673 EXPORT_SYMBOL(mmc_suspend_host);
674
675 /**
676  *      mmc_resume_host - resume a previously suspended host
677  *      @host: mmc host
678  */
679 int mmc_resume_host(struct mmc_host *host)
680 {
681         mmc_bus_get(host);
682         if (host->bus_ops && !host->bus_dead) {
683                 mmc_power_up(host);
684                 BUG_ON(!host->bus_ops->resume);
685                 host->bus_ops->resume(host);
686         }
687         mmc_bus_put(host);
688
689         /*
690          * We add a slight delay here so that resume can progress
691          * in parallel.
692          */
693         mmc_detect_change(host, 1);
694
695         return 0;
696 }
697
698 EXPORT_SYMBOL(mmc_resume_host);
699
700 #endif
701
702 static int __init mmc_init(void)
703 {
704         int ret;
705
706         workqueue = create_singlethread_workqueue("kmmcd");
707         if (!workqueue)
708                 return -ENOMEM;
709
710         ret = mmc_register_bus();
711         if (ret == 0) {
712                 ret = mmc_register_host_class();
713                 if (ret)
714                         mmc_unregister_bus();
715         }
716         return ret;
717 }
718
719 static void __exit mmc_exit(void)
720 {
721         mmc_unregister_host_class();
722         mmc_unregister_bus();
723         destroy_workqueue(workqueue);
724 }
725
726 module_init(mmc_init);
727 module_exit(mmc_exit);
728
729 MODULE_LICENSE("GPL");