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