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