xref: /openbmc/linux/drivers/mmc/core/core.c (revision f7777dcc)
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 #include <linux/log2.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/suspend.h>
27 #include <linux/fault-inject.h>
28 #include <linux/random.h>
29 #include <linux/slab.h>
30 #include <linux/of.h>
31 
32 #include <linux/mmc/card.h>
33 #include <linux/mmc/host.h>
34 #include <linux/mmc/mmc.h>
35 #include <linux/mmc/sd.h>
36 
37 #include "core.h"
38 #include "bus.h"
39 #include "host.h"
40 #include "sdio_bus.h"
41 
42 #include "mmc_ops.h"
43 #include "sd_ops.h"
44 #include "sdio_ops.h"
45 
46 /* If the device is not responding */
47 #define MMC_CORE_TIMEOUT_MS	(10 * 60 * 1000) /* 10 minute timeout */
48 
49 /*
50  * Background operations can take a long time, depending on the housekeeping
51  * operations the card has to perform.
52  */
53 #define MMC_BKOPS_MAX_TIMEOUT	(4 * 60 * 1000) /* max time to wait in ms */
54 
55 static struct workqueue_struct *workqueue;
56 static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
57 
58 /*
59  * Enabling software CRCs on the data blocks can be a significant (30%)
60  * performance cost, and for other reasons may not always be desired.
61  * So we allow it it to be disabled.
62  */
63 bool use_spi_crc = 1;
64 module_param(use_spi_crc, bool, 0);
65 
66 /*
67  * We normally treat cards as removed during suspend if they are not
68  * known to be on a non-removable bus, to avoid the risk of writing
69  * back data to a different card after resume.  Allow this to be
70  * overridden if necessary.
71  */
72 #ifdef CONFIG_MMC_UNSAFE_RESUME
73 bool mmc_assume_removable;
74 #else
75 bool mmc_assume_removable = 1;
76 #endif
77 EXPORT_SYMBOL(mmc_assume_removable);
78 module_param_named(removable, mmc_assume_removable, bool, 0644);
79 MODULE_PARM_DESC(
80 	removable,
81 	"MMC/SD cards are removable and may be removed during suspend");
82 
83 /*
84  * Internal function. Schedule delayed work in the MMC work queue.
85  */
86 static int mmc_schedule_delayed_work(struct delayed_work *work,
87 				     unsigned long delay)
88 {
89 	return queue_delayed_work(workqueue, work, delay);
90 }
91 
92 /*
93  * Internal function. Flush all scheduled work from the MMC work queue.
94  */
95 static void mmc_flush_scheduled_work(void)
96 {
97 	flush_workqueue(workqueue);
98 }
99 
100 #ifdef CONFIG_FAIL_MMC_REQUEST
101 
102 /*
103  * Internal function. Inject random data errors.
104  * If mmc_data is NULL no errors are injected.
105  */
106 static void mmc_should_fail_request(struct mmc_host *host,
107 				    struct mmc_request *mrq)
108 {
109 	struct mmc_command *cmd = mrq->cmd;
110 	struct mmc_data *data = mrq->data;
111 	static const int data_errors[] = {
112 		-ETIMEDOUT,
113 		-EILSEQ,
114 		-EIO,
115 	};
116 
117 	if (!data)
118 		return;
119 
120 	if (cmd->error || data->error ||
121 	    !should_fail(&host->fail_mmc_request, data->blksz * data->blocks))
122 		return;
123 
124 	data->error = data_errors[prandom_u32() % ARRAY_SIZE(data_errors)];
125 	data->bytes_xfered = (prandom_u32() % (data->bytes_xfered >> 9)) << 9;
126 }
127 
128 #else /* CONFIG_FAIL_MMC_REQUEST */
129 
130 static inline void mmc_should_fail_request(struct mmc_host *host,
131 					   struct mmc_request *mrq)
132 {
133 }
134 
135 #endif /* CONFIG_FAIL_MMC_REQUEST */
136 
137 /**
138  *	mmc_request_done - finish processing an MMC request
139  *	@host: MMC host which completed request
140  *	@mrq: MMC request which request
141  *
142  *	MMC drivers should call this function when they have completed
143  *	their processing of a request.
144  */
145 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
146 {
147 	struct mmc_command *cmd = mrq->cmd;
148 	int err = cmd->error;
149 
150 	if (err && cmd->retries && mmc_host_is_spi(host)) {
151 		if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
152 			cmd->retries = 0;
153 	}
154 
155 	if (err && cmd->retries && !mmc_card_removed(host->card)) {
156 		/*
157 		 * Request starter must handle retries - see
158 		 * mmc_wait_for_req_done().
159 		 */
160 		if (mrq->done)
161 			mrq->done(mrq);
162 	} else {
163 		mmc_should_fail_request(host, mrq);
164 
165 		led_trigger_event(host->led, LED_OFF);
166 
167 		pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
168 			mmc_hostname(host), cmd->opcode, err,
169 			cmd->resp[0], cmd->resp[1],
170 			cmd->resp[2], cmd->resp[3]);
171 
172 		if (mrq->data) {
173 			pr_debug("%s:     %d bytes transferred: %d\n",
174 				mmc_hostname(host),
175 				mrq->data->bytes_xfered, mrq->data->error);
176 		}
177 
178 		if (mrq->stop) {
179 			pr_debug("%s:     (CMD%u): %d: %08x %08x %08x %08x\n",
180 				mmc_hostname(host), mrq->stop->opcode,
181 				mrq->stop->error,
182 				mrq->stop->resp[0], mrq->stop->resp[1],
183 				mrq->stop->resp[2], mrq->stop->resp[3]);
184 		}
185 
186 		if (mrq->done)
187 			mrq->done(mrq);
188 
189 		mmc_host_clk_release(host);
190 	}
191 }
192 
193 EXPORT_SYMBOL(mmc_request_done);
194 
195 static void
196 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
197 {
198 #ifdef CONFIG_MMC_DEBUG
199 	unsigned int i, sz;
200 	struct scatterlist *sg;
201 #endif
202 
203 	if (mrq->sbc) {
204 		pr_debug("<%s: starting CMD%u arg %08x flags %08x>\n",
205 			 mmc_hostname(host), mrq->sbc->opcode,
206 			 mrq->sbc->arg, mrq->sbc->flags);
207 	}
208 
209 	pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
210 		 mmc_hostname(host), mrq->cmd->opcode,
211 		 mrq->cmd->arg, mrq->cmd->flags);
212 
213 	if (mrq->data) {
214 		pr_debug("%s:     blksz %d blocks %d flags %08x "
215 			"tsac %d ms nsac %d\n",
216 			mmc_hostname(host), mrq->data->blksz,
217 			mrq->data->blocks, mrq->data->flags,
218 			mrq->data->timeout_ns / 1000000,
219 			mrq->data->timeout_clks);
220 	}
221 
222 	if (mrq->stop) {
223 		pr_debug("%s:     CMD%u arg %08x flags %08x\n",
224 			 mmc_hostname(host), mrq->stop->opcode,
225 			 mrq->stop->arg, mrq->stop->flags);
226 	}
227 
228 	WARN_ON(!host->claimed);
229 
230 	mrq->cmd->error = 0;
231 	mrq->cmd->mrq = mrq;
232 	if (mrq->data) {
233 		BUG_ON(mrq->data->blksz > host->max_blk_size);
234 		BUG_ON(mrq->data->blocks > host->max_blk_count);
235 		BUG_ON(mrq->data->blocks * mrq->data->blksz >
236 			host->max_req_size);
237 
238 #ifdef CONFIG_MMC_DEBUG
239 		sz = 0;
240 		for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
241 			sz += sg->length;
242 		BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
243 #endif
244 
245 		mrq->cmd->data = mrq->data;
246 		mrq->data->error = 0;
247 		mrq->data->mrq = mrq;
248 		if (mrq->stop) {
249 			mrq->data->stop = mrq->stop;
250 			mrq->stop->error = 0;
251 			mrq->stop->mrq = mrq;
252 		}
253 	}
254 	mmc_host_clk_hold(host);
255 	led_trigger_event(host->led, LED_FULL);
256 	host->ops->request(host, mrq);
257 }
258 
259 /**
260  *	mmc_start_bkops - start BKOPS for supported cards
261  *	@card: MMC card to start BKOPS
262  *	@form_exception: A flag to indicate if this function was
263  *			 called due to an exception raised by the card
264  *
265  *	Start background operations whenever requested.
266  *	When the urgent BKOPS bit is set in a R1 command response
267  *	then background operations should be started immediately.
268 */
269 void mmc_start_bkops(struct mmc_card *card, bool from_exception)
270 {
271 	int err;
272 	int timeout;
273 	bool use_busy_signal;
274 
275 	BUG_ON(!card);
276 
277 	if (!card->ext_csd.bkops_en || mmc_card_doing_bkops(card))
278 		return;
279 
280 	err = mmc_read_bkops_status(card);
281 	if (err) {
282 		pr_err("%s: Failed to read bkops status: %d\n",
283 		       mmc_hostname(card->host), err);
284 		return;
285 	}
286 
287 	if (!card->ext_csd.raw_bkops_status)
288 		return;
289 
290 	if (card->ext_csd.raw_bkops_status < EXT_CSD_BKOPS_LEVEL_2 &&
291 	    from_exception)
292 		return;
293 
294 	mmc_claim_host(card->host);
295 	if (card->ext_csd.raw_bkops_status >= EXT_CSD_BKOPS_LEVEL_2) {
296 		timeout = MMC_BKOPS_MAX_TIMEOUT;
297 		use_busy_signal = true;
298 	} else {
299 		timeout = 0;
300 		use_busy_signal = false;
301 	}
302 
303 	err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
304 			EXT_CSD_BKOPS_START, 1, timeout, use_busy_signal);
305 	if (err) {
306 		pr_warn("%s: Error %d starting bkops\n",
307 			mmc_hostname(card->host), err);
308 		goto out;
309 	}
310 
311 	/*
312 	 * For urgent bkops status (LEVEL_2 and more)
313 	 * bkops executed synchronously, otherwise
314 	 * the operation is in progress
315 	 */
316 	if (!use_busy_signal)
317 		mmc_card_set_doing_bkops(card);
318 out:
319 	mmc_release_host(card->host);
320 }
321 EXPORT_SYMBOL(mmc_start_bkops);
322 
323 /*
324  * mmc_wait_data_done() - done callback for data request
325  * @mrq: done data request
326  *
327  * Wakes up mmc context, passed as a callback to host controller driver
328  */
329 static void mmc_wait_data_done(struct mmc_request *mrq)
330 {
331 	mrq->host->context_info.is_done_rcv = true;
332 	wake_up_interruptible(&mrq->host->context_info.wait);
333 }
334 
335 static void mmc_wait_done(struct mmc_request *mrq)
336 {
337 	complete(&mrq->completion);
338 }
339 
340 /*
341  *__mmc_start_data_req() - starts data request
342  * @host: MMC host to start the request
343  * @mrq: data request to start
344  *
345  * Sets the done callback to be called when request is completed by the card.
346  * Starts data mmc request execution
347  */
348 static int __mmc_start_data_req(struct mmc_host *host, struct mmc_request *mrq)
349 {
350 	mrq->done = mmc_wait_data_done;
351 	mrq->host = host;
352 	if (mmc_card_removed(host->card)) {
353 		mrq->cmd->error = -ENOMEDIUM;
354 		mmc_wait_data_done(mrq);
355 		return -ENOMEDIUM;
356 	}
357 	mmc_start_request(host, mrq);
358 
359 	return 0;
360 }
361 
362 static int __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq)
363 {
364 	init_completion(&mrq->completion);
365 	mrq->done = mmc_wait_done;
366 	if (mmc_card_removed(host->card)) {
367 		mrq->cmd->error = -ENOMEDIUM;
368 		complete(&mrq->completion);
369 		return -ENOMEDIUM;
370 	}
371 	mmc_start_request(host, mrq);
372 	return 0;
373 }
374 
375 /*
376  * mmc_wait_for_data_req_done() - wait for request completed
377  * @host: MMC host to prepare the command.
378  * @mrq: MMC request to wait for
379  *
380  * Blocks MMC context till host controller will ack end of data request
381  * execution or new request notification arrives from the block layer.
382  * Handles command retries.
383  *
384  * Returns enum mmc_blk_status after checking errors.
385  */
386 static int mmc_wait_for_data_req_done(struct mmc_host *host,
387 				      struct mmc_request *mrq,
388 				      struct mmc_async_req *next_req)
389 {
390 	struct mmc_command *cmd;
391 	struct mmc_context_info *context_info = &host->context_info;
392 	int err;
393 	unsigned long flags;
394 
395 	while (1) {
396 		wait_event_interruptible(context_info->wait,
397 				(context_info->is_done_rcv ||
398 				 context_info->is_new_req));
399 		spin_lock_irqsave(&context_info->lock, flags);
400 		context_info->is_waiting_last_req = false;
401 		spin_unlock_irqrestore(&context_info->lock, flags);
402 		if (context_info->is_done_rcv) {
403 			context_info->is_done_rcv = false;
404 			context_info->is_new_req = false;
405 			cmd = mrq->cmd;
406 
407 			if (!cmd->error || !cmd->retries ||
408 			    mmc_card_removed(host->card)) {
409 				err = host->areq->err_check(host->card,
410 							    host->areq);
411 				break; /* return err */
412 			} else {
413 				pr_info("%s: req failed (CMD%u): %d, retrying...\n",
414 					mmc_hostname(host),
415 					cmd->opcode, cmd->error);
416 				cmd->retries--;
417 				cmd->error = 0;
418 				host->ops->request(host, mrq);
419 				continue; /* wait for done/new event again */
420 			}
421 		} else if (context_info->is_new_req) {
422 			context_info->is_new_req = false;
423 			if (!next_req) {
424 				err = MMC_BLK_NEW_REQUEST;
425 				break; /* return err */
426 			}
427 		}
428 	}
429 	return err;
430 }
431 
432 static void mmc_wait_for_req_done(struct mmc_host *host,
433 				  struct mmc_request *mrq)
434 {
435 	struct mmc_command *cmd;
436 
437 	while (1) {
438 		wait_for_completion(&mrq->completion);
439 
440 		cmd = mrq->cmd;
441 
442 		/*
443 		 * If host has timed out waiting for the sanitize
444 		 * to complete, card might be still in programming state
445 		 * so let's try to bring the card out of programming
446 		 * state.
447 		 */
448 		if (cmd->sanitize_busy && cmd->error == -ETIMEDOUT) {
449 			if (!mmc_interrupt_hpi(host->card)) {
450 				pr_warning("%s: %s: Interrupted sanitize\n",
451 					   mmc_hostname(host), __func__);
452 				cmd->error = 0;
453 				break;
454 			} else {
455 				pr_err("%s: %s: Failed to interrupt sanitize\n",
456 				       mmc_hostname(host), __func__);
457 			}
458 		}
459 		if (!cmd->error || !cmd->retries ||
460 		    mmc_card_removed(host->card))
461 			break;
462 
463 		pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
464 			 mmc_hostname(host), cmd->opcode, cmd->error);
465 		cmd->retries--;
466 		cmd->error = 0;
467 		host->ops->request(host, mrq);
468 	}
469 }
470 
471 /**
472  *	mmc_pre_req - Prepare for a new request
473  *	@host: MMC host to prepare command
474  *	@mrq: MMC request to prepare for
475  *	@is_first_req: true if there is no previous started request
476  *                     that may run in parellel to this call, otherwise false
477  *
478  *	mmc_pre_req() is called in prior to mmc_start_req() to let
479  *	host prepare for the new request. Preparation of a request may be
480  *	performed while another request is running on the host.
481  */
482 static void mmc_pre_req(struct mmc_host *host, struct mmc_request *mrq,
483 		 bool is_first_req)
484 {
485 	if (host->ops->pre_req) {
486 		mmc_host_clk_hold(host);
487 		host->ops->pre_req(host, mrq, is_first_req);
488 		mmc_host_clk_release(host);
489 	}
490 }
491 
492 /**
493  *	mmc_post_req - Post process a completed request
494  *	@host: MMC host to post process command
495  *	@mrq: MMC request to post process for
496  *	@err: Error, if non zero, clean up any resources made in pre_req
497  *
498  *	Let the host post process a completed request. Post processing of
499  *	a request may be performed while another reuqest is running.
500  */
501 static void mmc_post_req(struct mmc_host *host, struct mmc_request *mrq,
502 			 int err)
503 {
504 	if (host->ops->post_req) {
505 		mmc_host_clk_hold(host);
506 		host->ops->post_req(host, mrq, err);
507 		mmc_host_clk_release(host);
508 	}
509 }
510 
511 /**
512  *	mmc_start_req - start a non-blocking request
513  *	@host: MMC host to start command
514  *	@areq: async request to start
515  *	@error: out parameter returns 0 for success, otherwise non zero
516  *
517  *	Start a new MMC custom command request for a host.
518  *	If there is on ongoing async request wait for completion
519  *	of that request and start the new one and return.
520  *	Does not wait for the new request to complete.
521  *
522  *      Returns the completed request, NULL in case of none completed.
523  *	Wait for the an ongoing request (previoulsy started) to complete and
524  *	return the completed request. If there is no ongoing request, NULL
525  *	is returned without waiting. NULL is not an error condition.
526  */
527 struct mmc_async_req *mmc_start_req(struct mmc_host *host,
528 				    struct mmc_async_req *areq, int *error)
529 {
530 	int err = 0;
531 	int start_err = 0;
532 	struct mmc_async_req *data = host->areq;
533 
534 	/* Prepare a new request */
535 	if (areq)
536 		mmc_pre_req(host, areq->mrq, !host->areq);
537 
538 	if (host->areq) {
539 		err = mmc_wait_for_data_req_done(host, host->areq->mrq,	areq);
540 		if (err == MMC_BLK_NEW_REQUEST) {
541 			if (error)
542 				*error = err;
543 			/*
544 			 * The previous request was not completed,
545 			 * nothing to return
546 			 */
547 			return NULL;
548 		}
549 		/*
550 		 * Check BKOPS urgency for each R1 response
551 		 */
552 		if (host->card && mmc_card_mmc(host->card) &&
553 		    ((mmc_resp_type(host->areq->mrq->cmd) == MMC_RSP_R1) ||
554 		     (mmc_resp_type(host->areq->mrq->cmd) == MMC_RSP_R1B)) &&
555 		    (host->areq->mrq->cmd->resp[0] & R1_EXCEPTION_EVENT))
556 			mmc_start_bkops(host->card, true);
557 	}
558 
559 	if (!err && areq)
560 		start_err = __mmc_start_data_req(host, areq->mrq);
561 
562 	if (host->areq)
563 		mmc_post_req(host, host->areq->mrq, 0);
564 
565 	 /* Cancel a prepared request if it was not started. */
566 	if ((err || start_err) && areq)
567 		mmc_post_req(host, areq->mrq, -EINVAL);
568 
569 	if (err)
570 		host->areq = NULL;
571 	else
572 		host->areq = areq;
573 
574 	if (error)
575 		*error = err;
576 	return data;
577 }
578 EXPORT_SYMBOL(mmc_start_req);
579 
580 /**
581  *	mmc_wait_for_req - start a request and wait for completion
582  *	@host: MMC host to start command
583  *	@mrq: MMC request to start
584  *
585  *	Start a new MMC custom command request for a host, and wait
586  *	for the command to complete. Does not attempt to parse the
587  *	response.
588  */
589 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
590 {
591 	__mmc_start_req(host, mrq);
592 	mmc_wait_for_req_done(host, mrq);
593 }
594 EXPORT_SYMBOL(mmc_wait_for_req);
595 
596 /**
597  *	mmc_interrupt_hpi - Issue for High priority Interrupt
598  *	@card: the MMC card associated with the HPI transfer
599  *
600  *	Issued High Priority Interrupt, and check for card status
601  *	until out-of prg-state.
602  */
603 int mmc_interrupt_hpi(struct mmc_card *card)
604 {
605 	int err;
606 	u32 status;
607 	unsigned long prg_wait;
608 
609 	BUG_ON(!card);
610 
611 	if (!card->ext_csd.hpi_en) {
612 		pr_info("%s: HPI enable bit unset\n", mmc_hostname(card->host));
613 		return 1;
614 	}
615 
616 	mmc_claim_host(card->host);
617 	err = mmc_send_status(card, &status);
618 	if (err) {
619 		pr_err("%s: Get card status fail\n", mmc_hostname(card->host));
620 		goto out;
621 	}
622 
623 	switch (R1_CURRENT_STATE(status)) {
624 	case R1_STATE_IDLE:
625 	case R1_STATE_READY:
626 	case R1_STATE_STBY:
627 	case R1_STATE_TRAN:
628 		/*
629 		 * In idle and transfer states, HPI is not needed and the caller
630 		 * can issue the next intended command immediately
631 		 */
632 		goto out;
633 	case R1_STATE_PRG:
634 		break;
635 	default:
636 		/* In all other states, it's illegal to issue HPI */
637 		pr_debug("%s: HPI cannot be sent. Card state=%d\n",
638 			mmc_hostname(card->host), R1_CURRENT_STATE(status));
639 		err = -EINVAL;
640 		goto out;
641 	}
642 
643 	err = mmc_send_hpi_cmd(card, &status);
644 	if (err)
645 		goto out;
646 
647 	prg_wait = jiffies + msecs_to_jiffies(card->ext_csd.out_of_int_time);
648 	do {
649 		err = mmc_send_status(card, &status);
650 
651 		if (!err && R1_CURRENT_STATE(status) == R1_STATE_TRAN)
652 			break;
653 		if (time_after(jiffies, prg_wait))
654 			err = -ETIMEDOUT;
655 	} while (!err);
656 
657 out:
658 	mmc_release_host(card->host);
659 	return err;
660 }
661 EXPORT_SYMBOL(mmc_interrupt_hpi);
662 
663 /**
664  *	mmc_wait_for_cmd - start a command and wait for completion
665  *	@host: MMC host to start command
666  *	@cmd: MMC command to start
667  *	@retries: maximum number of retries
668  *
669  *	Start a new MMC command for a host, and wait for the command
670  *	to complete.  Return any error that occurred while the command
671  *	was executing.  Do not attempt to parse the response.
672  */
673 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
674 {
675 	struct mmc_request mrq = {NULL};
676 
677 	WARN_ON(!host->claimed);
678 
679 	memset(cmd->resp, 0, sizeof(cmd->resp));
680 	cmd->retries = retries;
681 
682 	mrq.cmd = cmd;
683 	cmd->data = NULL;
684 
685 	mmc_wait_for_req(host, &mrq);
686 
687 	return cmd->error;
688 }
689 
690 EXPORT_SYMBOL(mmc_wait_for_cmd);
691 
692 /**
693  *	mmc_stop_bkops - stop ongoing BKOPS
694  *	@card: MMC card to check BKOPS
695  *
696  *	Send HPI command to stop ongoing background operations to
697  *	allow rapid servicing of foreground operations, e.g. read/
698  *	writes. Wait until the card comes out of the programming state
699  *	to avoid errors in servicing read/write requests.
700  */
701 int mmc_stop_bkops(struct mmc_card *card)
702 {
703 	int err = 0;
704 
705 	BUG_ON(!card);
706 	err = mmc_interrupt_hpi(card);
707 
708 	/*
709 	 * If err is EINVAL, we can't issue an HPI.
710 	 * It should complete the BKOPS.
711 	 */
712 	if (!err || (err == -EINVAL)) {
713 		mmc_card_clr_doing_bkops(card);
714 		err = 0;
715 	}
716 
717 	return err;
718 }
719 EXPORT_SYMBOL(mmc_stop_bkops);
720 
721 int mmc_read_bkops_status(struct mmc_card *card)
722 {
723 	int err;
724 	u8 *ext_csd;
725 
726 	/*
727 	 * In future work, we should consider storing the entire ext_csd.
728 	 */
729 	ext_csd = kmalloc(512, GFP_KERNEL);
730 	if (!ext_csd) {
731 		pr_err("%s: could not allocate buffer to receive the ext_csd.\n",
732 		       mmc_hostname(card->host));
733 		return -ENOMEM;
734 	}
735 
736 	mmc_claim_host(card->host);
737 	err = mmc_send_ext_csd(card, ext_csd);
738 	mmc_release_host(card->host);
739 	if (err)
740 		goto out;
741 
742 	card->ext_csd.raw_bkops_status = ext_csd[EXT_CSD_BKOPS_STATUS];
743 	card->ext_csd.raw_exception_status = ext_csd[EXT_CSD_EXP_EVENTS_STATUS];
744 out:
745 	kfree(ext_csd);
746 	return err;
747 }
748 EXPORT_SYMBOL(mmc_read_bkops_status);
749 
750 /**
751  *	mmc_set_data_timeout - set the timeout for a data command
752  *	@data: data phase for command
753  *	@card: the MMC card associated with the data transfer
754  *
755  *	Computes the data timeout parameters according to the
756  *	correct algorithm given the card type.
757  */
758 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
759 {
760 	unsigned int mult;
761 
762 	/*
763 	 * SDIO cards only define an upper 1 s limit on access.
764 	 */
765 	if (mmc_card_sdio(card)) {
766 		data->timeout_ns = 1000000000;
767 		data->timeout_clks = 0;
768 		return;
769 	}
770 
771 	/*
772 	 * SD cards use a 100 multiplier rather than 10
773 	 */
774 	mult = mmc_card_sd(card) ? 100 : 10;
775 
776 	/*
777 	 * Scale up the multiplier (and therefore the timeout) by
778 	 * the r2w factor for writes.
779 	 */
780 	if (data->flags & MMC_DATA_WRITE)
781 		mult <<= card->csd.r2w_factor;
782 
783 	data->timeout_ns = card->csd.tacc_ns * mult;
784 	data->timeout_clks = card->csd.tacc_clks * mult;
785 
786 	/*
787 	 * SD cards also have an upper limit on the timeout.
788 	 */
789 	if (mmc_card_sd(card)) {
790 		unsigned int timeout_us, limit_us;
791 
792 		timeout_us = data->timeout_ns / 1000;
793 		if (mmc_host_clk_rate(card->host))
794 			timeout_us += data->timeout_clks * 1000 /
795 				(mmc_host_clk_rate(card->host) / 1000);
796 
797 		if (data->flags & MMC_DATA_WRITE)
798 			/*
799 			 * The MMC spec "It is strongly recommended
800 			 * for hosts to implement more than 500ms
801 			 * timeout value even if the card indicates
802 			 * the 250ms maximum busy length."  Even the
803 			 * previous value of 300ms is known to be
804 			 * insufficient for some cards.
805 			 */
806 			limit_us = 3000000;
807 		else
808 			limit_us = 100000;
809 
810 		/*
811 		 * SDHC cards always use these fixed values.
812 		 */
813 		if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
814 			data->timeout_ns = limit_us * 1000;
815 			data->timeout_clks = 0;
816 		}
817 	}
818 
819 	/*
820 	 * Some cards require longer data read timeout than indicated in CSD.
821 	 * Address this by setting the read timeout to a "reasonably high"
822 	 * value. For the cards tested, 300ms has proven enough. If necessary,
823 	 * this value can be increased if other problematic cards require this.
824 	 */
825 	if (mmc_card_long_read_time(card) && data->flags & MMC_DATA_READ) {
826 		data->timeout_ns = 300000000;
827 		data->timeout_clks = 0;
828 	}
829 
830 	/*
831 	 * Some cards need very high timeouts if driven in SPI mode.
832 	 * The worst observed timeout was 900ms after writing a
833 	 * continuous stream of data until the internal logic
834 	 * overflowed.
835 	 */
836 	if (mmc_host_is_spi(card->host)) {
837 		if (data->flags & MMC_DATA_WRITE) {
838 			if (data->timeout_ns < 1000000000)
839 				data->timeout_ns = 1000000000;	/* 1s */
840 		} else {
841 			if (data->timeout_ns < 100000000)
842 				data->timeout_ns =  100000000;	/* 100ms */
843 		}
844 	}
845 }
846 EXPORT_SYMBOL(mmc_set_data_timeout);
847 
848 /**
849  *	mmc_align_data_size - pads a transfer size to a more optimal value
850  *	@card: the MMC card associated with the data transfer
851  *	@sz: original transfer size
852  *
853  *	Pads the original data size with a number of extra bytes in
854  *	order to avoid controller bugs and/or performance hits
855  *	(e.g. some controllers revert to PIO for certain sizes).
856  *
857  *	Returns the improved size, which might be unmodified.
858  *
859  *	Note that this function is only relevant when issuing a
860  *	single scatter gather entry.
861  */
862 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
863 {
864 	/*
865 	 * FIXME: We don't have a system for the controller to tell
866 	 * the core about its problems yet, so for now we just 32-bit
867 	 * align the size.
868 	 */
869 	sz = ((sz + 3) / 4) * 4;
870 
871 	return sz;
872 }
873 EXPORT_SYMBOL(mmc_align_data_size);
874 
875 /**
876  *	__mmc_claim_host - exclusively claim a host
877  *	@host: mmc host to claim
878  *	@abort: whether or not the operation should be aborted
879  *
880  *	Claim a host for a set of operations.  If @abort is non null and
881  *	dereference a non-zero value then this will return prematurely with
882  *	that non-zero value without acquiring the lock.  Returns zero
883  *	with the lock held otherwise.
884  */
885 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
886 {
887 	DECLARE_WAITQUEUE(wait, current);
888 	unsigned long flags;
889 	int stop;
890 
891 	might_sleep();
892 
893 	add_wait_queue(&host->wq, &wait);
894 	spin_lock_irqsave(&host->lock, flags);
895 	while (1) {
896 		set_current_state(TASK_UNINTERRUPTIBLE);
897 		stop = abort ? atomic_read(abort) : 0;
898 		if (stop || !host->claimed || host->claimer == current)
899 			break;
900 		spin_unlock_irqrestore(&host->lock, flags);
901 		schedule();
902 		spin_lock_irqsave(&host->lock, flags);
903 	}
904 	set_current_state(TASK_RUNNING);
905 	if (!stop) {
906 		host->claimed = 1;
907 		host->claimer = current;
908 		host->claim_cnt += 1;
909 	} else
910 		wake_up(&host->wq);
911 	spin_unlock_irqrestore(&host->lock, flags);
912 	remove_wait_queue(&host->wq, &wait);
913 	if (host->ops->enable && !stop && host->claim_cnt == 1)
914 		host->ops->enable(host);
915 	return stop;
916 }
917 
918 EXPORT_SYMBOL(__mmc_claim_host);
919 
920 /**
921  *	mmc_try_claim_host - try exclusively to claim a host
922  *	@host: mmc host to claim
923  *
924  *	Returns %1 if the host is claimed, %0 otherwise.
925  */
926 int mmc_try_claim_host(struct mmc_host *host)
927 {
928 	int claimed_host = 0;
929 	unsigned long flags;
930 
931 	spin_lock_irqsave(&host->lock, flags);
932 	if (!host->claimed || host->claimer == current) {
933 		host->claimed = 1;
934 		host->claimer = current;
935 		host->claim_cnt += 1;
936 		claimed_host = 1;
937 	}
938 	spin_unlock_irqrestore(&host->lock, flags);
939 	if (host->ops->enable && claimed_host && host->claim_cnt == 1)
940 		host->ops->enable(host);
941 	return claimed_host;
942 }
943 EXPORT_SYMBOL(mmc_try_claim_host);
944 
945 /**
946  *	mmc_release_host - release a host
947  *	@host: mmc host to release
948  *
949  *	Release a MMC host, allowing others to claim the host
950  *	for their operations.
951  */
952 void mmc_release_host(struct mmc_host *host)
953 {
954 	unsigned long flags;
955 
956 	WARN_ON(!host->claimed);
957 
958 	if (host->ops->disable && host->claim_cnt == 1)
959 		host->ops->disable(host);
960 
961 	spin_lock_irqsave(&host->lock, flags);
962 	if (--host->claim_cnt) {
963 		/* Release for nested claim */
964 		spin_unlock_irqrestore(&host->lock, flags);
965 	} else {
966 		host->claimed = 0;
967 		host->claimer = NULL;
968 		spin_unlock_irqrestore(&host->lock, flags);
969 		wake_up(&host->wq);
970 	}
971 }
972 EXPORT_SYMBOL(mmc_release_host);
973 
974 /*
975  * This is a helper function, which fetches a runtime pm reference for the
976  * card device and also claims the host.
977  */
978 void mmc_get_card(struct mmc_card *card)
979 {
980 	pm_runtime_get_sync(&card->dev);
981 	mmc_claim_host(card->host);
982 }
983 EXPORT_SYMBOL(mmc_get_card);
984 
985 /*
986  * This is a helper function, which releases the host and drops the runtime
987  * pm reference for the card device.
988  */
989 void mmc_put_card(struct mmc_card *card)
990 {
991 	mmc_release_host(card->host);
992 	pm_runtime_mark_last_busy(&card->dev);
993 	pm_runtime_put_autosuspend(&card->dev);
994 }
995 EXPORT_SYMBOL(mmc_put_card);
996 
997 /*
998  * Internal function that does the actual ios call to the host driver,
999  * optionally printing some debug output.
1000  */
1001 static inline void mmc_set_ios(struct mmc_host *host)
1002 {
1003 	struct mmc_ios *ios = &host->ios;
1004 
1005 	pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
1006 		"width %u timing %u\n",
1007 		 mmc_hostname(host), ios->clock, ios->bus_mode,
1008 		 ios->power_mode, ios->chip_select, ios->vdd,
1009 		 ios->bus_width, ios->timing);
1010 
1011 	if (ios->clock > 0)
1012 		mmc_set_ungated(host);
1013 	host->ops->set_ios(host, ios);
1014 }
1015 
1016 /*
1017  * Control chip select pin on a host.
1018  */
1019 void mmc_set_chip_select(struct mmc_host *host, int mode)
1020 {
1021 	mmc_host_clk_hold(host);
1022 	host->ios.chip_select = mode;
1023 	mmc_set_ios(host);
1024 	mmc_host_clk_release(host);
1025 }
1026 
1027 /*
1028  * Sets the host clock to the highest possible frequency that
1029  * is below "hz".
1030  */
1031 static void __mmc_set_clock(struct mmc_host *host, unsigned int hz)
1032 {
1033 	WARN_ON(hz < host->f_min);
1034 
1035 	if (hz > host->f_max)
1036 		hz = host->f_max;
1037 
1038 	host->ios.clock = hz;
1039 	mmc_set_ios(host);
1040 }
1041 
1042 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
1043 {
1044 	mmc_host_clk_hold(host);
1045 	__mmc_set_clock(host, hz);
1046 	mmc_host_clk_release(host);
1047 }
1048 
1049 #ifdef CONFIG_MMC_CLKGATE
1050 /*
1051  * This gates the clock by setting it to 0 Hz.
1052  */
1053 void mmc_gate_clock(struct mmc_host *host)
1054 {
1055 	unsigned long flags;
1056 
1057 	spin_lock_irqsave(&host->clk_lock, flags);
1058 	host->clk_old = host->ios.clock;
1059 	host->ios.clock = 0;
1060 	host->clk_gated = true;
1061 	spin_unlock_irqrestore(&host->clk_lock, flags);
1062 	mmc_set_ios(host);
1063 }
1064 
1065 /*
1066  * This restores the clock from gating by using the cached
1067  * clock value.
1068  */
1069 void mmc_ungate_clock(struct mmc_host *host)
1070 {
1071 	/*
1072 	 * We should previously have gated the clock, so the clock shall
1073 	 * be 0 here! The clock may however be 0 during initialization,
1074 	 * when some request operations are performed before setting
1075 	 * the frequency. When ungate is requested in that situation
1076 	 * we just ignore the call.
1077 	 */
1078 	if (host->clk_old) {
1079 		BUG_ON(host->ios.clock);
1080 		/* This call will also set host->clk_gated to false */
1081 		__mmc_set_clock(host, host->clk_old);
1082 	}
1083 }
1084 
1085 void mmc_set_ungated(struct mmc_host *host)
1086 {
1087 	unsigned long flags;
1088 
1089 	/*
1090 	 * We've been given a new frequency while the clock is gated,
1091 	 * so make sure we regard this as ungating it.
1092 	 */
1093 	spin_lock_irqsave(&host->clk_lock, flags);
1094 	host->clk_gated = false;
1095 	spin_unlock_irqrestore(&host->clk_lock, flags);
1096 }
1097 
1098 #else
1099 void mmc_set_ungated(struct mmc_host *host)
1100 {
1101 }
1102 #endif
1103 
1104 /*
1105  * Change the bus mode (open drain/push-pull) of a host.
1106  */
1107 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
1108 {
1109 	mmc_host_clk_hold(host);
1110 	host->ios.bus_mode = mode;
1111 	mmc_set_ios(host);
1112 	mmc_host_clk_release(host);
1113 }
1114 
1115 /*
1116  * Change data bus width of a host.
1117  */
1118 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
1119 {
1120 	mmc_host_clk_hold(host);
1121 	host->ios.bus_width = width;
1122 	mmc_set_ios(host);
1123 	mmc_host_clk_release(host);
1124 }
1125 
1126 /**
1127  * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
1128  * @vdd:	voltage (mV)
1129  * @low_bits:	prefer low bits in boundary cases
1130  *
1131  * This function returns the OCR bit number according to the provided @vdd
1132  * value. If conversion is not possible a negative errno value returned.
1133  *
1134  * Depending on the @low_bits flag the function prefers low or high OCR bits
1135  * on boundary voltages. For example,
1136  * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
1137  * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
1138  *
1139  * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
1140  */
1141 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
1142 {
1143 	const int max_bit = ilog2(MMC_VDD_35_36);
1144 	int bit;
1145 
1146 	if (vdd < 1650 || vdd > 3600)
1147 		return -EINVAL;
1148 
1149 	if (vdd >= 1650 && vdd <= 1950)
1150 		return ilog2(MMC_VDD_165_195);
1151 
1152 	if (low_bits)
1153 		vdd -= 1;
1154 
1155 	/* Base 2000 mV, step 100 mV, bit's base 8. */
1156 	bit = (vdd - 2000) / 100 + 8;
1157 	if (bit > max_bit)
1158 		return max_bit;
1159 	return bit;
1160 }
1161 
1162 /**
1163  * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
1164  * @vdd_min:	minimum voltage value (mV)
1165  * @vdd_max:	maximum voltage value (mV)
1166  *
1167  * This function returns the OCR mask bits according to the provided @vdd_min
1168  * and @vdd_max values. If conversion is not possible the function returns 0.
1169  *
1170  * Notes wrt boundary cases:
1171  * This function sets the OCR bits for all boundary voltages, for example
1172  * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
1173  * MMC_VDD_34_35 mask.
1174  */
1175 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
1176 {
1177 	u32 mask = 0;
1178 
1179 	if (vdd_max < vdd_min)
1180 		return 0;
1181 
1182 	/* Prefer high bits for the boundary vdd_max values. */
1183 	vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
1184 	if (vdd_max < 0)
1185 		return 0;
1186 
1187 	/* Prefer low bits for the boundary vdd_min values. */
1188 	vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
1189 	if (vdd_min < 0)
1190 		return 0;
1191 
1192 	/* Fill the mask, from max bit to min bit. */
1193 	while (vdd_max >= vdd_min)
1194 		mask |= 1 << vdd_max--;
1195 
1196 	return mask;
1197 }
1198 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
1199 
1200 #ifdef CONFIG_OF
1201 
1202 /**
1203  * mmc_of_parse_voltage - return mask of supported voltages
1204  * @np: The device node need to be parsed.
1205  * @mask: mask of voltages available for MMC/SD/SDIO
1206  *
1207  * 1. Return zero on success.
1208  * 2. Return negative errno: voltage-range is invalid.
1209  */
1210 int mmc_of_parse_voltage(struct device_node *np, u32 *mask)
1211 {
1212 	const u32 *voltage_ranges;
1213 	int num_ranges, i;
1214 
1215 	voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges);
1216 	num_ranges = num_ranges / sizeof(*voltage_ranges) / 2;
1217 	if (!voltage_ranges || !num_ranges) {
1218 		pr_info("%s: voltage-ranges unspecified\n", np->full_name);
1219 		return -EINVAL;
1220 	}
1221 
1222 	for (i = 0; i < num_ranges; i++) {
1223 		const int j = i * 2;
1224 		u32 ocr_mask;
1225 
1226 		ocr_mask = mmc_vddrange_to_ocrmask(
1227 				be32_to_cpu(voltage_ranges[j]),
1228 				be32_to_cpu(voltage_ranges[j + 1]));
1229 		if (!ocr_mask) {
1230 			pr_err("%s: voltage-range #%d is invalid\n",
1231 				np->full_name, i);
1232 			return -EINVAL;
1233 		}
1234 		*mask |= ocr_mask;
1235 	}
1236 
1237 	return 0;
1238 }
1239 EXPORT_SYMBOL(mmc_of_parse_voltage);
1240 
1241 #endif /* CONFIG_OF */
1242 
1243 #ifdef CONFIG_REGULATOR
1244 
1245 /**
1246  * mmc_regulator_get_ocrmask - return mask of supported voltages
1247  * @supply: regulator to use
1248  *
1249  * This returns either a negative errno, or a mask of voltages that
1250  * can be provided to MMC/SD/SDIO devices using the specified voltage
1251  * regulator.  This would normally be called before registering the
1252  * MMC host adapter.
1253  */
1254 int mmc_regulator_get_ocrmask(struct regulator *supply)
1255 {
1256 	int			result = 0;
1257 	int			count;
1258 	int			i;
1259 
1260 	count = regulator_count_voltages(supply);
1261 	if (count < 0)
1262 		return count;
1263 
1264 	for (i = 0; i < count; i++) {
1265 		int		vdd_uV;
1266 		int		vdd_mV;
1267 
1268 		vdd_uV = regulator_list_voltage(supply, i);
1269 		if (vdd_uV <= 0)
1270 			continue;
1271 
1272 		vdd_mV = vdd_uV / 1000;
1273 		result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
1274 	}
1275 
1276 	return result;
1277 }
1278 EXPORT_SYMBOL_GPL(mmc_regulator_get_ocrmask);
1279 
1280 /**
1281  * mmc_regulator_set_ocr - set regulator to match host->ios voltage
1282  * @mmc: the host to regulate
1283  * @supply: regulator to use
1284  * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
1285  *
1286  * Returns zero on success, else negative errno.
1287  *
1288  * MMC host drivers may use this to enable or disable a regulator using
1289  * a particular supply voltage.  This would normally be called from the
1290  * set_ios() method.
1291  */
1292 int mmc_regulator_set_ocr(struct mmc_host *mmc,
1293 			struct regulator *supply,
1294 			unsigned short vdd_bit)
1295 {
1296 	int			result = 0;
1297 	int			min_uV, max_uV;
1298 
1299 	if (vdd_bit) {
1300 		int		tmp;
1301 		int		voltage;
1302 
1303 		/*
1304 		 * REVISIT mmc_vddrange_to_ocrmask() may have set some
1305 		 * bits this regulator doesn't quite support ... don't
1306 		 * be too picky, most cards and regulators are OK with
1307 		 * a 0.1V range goof (it's a small error percentage).
1308 		 */
1309 		tmp = vdd_bit - ilog2(MMC_VDD_165_195);
1310 		if (tmp == 0) {
1311 			min_uV = 1650 * 1000;
1312 			max_uV = 1950 * 1000;
1313 		} else {
1314 			min_uV = 1900 * 1000 + tmp * 100 * 1000;
1315 			max_uV = min_uV + 100 * 1000;
1316 		}
1317 
1318 		/*
1319 		 * If we're using a fixed/static regulator, don't call
1320 		 * regulator_set_voltage; it would fail.
1321 		 */
1322 		voltage = regulator_get_voltage(supply);
1323 
1324 		if (!regulator_can_change_voltage(supply))
1325 			min_uV = max_uV = voltage;
1326 
1327 		if (voltage < 0)
1328 			result = voltage;
1329 		else if (voltage < min_uV || voltage > max_uV)
1330 			result = regulator_set_voltage(supply, min_uV, max_uV);
1331 		else
1332 			result = 0;
1333 
1334 		if (result == 0 && !mmc->regulator_enabled) {
1335 			result = regulator_enable(supply);
1336 			if (!result)
1337 				mmc->regulator_enabled = true;
1338 		}
1339 	} else if (mmc->regulator_enabled) {
1340 		result = regulator_disable(supply);
1341 		if (result == 0)
1342 			mmc->regulator_enabled = false;
1343 	}
1344 
1345 	if (result)
1346 		dev_err(mmc_dev(mmc),
1347 			"could not set regulator OCR (%d)\n", result);
1348 	return result;
1349 }
1350 EXPORT_SYMBOL_GPL(mmc_regulator_set_ocr);
1351 
1352 int mmc_regulator_get_supply(struct mmc_host *mmc)
1353 {
1354 	struct device *dev = mmc_dev(mmc);
1355 	struct regulator *supply;
1356 	int ret;
1357 
1358 	supply = devm_regulator_get(dev, "vmmc");
1359 	mmc->supply.vmmc = supply;
1360 	mmc->supply.vqmmc = devm_regulator_get_optional(dev, "vqmmc");
1361 
1362 	if (IS_ERR(supply))
1363 		return PTR_ERR(supply);
1364 
1365 	ret = mmc_regulator_get_ocrmask(supply);
1366 	if (ret > 0)
1367 		mmc->ocr_avail = ret;
1368 	else
1369 		dev_warn(mmc_dev(mmc), "Failed getting OCR mask: %d\n", ret);
1370 
1371 	return 0;
1372 }
1373 EXPORT_SYMBOL_GPL(mmc_regulator_get_supply);
1374 
1375 #endif /* CONFIG_REGULATOR */
1376 
1377 /*
1378  * Mask off any voltages we don't support and select
1379  * the lowest voltage
1380  */
1381 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
1382 {
1383 	int bit;
1384 
1385 	ocr &= host->ocr_avail;
1386 
1387 	bit = ffs(ocr);
1388 	if (bit) {
1389 		bit -= 1;
1390 
1391 		ocr &= 3 << bit;
1392 
1393 		mmc_host_clk_hold(host);
1394 		host->ios.vdd = bit;
1395 		mmc_set_ios(host);
1396 		mmc_host_clk_release(host);
1397 	} else {
1398 		pr_warning("%s: host doesn't support card's voltages\n",
1399 				mmc_hostname(host));
1400 		ocr = 0;
1401 	}
1402 
1403 	return ocr;
1404 }
1405 
1406 int __mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage)
1407 {
1408 	int err = 0;
1409 	int old_signal_voltage = host->ios.signal_voltage;
1410 
1411 	host->ios.signal_voltage = signal_voltage;
1412 	if (host->ops->start_signal_voltage_switch) {
1413 		mmc_host_clk_hold(host);
1414 		err = host->ops->start_signal_voltage_switch(host, &host->ios);
1415 		mmc_host_clk_release(host);
1416 	}
1417 
1418 	if (err)
1419 		host->ios.signal_voltage = old_signal_voltage;
1420 
1421 	return err;
1422 
1423 }
1424 
1425 int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage)
1426 {
1427 	struct mmc_command cmd = {0};
1428 	int err = 0;
1429 	u32 clock;
1430 
1431 	BUG_ON(!host);
1432 
1433 	/*
1434 	 * Send CMD11 only if the request is to switch the card to
1435 	 * 1.8V signalling.
1436 	 */
1437 	if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
1438 		return __mmc_set_signal_voltage(host, signal_voltage);
1439 
1440 	/*
1441 	 * If we cannot switch voltages, return failure so the caller
1442 	 * can continue without UHS mode
1443 	 */
1444 	if (!host->ops->start_signal_voltage_switch)
1445 		return -EPERM;
1446 	if (!host->ops->card_busy)
1447 		pr_warning("%s: cannot verify signal voltage switch\n",
1448 				mmc_hostname(host));
1449 
1450 	cmd.opcode = SD_SWITCH_VOLTAGE;
1451 	cmd.arg = 0;
1452 	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1453 
1454 	err = mmc_wait_for_cmd(host, &cmd, 0);
1455 	if (err)
1456 		return err;
1457 
1458 	if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
1459 		return -EIO;
1460 
1461 	mmc_host_clk_hold(host);
1462 	/*
1463 	 * The card should drive cmd and dat[0:3] low immediately
1464 	 * after the response of cmd11, but wait 1 ms to be sure
1465 	 */
1466 	mmc_delay(1);
1467 	if (host->ops->card_busy && !host->ops->card_busy(host)) {
1468 		err = -EAGAIN;
1469 		goto power_cycle;
1470 	}
1471 	/*
1472 	 * During a signal voltage level switch, the clock must be gated
1473 	 * for 5 ms according to the SD spec
1474 	 */
1475 	clock = host->ios.clock;
1476 	host->ios.clock = 0;
1477 	mmc_set_ios(host);
1478 
1479 	if (__mmc_set_signal_voltage(host, signal_voltage)) {
1480 		/*
1481 		 * Voltages may not have been switched, but we've already
1482 		 * sent CMD11, so a power cycle is required anyway
1483 		 */
1484 		err = -EAGAIN;
1485 		goto power_cycle;
1486 	}
1487 
1488 	/* Keep clock gated for at least 5 ms */
1489 	mmc_delay(5);
1490 	host->ios.clock = clock;
1491 	mmc_set_ios(host);
1492 
1493 	/* Wait for at least 1 ms according to spec */
1494 	mmc_delay(1);
1495 
1496 	/*
1497 	 * Failure to switch is indicated by the card holding
1498 	 * dat[0:3] low
1499 	 */
1500 	if (host->ops->card_busy && host->ops->card_busy(host))
1501 		err = -EAGAIN;
1502 
1503 power_cycle:
1504 	if (err) {
1505 		pr_debug("%s: Signal voltage switch failed, "
1506 			"power cycling card\n", mmc_hostname(host));
1507 		mmc_power_cycle(host);
1508 	}
1509 
1510 	mmc_host_clk_release(host);
1511 
1512 	return err;
1513 }
1514 
1515 /*
1516  * Select timing parameters for host.
1517  */
1518 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
1519 {
1520 	mmc_host_clk_hold(host);
1521 	host->ios.timing = timing;
1522 	mmc_set_ios(host);
1523 	mmc_host_clk_release(host);
1524 }
1525 
1526 /*
1527  * Select appropriate driver type for host.
1528  */
1529 void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
1530 {
1531 	mmc_host_clk_hold(host);
1532 	host->ios.drv_type = drv_type;
1533 	mmc_set_ios(host);
1534 	mmc_host_clk_release(host);
1535 }
1536 
1537 /*
1538  * Apply power to the MMC stack.  This is a two-stage process.
1539  * First, we enable power to the card without the clock running.
1540  * We then wait a bit for the power to stabilise.  Finally,
1541  * enable the bus drivers and clock to the card.
1542  *
1543  * We must _NOT_ enable the clock prior to power stablising.
1544  *
1545  * If a host does all the power sequencing itself, ignore the
1546  * initial MMC_POWER_UP stage.
1547  */
1548 void mmc_power_up(struct mmc_host *host)
1549 {
1550 	int bit;
1551 
1552 	if (host->ios.power_mode == MMC_POWER_ON)
1553 		return;
1554 
1555 	mmc_host_clk_hold(host);
1556 
1557 	/* If ocr is set, we use it */
1558 	if (host->ocr)
1559 		bit = ffs(host->ocr) - 1;
1560 	else
1561 		bit = fls(host->ocr_avail) - 1;
1562 
1563 	host->ios.vdd = bit;
1564 	if (mmc_host_is_spi(host))
1565 		host->ios.chip_select = MMC_CS_HIGH;
1566 	else
1567 		host->ios.chip_select = MMC_CS_DONTCARE;
1568 	host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1569 	host->ios.power_mode = MMC_POWER_UP;
1570 	host->ios.bus_width = MMC_BUS_WIDTH_1;
1571 	host->ios.timing = MMC_TIMING_LEGACY;
1572 	mmc_set_ios(host);
1573 
1574 	/* Set signal voltage to 3.3V */
1575 	__mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330);
1576 
1577 	/*
1578 	 * This delay should be sufficient to allow the power supply
1579 	 * to reach the minimum voltage.
1580 	 */
1581 	mmc_delay(10);
1582 
1583 	host->ios.clock = host->f_init;
1584 
1585 	host->ios.power_mode = MMC_POWER_ON;
1586 	mmc_set_ios(host);
1587 
1588 	/*
1589 	 * This delay must be at least 74 clock sizes, or 1 ms, or the
1590 	 * time required to reach a stable voltage.
1591 	 */
1592 	mmc_delay(10);
1593 
1594 	mmc_host_clk_release(host);
1595 }
1596 
1597 void mmc_power_off(struct mmc_host *host)
1598 {
1599 	if (host->ios.power_mode == MMC_POWER_OFF)
1600 		return;
1601 
1602 	mmc_host_clk_hold(host);
1603 
1604 	host->ios.clock = 0;
1605 	host->ios.vdd = 0;
1606 
1607 
1608 	/*
1609 	 * Reset ocr mask to be the highest possible voltage supported for
1610 	 * this mmc host. This value will be used at next power up.
1611 	 */
1612 	host->ocr = 1 << (fls(host->ocr_avail) - 1);
1613 
1614 	if (!mmc_host_is_spi(host)) {
1615 		host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1616 		host->ios.chip_select = MMC_CS_DONTCARE;
1617 	}
1618 	host->ios.power_mode = MMC_POWER_OFF;
1619 	host->ios.bus_width = MMC_BUS_WIDTH_1;
1620 	host->ios.timing = MMC_TIMING_LEGACY;
1621 	mmc_set_ios(host);
1622 
1623 	/*
1624 	 * Some configurations, such as the 802.11 SDIO card in the OLPC
1625 	 * XO-1.5, require a short delay after poweroff before the card
1626 	 * can be successfully turned on again.
1627 	 */
1628 	mmc_delay(1);
1629 
1630 	mmc_host_clk_release(host);
1631 }
1632 
1633 void mmc_power_cycle(struct mmc_host *host)
1634 {
1635 	mmc_power_off(host);
1636 	/* Wait at least 1 ms according to SD spec */
1637 	mmc_delay(1);
1638 	mmc_power_up(host);
1639 }
1640 
1641 /*
1642  * Cleanup when the last reference to the bus operator is dropped.
1643  */
1644 static void __mmc_release_bus(struct mmc_host *host)
1645 {
1646 	BUG_ON(!host);
1647 	BUG_ON(host->bus_refs);
1648 	BUG_ON(!host->bus_dead);
1649 
1650 	host->bus_ops = NULL;
1651 }
1652 
1653 /*
1654  * Increase reference count of bus operator
1655  */
1656 static inline void mmc_bus_get(struct mmc_host *host)
1657 {
1658 	unsigned long flags;
1659 
1660 	spin_lock_irqsave(&host->lock, flags);
1661 	host->bus_refs++;
1662 	spin_unlock_irqrestore(&host->lock, flags);
1663 }
1664 
1665 /*
1666  * Decrease reference count of bus operator and free it if
1667  * it is the last reference.
1668  */
1669 static inline void mmc_bus_put(struct mmc_host *host)
1670 {
1671 	unsigned long flags;
1672 
1673 	spin_lock_irqsave(&host->lock, flags);
1674 	host->bus_refs--;
1675 	if ((host->bus_refs == 0) && host->bus_ops)
1676 		__mmc_release_bus(host);
1677 	spin_unlock_irqrestore(&host->lock, flags);
1678 }
1679 
1680 /*
1681  * Assign a mmc bus handler to a host. Only one bus handler may control a
1682  * host at any given time.
1683  */
1684 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1685 {
1686 	unsigned long flags;
1687 
1688 	BUG_ON(!host);
1689 	BUG_ON(!ops);
1690 
1691 	WARN_ON(!host->claimed);
1692 
1693 	spin_lock_irqsave(&host->lock, flags);
1694 
1695 	BUG_ON(host->bus_ops);
1696 	BUG_ON(host->bus_refs);
1697 
1698 	host->bus_ops = ops;
1699 	host->bus_refs = 1;
1700 	host->bus_dead = 0;
1701 
1702 	spin_unlock_irqrestore(&host->lock, flags);
1703 }
1704 
1705 /*
1706  * Remove the current bus handler from a host.
1707  */
1708 void mmc_detach_bus(struct mmc_host *host)
1709 {
1710 	unsigned long flags;
1711 
1712 	BUG_ON(!host);
1713 
1714 	WARN_ON(!host->claimed);
1715 	WARN_ON(!host->bus_ops);
1716 
1717 	spin_lock_irqsave(&host->lock, flags);
1718 
1719 	host->bus_dead = 1;
1720 
1721 	spin_unlock_irqrestore(&host->lock, flags);
1722 
1723 	mmc_bus_put(host);
1724 }
1725 
1726 /**
1727  *	mmc_detect_change - process change of state on a MMC socket
1728  *	@host: host which changed state.
1729  *	@delay: optional delay to wait before detection (jiffies)
1730  *
1731  *	MMC drivers should call this when they detect a card has been
1732  *	inserted or removed. The MMC layer will confirm that any
1733  *	present card is still functional, and initialize any newly
1734  *	inserted.
1735  */
1736 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1737 {
1738 #ifdef CONFIG_MMC_DEBUG
1739 	unsigned long flags;
1740 	spin_lock_irqsave(&host->lock, flags);
1741 	WARN_ON(host->removed);
1742 	spin_unlock_irqrestore(&host->lock, flags);
1743 #endif
1744 	host->detect_change = 1;
1745 	mmc_schedule_delayed_work(&host->detect, delay);
1746 }
1747 
1748 EXPORT_SYMBOL(mmc_detect_change);
1749 
1750 void mmc_init_erase(struct mmc_card *card)
1751 {
1752 	unsigned int sz;
1753 
1754 	if (is_power_of_2(card->erase_size))
1755 		card->erase_shift = ffs(card->erase_size) - 1;
1756 	else
1757 		card->erase_shift = 0;
1758 
1759 	/*
1760 	 * It is possible to erase an arbitrarily large area of an SD or MMC
1761 	 * card.  That is not desirable because it can take a long time
1762 	 * (minutes) potentially delaying more important I/O, and also the
1763 	 * timeout calculations become increasingly hugely over-estimated.
1764 	 * Consequently, 'pref_erase' is defined as a guide to limit erases
1765 	 * to that size and alignment.
1766 	 *
1767 	 * For SD cards that define Allocation Unit size, limit erases to one
1768 	 * Allocation Unit at a time.  For MMC cards that define High Capacity
1769 	 * Erase Size, whether it is switched on or not, limit to that size.
1770 	 * Otherwise just have a stab at a good value.  For modern cards it
1771 	 * will end up being 4MiB.  Note that if the value is too small, it
1772 	 * can end up taking longer to erase.
1773 	 */
1774 	if (mmc_card_sd(card) && card->ssr.au) {
1775 		card->pref_erase = card->ssr.au;
1776 		card->erase_shift = ffs(card->ssr.au) - 1;
1777 	} else if (card->ext_csd.hc_erase_size) {
1778 		card->pref_erase = card->ext_csd.hc_erase_size;
1779 	} else {
1780 		sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1781 		if (sz < 128)
1782 			card->pref_erase = 512 * 1024 / 512;
1783 		else if (sz < 512)
1784 			card->pref_erase = 1024 * 1024 / 512;
1785 		else if (sz < 1024)
1786 			card->pref_erase = 2 * 1024 * 1024 / 512;
1787 		else
1788 			card->pref_erase = 4 * 1024 * 1024 / 512;
1789 		if (card->pref_erase < card->erase_size)
1790 			card->pref_erase = card->erase_size;
1791 		else {
1792 			sz = card->pref_erase % card->erase_size;
1793 			if (sz)
1794 				card->pref_erase += card->erase_size - sz;
1795 		}
1796 	}
1797 }
1798 
1799 static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
1800 				          unsigned int arg, unsigned int qty)
1801 {
1802 	unsigned int erase_timeout;
1803 
1804 	if (arg == MMC_DISCARD_ARG ||
1805 	    (arg == MMC_TRIM_ARG && card->ext_csd.rev >= 6)) {
1806 		erase_timeout = card->ext_csd.trim_timeout;
1807 	} else if (card->ext_csd.erase_group_def & 1) {
1808 		/* High Capacity Erase Group Size uses HC timeouts */
1809 		if (arg == MMC_TRIM_ARG)
1810 			erase_timeout = card->ext_csd.trim_timeout;
1811 		else
1812 			erase_timeout = card->ext_csd.hc_erase_timeout;
1813 	} else {
1814 		/* CSD Erase Group Size uses write timeout */
1815 		unsigned int mult = (10 << card->csd.r2w_factor);
1816 		unsigned int timeout_clks = card->csd.tacc_clks * mult;
1817 		unsigned int timeout_us;
1818 
1819 		/* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1820 		if (card->csd.tacc_ns < 1000000)
1821 			timeout_us = (card->csd.tacc_ns * mult) / 1000;
1822 		else
1823 			timeout_us = (card->csd.tacc_ns / 1000) * mult;
1824 
1825 		/*
1826 		 * ios.clock is only a target.  The real clock rate might be
1827 		 * less but not that much less, so fudge it by multiplying by 2.
1828 		 */
1829 		timeout_clks <<= 1;
1830 		timeout_us += (timeout_clks * 1000) /
1831 			      (mmc_host_clk_rate(card->host) / 1000);
1832 
1833 		erase_timeout = timeout_us / 1000;
1834 
1835 		/*
1836 		 * Theoretically, the calculation could underflow so round up
1837 		 * to 1ms in that case.
1838 		 */
1839 		if (!erase_timeout)
1840 			erase_timeout = 1;
1841 	}
1842 
1843 	/* Multiplier for secure operations */
1844 	if (arg & MMC_SECURE_ARGS) {
1845 		if (arg == MMC_SECURE_ERASE_ARG)
1846 			erase_timeout *= card->ext_csd.sec_erase_mult;
1847 		else
1848 			erase_timeout *= card->ext_csd.sec_trim_mult;
1849 	}
1850 
1851 	erase_timeout *= qty;
1852 
1853 	/*
1854 	 * Ensure at least a 1 second timeout for SPI as per
1855 	 * 'mmc_set_data_timeout()'
1856 	 */
1857 	if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1858 		erase_timeout = 1000;
1859 
1860 	return erase_timeout;
1861 }
1862 
1863 static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
1864 					 unsigned int arg,
1865 					 unsigned int qty)
1866 {
1867 	unsigned int erase_timeout;
1868 
1869 	if (card->ssr.erase_timeout) {
1870 		/* Erase timeout specified in SD Status Register (SSR) */
1871 		erase_timeout = card->ssr.erase_timeout * qty +
1872 				card->ssr.erase_offset;
1873 	} else {
1874 		/*
1875 		 * Erase timeout not specified in SD Status Register (SSR) so
1876 		 * use 250ms per write block.
1877 		 */
1878 		erase_timeout = 250 * qty;
1879 	}
1880 
1881 	/* Must not be less than 1 second */
1882 	if (erase_timeout < 1000)
1883 		erase_timeout = 1000;
1884 
1885 	return erase_timeout;
1886 }
1887 
1888 static unsigned int mmc_erase_timeout(struct mmc_card *card,
1889 				      unsigned int arg,
1890 				      unsigned int qty)
1891 {
1892 	if (mmc_card_sd(card))
1893 		return mmc_sd_erase_timeout(card, arg, qty);
1894 	else
1895 		return mmc_mmc_erase_timeout(card, arg, qty);
1896 }
1897 
1898 static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1899 			unsigned int to, unsigned int arg)
1900 {
1901 	struct mmc_command cmd = {0};
1902 	unsigned int qty = 0;
1903 	unsigned long timeout;
1904 	int err;
1905 
1906 	/*
1907 	 * qty is used to calculate the erase timeout which depends on how many
1908 	 * erase groups (or allocation units in SD terminology) are affected.
1909 	 * We count erasing part of an erase group as one erase group.
1910 	 * For SD, the allocation units are always a power of 2.  For MMC, the
1911 	 * erase group size is almost certainly also power of 2, but it does not
1912 	 * seem to insist on that in the JEDEC standard, so we fall back to
1913 	 * division in that case.  SD may not specify an allocation unit size,
1914 	 * in which case the timeout is based on the number of write blocks.
1915 	 *
1916 	 * Note that the timeout for secure trim 2 will only be correct if the
1917 	 * number of erase groups specified is the same as the total of all
1918 	 * preceding secure trim 1 commands.  Since the power may have been
1919 	 * lost since the secure trim 1 commands occurred, it is generally
1920 	 * impossible to calculate the secure trim 2 timeout correctly.
1921 	 */
1922 	if (card->erase_shift)
1923 		qty += ((to >> card->erase_shift) -
1924 			(from >> card->erase_shift)) + 1;
1925 	else if (mmc_card_sd(card))
1926 		qty += to - from + 1;
1927 	else
1928 		qty += ((to / card->erase_size) -
1929 			(from / card->erase_size)) + 1;
1930 
1931 	if (!mmc_card_blockaddr(card)) {
1932 		from <<= 9;
1933 		to <<= 9;
1934 	}
1935 
1936 	if (mmc_card_sd(card))
1937 		cmd.opcode = SD_ERASE_WR_BLK_START;
1938 	else
1939 		cmd.opcode = MMC_ERASE_GROUP_START;
1940 	cmd.arg = from;
1941 	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1942 	err = mmc_wait_for_cmd(card->host, &cmd, 0);
1943 	if (err) {
1944 		pr_err("mmc_erase: group start error %d, "
1945 		       "status %#x\n", err, cmd.resp[0]);
1946 		err = -EIO;
1947 		goto out;
1948 	}
1949 
1950 	memset(&cmd, 0, sizeof(struct mmc_command));
1951 	if (mmc_card_sd(card))
1952 		cmd.opcode = SD_ERASE_WR_BLK_END;
1953 	else
1954 		cmd.opcode = MMC_ERASE_GROUP_END;
1955 	cmd.arg = to;
1956 	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1957 	err = mmc_wait_for_cmd(card->host, &cmd, 0);
1958 	if (err) {
1959 		pr_err("mmc_erase: group end error %d, status %#x\n",
1960 		       err, cmd.resp[0]);
1961 		err = -EIO;
1962 		goto out;
1963 	}
1964 
1965 	memset(&cmd, 0, sizeof(struct mmc_command));
1966 	cmd.opcode = MMC_ERASE;
1967 	cmd.arg = arg;
1968 	cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1969 	cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty);
1970 	err = mmc_wait_for_cmd(card->host, &cmd, 0);
1971 	if (err) {
1972 		pr_err("mmc_erase: erase error %d, status %#x\n",
1973 		       err, cmd.resp[0]);
1974 		err = -EIO;
1975 		goto out;
1976 	}
1977 
1978 	if (mmc_host_is_spi(card->host))
1979 		goto out;
1980 
1981 	timeout = jiffies + msecs_to_jiffies(MMC_CORE_TIMEOUT_MS);
1982 	do {
1983 		memset(&cmd, 0, sizeof(struct mmc_command));
1984 		cmd.opcode = MMC_SEND_STATUS;
1985 		cmd.arg = card->rca << 16;
1986 		cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1987 		/* Do not retry else we can't see errors */
1988 		err = mmc_wait_for_cmd(card->host, &cmd, 0);
1989 		if (err || (cmd.resp[0] & 0xFDF92000)) {
1990 			pr_err("error %d requesting status %#x\n",
1991 				err, cmd.resp[0]);
1992 			err = -EIO;
1993 			goto out;
1994 		}
1995 
1996 		/* Timeout if the device never becomes ready for data and
1997 		 * never leaves the program state.
1998 		 */
1999 		if (time_after(jiffies, timeout)) {
2000 			pr_err("%s: Card stuck in programming state! %s\n",
2001 				mmc_hostname(card->host), __func__);
2002 			err =  -EIO;
2003 			goto out;
2004 		}
2005 
2006 	} while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
2007 		 (R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG));
2008 out:
2009 	return err;
2010 }
2011 
2012 /**
2013  * mmc_erase - erase sectors.
2014  * @card: card to erase
2015  * @from: first sector to erase
2016  * @nr: number of sectors to erase
2017  * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
2018  *
2019  * Caller must claim host before calling this function.
2020  */
2021 int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
2022 	      unsigned int arg)
2023 {
2024 	unsigned int rem, to = from + nr;
2025 
2026 	if (!(card->host->caps & MMC_CAP_ERASE) ||
2027 	    !(card->csd.cmdclass & CCC_ERASE))
2028 		return -EOPNOTSUPP;
2029 
2030 	if (!card->erase_size)
2031 		return -EOPNOTSUPP;
2032 
2033 	if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
2034 		return -EOPNOTSUPP;
2035 
2036 	if ((arg & MMC_SECURE_ARGS) &&
2037 	    !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
2038 		return -EOPNOTSUPP;
2039 
2040 	if ((arg & MMC_TRIM_ARGS) &&
2041 	    !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
2042 		return -EOPNOTSUPP;
2043 
2044 	if (arg == MMC_SECURE_ERASE_ARG) {
2045 		if (from % card->erase_size || nr % card->erase_size)
2046 			return -EINVAL;
2047 	}
2048 
2049 	if (arg == MMC_ERASE_ARG) {
2050 		rem = from % card->erase_size;
2051 		if (rem) {
2052 			rem = card->erase_size - rem;
2053 			from += rem;
2054 			if (nr > rem)
2055 				nr -= rem;
2056 			else
2057 				return 0;
2058 		}
2059 		rem = nr % card->erase_size;
2060 		if (rem)
2061 			nr -= rem;
2062 	}
2063 
2064 	if (nr == 0)
2065 		return 0;
2066 
2067 	to = from + nr;
2068 
2069 	if (to <= from)
2070 		return -EINVAL;
2071 
2072 	/* 'from' and 'to' are inclusive */
2073 	to -= 1;
2074 
2075 	return mmc_do_erase(card, from, to, arg);
2076 }
2077 EXPORT_SYMBOL(mmc_erase);
2078 
2079 int mmc_can_erase(struct mmc_card *card)
2080 {
2081 	if ((card->host->caps & MMC_CAP_ERASE) &&
2082 	    (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
2083 		return 1;
2084 	return 0;
2085 }
2086 EXPORT_SYMBOL(mmc_can_erase);
2087 
2088 int mmc_can_trim(struct mmc_card *card)
2089 {
2090 	if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
2091 		return 1;
2092 	return 0;
2093 }
2094 EXPORT_SYMBOL(mmc_can_trim);
2095 
2096 int mmc_can_discard(struct mmc_card *card)
2097 {
2098 	/*
2099 	 * As there's no way to detect the discard support bit at v4.5
2100 	 * use the s/w feature support filed.
2101 	 */
2102 	if (card->ext_csd.feature_support & MMC_DISCARD_FEATURE)
2103 		return 1;
2104 	return 0;
2105 }
2106 EXPORT_SYMBOL(mmc_can_discard);
2107 
2108 int mmc_can_sanitize(struct mmc_card *card)
2109 {
2110 	if (!mmc_can_trim(card) && !mmc_can_erase(card))
2111 		return 0;
2112 	if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_SANITIZE)
2113 		return 1;
2114 	return 0;
2115 }
2116 EXPORT_SYMBOL(mmc_can_sanitize);
2117 
2118 int mmc_can_secure_erase_trim(struct mmc_card *card)
2119 {
2120 	if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
2121 		return 1;
2122 	return 0;
2123 }
2124 EXPORT_SYMBOL(mmc_can_secure_erase_trim);
2125 
2126 int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
2127 			    unsigned int nr)
2128 {
2129 	if (!card->erase_size)
2130 		return 0;
2131 	if (from % card->erase_size || nr % card->erase_size)
2132 		return 0;
2133 	return 1;
2134 }
2135 EXPORT_SYMBOL(mmc_erase_group_aligned);
2136 
2137 static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
2138 					    unsigned int arg)
2139 {
2140 	struct mmc_host *host = card->host;
2141 	unsigned int max_discard, x, y, qty = 0, max_qty, timeout;
2142 	unsigned int last_timeout = 0;
2143 
2144 	if (card->erase_shift)
2145 		max_qty = UINT_MAX >> card->erase_shift;
2146 	else if (mmc_card_sd(card))
2147 		max_qty = UINT_MAX;
2148 	else
2149 		max_qty = UINT_MAX / card->erase_size;
2150 
2151 	/* Find the largest qty with an OK timeout */
2152 	do {
2153 		y = 0;
2154 		for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
2155 			timeout = mmc_erase_timeout(card, arg, qty + x);
2156 			if (timeout > host->max_discard_to)
2157 				break;
2158 			if (timeout < last_timeout)
2159 				break;
2160 			last_timeout = timeout;
2161 			y = x;
2162 		}
2163 		qty += y;
2164 	} while (y);
2165 
2166 	if (!qty)
2167 		return 0;
2168 
2169 	if (qty == 1)
2170 		return 1;
2171 
2172 	/* Convert qty to sectors */
2173 	if (card->erase_shift)
2174 		max_discard = --qty << card->erase_shift;
2175 	else if (mmc_card_sd(card))
2176 		max_discard = qty;
2177 	else
2178 		max_discard = --qty * card->erase_size;
2179 
2180 	return max_discard;
2181 }
2182 
2183 unsigned int mmc_calc_max_discard(struct mmc_card *card)
2184 {
2185 	struct mmc_host *host = card->host;
2186 	unsigned int max_discard, max_trim;
2187 
2188 	if (!host->max_discard_to)
2189 		return UINT_MAX;
2190 
2191 	/*
2192 	 * Without erase_group_def set, MMC erase timeout depends on clock
2193 	 * frequence which can change.  In that case, the best choice is
2194 	 * just the preferred erase size.
2195 	 */
2196 	if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1))
2197 		return card->pref_erase;
2198 
2199 	max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG);
2200 	if (mmc_can_trim(card)) {
2201 		max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG);
2202 		if (max_trim < max_discard)
2203 			max_discard = max_trim;
2204 	} else if (max_discard < card->erase_size) {
2205 		max_discard = 0;
2206 	}
2207 	pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
2208 		 mmc_hostname(host), max_discard, host->max_discard_to);
2209 	return max_discard;
2210 }
2211 EXPORT_SYMBOL(mmc_calc_max_discard);
2212 
2213 int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
2214 {
2215 	struct mmc_command cmd = {0};
2216 
2217 	if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
2218 		return 0;
2219 
2220 	cmd.opcode = MMC_SET_BLOCKLEN;
2221 	cmd.arg = blocklen;
2222 	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
2223 	return mmc_wait_for_cmd(card->host, &cmd, 5);
2224 }
2225 EXPORT_SYMBOL(mmc_set_blocklen);
2226 
2227 int mmc_set_blockcount(struct mmc_card *card, unsigned int blockcount,
2228 			bool is_rel_write)
2229 {
2230 	struct mmc_command cmd = {0};
2231 
2232 	cmd.opcode = MMC_SET_BLOCK_COUNT;
2233 	cmd.arg = blockcount & 0x0000FFFF;
2234 	if (is_rel_write)
2235 		cmd.arg |= 1 << 31;
2236 	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
2237 	return mmc_wait_for_cmd(card->host, &cmd, 5);
2238 }
2239 EXPORT_SYMBOL(mmc_set_blockcount);
2240 
2241 static void mmc_hw_reset_for_init(struct mmc_host *host)
2242 {
2243 	if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
2244 		return;
2245 	mmc_host_clk_hold(host);
2246 	host->ops->hw_reset(host);
2247 	mmc_host_clk_release(host);
2248 }
2249 
2250 int mmc_can_reset(struct mmc_card *card)
2251 {
2252 	u8 rst_n_function;
2253 
2254 	if (!mmc_card_mmc(card))
2255 		return 0;
2256 	rst_n_function = card->ext_csd.rst_n_function;
2257 	if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
2258 		return 0;
2259 	return 1;
2260 }
2261 EXPORT_SYMBOL(mmc_can_reset);
2262 
2263 static int mmc_do_hw_reset(struct mmc_host *host, int check)
2264 {
2265 	struct mmc_card *card = host->card;
2266 
2267 	if (!host->bus_ops->power_restore)
2268 		return -EOPNOTSUPP;
2269 
2270 	if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
2271 		return -EOPNOTSUPP;
2272 
2273 	if (!card)
2274 		return -EINVAL;
2275 
2276 	if (!mmc_can_reset(card))
2277 		return -EOPNOTSUPP;
2278 
2279 	mmc_host_clk_hold(host);
2280 	mmc_set_clock(host, host->f_init);
2281 
2282 	host->ops->hw_reset(host);
2283 
2284 	/* If the reset has happened, then a status command will fail */
2285 	if (check) {
2286 		struct mmc_command cmd = {0};
2287 		int err;
2288 
2289 		cmd.opcode = MMC_SEND_STATUS;
2290 		if (!mmc_host_is_spi(card->host))
2291 			cmd.arg = card->rca << 16;
2292 		cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
2293 		err = mmc_wait_for_cmd(card->host, &cmd, 0);
2294 		if (!err) {
2295 			mmc_host_clk_release(host);
2296 			return -ENOSYS;
2297 		}
2298 	}
2299 
2300 	host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_DDR);
2301 	if (mmc_host_is_spi(host)) {
2302 		host->ios.chip_select = MMC_CS_HIGH;
2303 		host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
2304 	} else {
2305 		host->ios.chip_select = MMC_CS_DONTCARE;
2306 		host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
2307 	}
2308 	host->ios.bus_width = MMC_BUS_WIDTH_1;
2309 	host->ios.timing = MMC_TIMING_LEGACY;
2310 	mmc_set_ios(host);
2311 
2312 	mmc_host_clk_release(host);
2313 
2314 	return host->bus_ops->power_restore(host);
2315 }
2316 
2317 int mmc_hw_reset(struct mmc_host *host)
2318 {
2319 	return mmc_do_hw_reset(host, 0);
2320 }
2321 EXPORT_SYMBOL(mmc_hw_reset);
2322 
2323 int mmc_hw_reset_check(struct mmc_host *host)
2324 {
2325 	return mmc_do_hw_reset(host, 1);
2326 }
2327 EXPORT_SYMBOL(mmc_hw_reset_check);
2328 
2329 static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
2330 {
2331 	host->f_init = freq;
2332 
2333 #ifdef CONFIG_MMC_DEBUG
2334 	pr_info("%s: %s: trying to init card at %u Hz\n",
2335 		mmc_hostname(host), __func__, host->f_init);
2336 #endif
2337 	mmc_power_up(host);
2338 
2339 	/*
2340 	 * Some eMMCs (with VCCQ always on) may not be reset after power up, so
2341 	 * do a hardware reset if possible.
2342 	 */
2343 	mmc_hw_reset_for_init(host);
2344 
2345 	/*
2346 	 * sdio_reset sends CMD52 to reset card.  Since we do not know
2347 	 * if the card is being re-initialized, just send it.  CMD52
2348 	 * should be ignored by SD/eMMC cards.
2349 	 */
2350 	sdio_reset(host);
2351 	mmc_go_idle(host);
2352 
2353 	mmc_send_if_cond(host, host->ocr_avail);
2354 
2355 	/* Order's important: probe SDIO, then SD, then MMC */
2356 	if (!mmc_attach_sdio(host))
2357 		return 0;
2358 	if (!mmc_attach_sd(host))
2359 		return 0;
2360 	if (!mmc_attach_mmc(host))
2361 		return 0;
2362 
2363 	mmc_power_off(host);
2364 	return -EIO;
2365 }
2366 
2367 int _mmc_detect_card_removed(struct mmc_host *host)
2368 {
2369 	int ret;
2370 
2371 	if ((host->caps & MMC_CAP_NONREMOVABLE) || !host->bus_ops->alive)
2372 		return 0;
2373 
2374 	if (!host->card || mmc_card_removed(host->card))
2375 		return 1;
2376 
2377 	ret = host->bus_ops->alive(host);
2378 
2379 	/*
2380 	 * Card detect status and alive check may be out of sync if card is
2381 	 * removed slowly, when card detect switch changes while card/slot
2382 	 * pads are still contacted in hardware (refer to "SD Card Mechanical
2383 	 * Addendum, Appendix C: Card Detection Switch"). So reschedule a
2384 	 * detect work 200ms later for this case.
2385 	 */
2386 	if (!ret && host->ops->get_cd && !host->ops->get_cd(host)) {
2387 		mmc_detect_change(host, msecs_to_jiffies(200));
2388 		pr_debug("%s: card removed too slowly\n", mmc_hostname(host));
2389 	}
2390 
2391 	if (ret) {
2392 		mmc_card_set_removed(host->card);
2393 		pr_debug("%s: card remove detected\n", mmc_hostname(host));
2394 	}
2395 
2396 	return ret;
2397 }
2398 
2399 int mmc_detect_card_removed(struct mmc_host *host)
2400 {
2401 	struct mmc_card *card = host->card;
2402 	int ret;
2403 
2404 	WARN_ON(!host->claimed);
2405 
2406 	if (!card)
2407 		return 1;
2408 
2409 	ret = mmc_card_removed(card);
2410 	/*
2411 	 * The card will be considered unchanged unless we have been asked to
2412 	 * detect a change or host requires polling to provide card detection.
2413 	 */
2414 	if (!host->detect_change && !(host->caps & MMC_CAP_NEEDS_POLL))
2415 		return ret;
2416 
2417 	host->detect_change = 0;
2418 	if (!ret) {
2419 		ret = _mmc_detect_card_removed(host);
2420 		if (ret && (host->caps & MMC_CAP_NEEDS_POLL)) {
2421 			/*
2422 			 * Schedule a detect work as soon as possible to let a
2423 			 * rescan handle the card removal.
2424 			 */
2425 			cancel_delayed_work(&host->detect);
2426 			mmc_detect_change(host, 0);
2427 		}
2428 	}
2429 
2430 	return ret;
2431 }
2432 EXPORT_SYMBOL(mmc_detect_card_removed);
2433 
2434 void mmc_rescan(struct work_struct *work)
2435 {
2436 	struct mmc_host *host =
2437 		container_of(work, struct mmc_host, detect.work);
2438 	int i;
2439 
2440 	if (host->rescan_disable)
2441 		return;
2442 
2443 	/* If there is a non-removable card registered, only scan once */
2444 	if ((host->caps & MMC_CAP_NONREMOVABLE) && host->rescan_entered)
2445 		return;
2446 	host->rescan_entered = 1;
2447 
2448 	mmc_bus_get(host);
2449 
2450 	/*
2451 	 * if there is a _removable_ card registered, check whether it is
2452 	 * still present
2453 	 */
2454 	if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
2455 	    && !(host->caps & MMC_CAP_NONREMOVABLE))
2456 		host->bus_ops->detect(host);
2457 
2458 	host->detect_change = 0;
2459 
2460 	/*
2461 	 * Let mmc_bus_put() free the bus/bus_ops if we've found that
2462 	 * the card is no longer present.
2463 	 */
2464 	mmc_bus_put(host);
2465 	mmc_bus_get(host);
2466 
2467 	/* if there still is a card present, stop here */
2468 	if (host->bus_ops != NULL) {
2469 		mmc_bus_put(host);
2470 		goto out;
2471 	}
2472 
2473 	/*
2474 	 * Only we can add a new handler, so it's safe to
2475 	 * release the lock here.
2476 	 */
2477 	mmc_bus_put(host);
2478 
2479 	if (host->ops->get_cd && host->ops->get_cd(host) == 0) {
2480 		mmc_claim_host(host);
2481 		mmc_power_off(host);
2482 		mmc_release_host(host);
2483 		goto out;
2484 	}
2485 
2486 	mmc_claim_host(host);
2487 	for (i = 0; i < ARRAY_SIZE(freqs); i++) {
2488 		if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
2489 			break;
2490 		if (freqs[i] <= host->f_min)
2491 			break;
2492 	}
2493 	mmc_release_host(host);
2494 
2495  out:
2496 	if (host->caps & MMC_CAP_NEEDS_POLL)
2497 		mmc_schedule_delayed_work(&host->detect, HZ);
2498 }
2499 
2500 void mmc_start_host(struct mmc_host *host)
2501 {
2502 	host->f_init = max(freqs[0], host->f_min);
2503 	host->rescan_disable = 0;
2504 	if (host->caps2 & MMC_CAP2_NO_PRESCAN_POWERUP)
2505 		mmc_power_off(host);
2506 	else
2507 		mmc_power_up(host);
2508 	mmc_detect_change(host, 0);
2509 }
2510 
2511 void mmc_stop_host(struct mmc_host *host)
2512 {
2513 #ifdef CONFIG_MMC_DEBUG
2514 	unsigned long flags;
2515 	spin_lock_irqsave(&host->lock, flags);
2516 	host->removed = 1;
2517 	spin_unlock_irqrestore(&host->lock, flags);
2518 #endif
2519 
2520 	host->rescan_disable = 1;
2521 	cancel_delayed_work_sync(&host->detect);
2522 	mmc_flush_scheduled_work();
2523 
2524 	/* clear pm flags now and let card drivers set them as needed */
2525 	host->pm_flags = 0;
2526 
2527 	mmc_bus_get(host);
2528 	if (host->bus_ops && !host->bus_dead) {
2529 		/* Calling bus_ops->remove() with a claimed host can deadlock */
2530 		host->bus_ops->remove(host);
2531 		mmc_claim_host(host);
2532 		mmc_detach_bus(host);
2533 		mmc_power_off(host);
2534 		mmc_release_host(host);
2535 		mmc_bus_put(host);
2536 		return;
2537 	}
2538 	mmc_bus_put(host);
2539 
2540 	BUG_ON(host->card);
2541 
2542 	mmc_power_off(host);
2543 }
2544 
2545 int mmc_power_save_host(struct mmc_host *host)
2546 {
2547 	int ret = 0;
2548 
2549 #ifdef CONFIG_MMC_DEBUG
2550 	pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__);
2551 #endif
2552 
2553 	mmc_bus_get(host);
2554 
2555 	if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
2556 		mmc_bus_put(host);
2557 		return -EINVAL;
2558 	}
2559 
2560 	if (host->bus_ops->power_save)
2561 		ret = host->bus_ops->power_save(host);
2562 
2563 	mmc_bus_put(host);
2564 
2565 	mmc_power_off(host);
2566 
2567 	return ret;
2568 }
2569 EXPORT_SYMBOL(mmc_power_save_host);
2570 
2571 int mmc_power_restore_host(struct mmc_host *host)
2572 {
2573 	int ret;
2574 
2575 #ifdef CONFIG_MMC_DEBUG
2576 	pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__);
2577 #endif
2578 
2579 	mmc_bus_get(host);
2580 
2581 	if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
2582 		mmc_bus_put(host);
2583 		return -EINVAL;
2584 	}
2585 
2586 	mmc_power_up(host);
2587 	ret = host->bus_ops->power_restore(host);
2588 
2589 	mmc_bus_put(host);
2590 
2591 	return ret;
2592 }
2593 EXPORT_SYMBOL(mmc_power_restore_host);
2594 
2595 /*
2596  * Flush the cache to the non-volatile storage.
2597  */
2598 int mmc_flush_cache(struct mmc_card *card)
2599 {
2600 	struct mmc_host *host = card->host;
2601 	int err = 0;
2602 
2603 	if (!(host->caps2 & MMC_CAP2_CACHE_CTRL))
2604 		return err;
2605 
2606 	if (mmc_card_mmc(card) &&
2607 			(card->ext_csd.cache_size > 0) &&
2608 			(card->ext_csd.cache_ctrl & 1)) {
2609 		err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2610 				EXT_CSD_FLUSH_CACHE, 1, 0);
2611 		if (err)
2612 			pr_err("%s: cache flush error %d\n",
2613 					mmc_hostname(card->host), err);
2614 	}
2615 
2616 	return err;
2617 }
2618 EXPORT_SYMBOL(mmc_flush_cache);
2619 
2620 /*
2621  * Turn the cache ON/OFF.
2622  * Turning the cache OFF shall trigger flushing of the data
2623  * to the non-volatile storage.
2624  * This function should be called with host claimed
2625  */
2626 int mmc_cache_ctrl(struct mmc_host *host, u8 enable)
2627 {
2628 	struct mmc_card *card = host->card;
2629 	unsigned int timeout;
2630 	int err = 0;
2631 
2632 	if (!(host->caps2 & MMC_CAP2_CACHE_CTRL) ||
2633 			mmc_card_is_removable(host))
2634 		return err;
2635 
2636 	if (card && mmc_card_mmc(card) &&
2637 			(card->ext_csd.cache_size > 0)) {
2638 		enable = !!enable;
2639 
2640 		if (card->ext_csd.cache_ctrl ^ enable) {
2641 			timeout = enable ? card->ext_csd.generic_cmd6_time : 0;
2642 			err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2643 					EXT_CSD_CACHE_CTRL, enable, timeout);
2644 			if (err)
2645 				pr_err("%s: cache %s error %d\n",
2646 						mmc_hostname(card->host),
2647 						enable ? "on" : "off",
2648 						err);
2649 			else
2650 				card->ext_csd.cache_ctrl = enable;
2651 		}
2652 	}
2653 
2654 	return err;
2655 }
2656 EXPORT_SYMBOL(mmc_cache_ctrl);
2657 
2658 #ifdef CONFIG_PM
2659 
2660 /**
2661  *	mmc_suspend_host - suspend a host
2662  *	@host: mmc host
2663  */
2664 int mmc_suspend_host(struct mmc_host *host)
2665 {
2666 	/* This function is deprecated */
2667 	return 0;
2668 }
2669 EXPORT_SYMBOL(mmc_suspend_host);
2670 
2671 /**
2672  *	mmc_resume_host - resume a previously suspended host
2673  *	@host: mmc host
2674  */
2675 int mmc_resume_host(struct mmc_host *host)
2676 {
2677 	/* This function is deprecated */
2678 	return 0;
2679 }
2680 EXPORT_SYMBOL(mmc_resume_host);
2681 
2682 /* Do the card removal on suspend if card is assumed removeable
2683  * Do that in pm notifier while userspace isn't yet frozen, so we will be able
2684    to sync the card.
2685 */
2686 int mmc_pm_notify(struct notifier_block *notify_block,
2687 					unsigned long mode, void *unused)
2688 {
2689 	struct mmc_host *host = container_of(
2690 		notify_block, struct mmc_host, pm_notify);
2691 	unsigned long flags;
2692 	int err = 0;
2693 
2694 	switch (mode) {
2695 	case PM_HIBERNATION_PREPARE:
2696 	case PM_SUSPEND_PREPARE:
2697 		spin_lock_irqsave(&host->lock, flags);
2698 		host->rescan_disable = 1;
2699 		spin_unlock_irqrestore(&host->lock, flags);
2700 		cancel_delayed_work_sync(&host->detect);
2701 
2702 		if (!host->bus_ops)
2703 			break;
2704 
2705 		/* Validate prerequisites for suspend */
2706 		if (host->bus_ops->pre_suspend)
2707 			err = host->bus_ops->pre_suspend(host);
2708 		if (!err && host->bus_ops->suspend)
2709 			break;
2710 
2711 		/* Calling bus_ops->remove() with a claimed host can deadlock */
2712 		host->bus_ops->remove(host);
2713 		mmc_claim_host(host);
2714 		mmc_detach_bus(host);
2715 		mmc_power_off(host);
2716 		mmc_release_host(host);
2717 		host->pm_flags = 0;
2718 		break;
2719 
2720 	case PM_POST_SUSPEND:
2721 	case PM_POST_HIBERNATION:
2722 	case PM_POST_RESTORE:
2723 
2724 		spin_lock_irqsave(&host->lock, flags);
2725 		host->rescan_disable = 0;
2726 		spin_unlock_irqrestore(&host->lock, flags);
2727 		mmc_detect_change(host, 0);
2728 
2729 	}
2730 
2731 	return 0;
2732 }
2733 #endif
2734 
2735 /**
2736  * mmc_init_context_info() - init synchronization context
2737  * @host: mmc host
2738  *
2739  * Init struct context_info needed to implement asynchronous
2740  * request mechanism, used by mmc core, host driver and mmc requests
2741  * supplier.
2742  */
2743 void mmc_init_context_info(struct mmc_host *host)
2744 {
2745 	spin_lock_init(&host->context_info.lock);
2746 	host->context_info.is_new_req = false;
2747 	host->context_info.is_done_rcv = false;
2748 	host->context_info.is_waiting_last_req = false;
2749 	init_waitqueue_head(&host->context_info.wait);
2750 }
2751 
2752 static int __init mmc_init(void)
2753 {
2754 	int ret;
2755 
2756 	workqueue = alloc_ordered_workqueue("kmmcd", 0);
2757 	if (!workqueue)
2758 		return -ENOMEM;
2759 
2760 	ret = mmc_register_bus();
2761 	if (ret)
2762 		goto destroy_workqueue;
2763 
2764 	ret = mmc_register_host_class();
2765 	if (ret)
2766 		goto unregister_bus;
2767 
2768 	ret = sdio_register_bus();
2769 	if (ret)
2770 		goto unregister_host_class;
2771 
2772 	return 0;
2773 
2774 unregister_host_class:
2775 	mmc_unregister_host_class();
2776 unregister_bus:
2777 	mmc_unregister_bus();
2778 destroy_workqueue:
2779 	destroy_workqueue(workqueue);
2780 
2781 	return ret;
2782 }
2783 
2784 static void __exit mmc_exit(void)
2785 {
2786 	sdio_unregister_bus();
2787 	mmc_unregister_host_class();
2788 	mmc_unregister_bus();
2789 	destroy_workqueue(workqueue);
2790 }
2791 
2792 subsys_initcall(mmc_init);
2793 module_exit(mmc_exit);
2794 
2795 MODULE_LICENSE("GPL");
2796