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