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