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