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