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