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> |
1da177e4 LT |
25 | |
26 | #include <linux/mmc/card.h> | |
27 | #include <linux/mmc/host.h> | |
da7fbe58 PO |
28 | #include <linux/mmc/mmc.h> |
29 | #include <linux/mmc/sd.h> | |
1da177e4 | 30 | |
aaac1b47 | 31 | #include "core.h" |
ffce2e7e PO |
32 | #include "bus.h" |
33 | #include "host.h" | |
e29a7d73 | 34 | #include "sdio_bus.h" |
da7fbe58 PO |
35 | |
36 | #include "mmc_ops.h" | |
37 | #include "sd_ops.h" | |
5c4e6f13 | 38 | #include "sdio_ops.h" |
1da177e4 | 39 | |
ffce2e7e PO |
40 | static struct workqueue_struct *workqueue; |
41 | ||
af517150 DB |
42 | /* |
43 | * Enabling software CRCs on the data blocks can be a significant (30%) | |
44 | * performance cost, and for other reasons may not always be desired. | |
45 | * So we allow it it to be disabled. | |
46 | */ | |
47 | int use_spi_crc = 1; | |
48 | module_param(use_spi_crc, bool, 0); | |
49 | ||
bd68e083 BH |
50 | /* |
51 | * We normally treat cards as removed during suspend if they are not | |
52 | * known to be on a non-removable bus, to avoid the risk of writing | |
53 | * back data to a different card after resume. Allow this to be | |
54 | * overridden if necessary. | |
55 | */ | |
56 | #ifdef CONFIG_MMC_UNSAFE_RESUME | |
57 | int mmc_assume_removable; | |
58 | #else | |
59 | int mmc_assume_removable = 1; | |
60 | #endif | |
71d7d3d1 | 61 | EXPORT_SYMBOL(mmc_assume_removable); |
bd68e083 BH |
62 | module_param_named(removable, mmc_assume_removable, bool, 0644); |
63 | MODULE_PARM_DESC( | |
64 | removable, | |
65 | "MMC/SD cards are removable and may be removed during suspend"); | |
66 | ||
ffce2e7e PO |
67 | /* |
68 | * Internal function. Schedule delayed work in the MMC work queue. | |
69 | */ | |
70 | static int mmc_schedule_delayed_work(struct delayed_work *work, | |
71 | unsigned long delay) | |
72 | { | |
73 | return queue_delayed_work(workqueue, work, delay); | |
74 | } | |
75 | ||
76 | /* | |
77 | * Internal function. Flush all scheduled work from the MMC work queue. | |
78 | */ | |
79 | static void mmc_flush_scheduled_work(void) | |
80 | { | |
81 | flush_workqueue(workqueue); | |
82 | } | |
83 | ||
1da177e4 | 84 | /** |
fe10c6ab RK |
85 | * mmc_request_done - finish processing an MMC request |
86 | * @host: MMC host which completed request | |
87 | * @mrq: MMC request which request | |
1da177e4 LT |
88 | * |
89 | * MMC drivers should call this function when they have completed | |
fe10c6ab | 90 | * their processing of a request. |
1da177e4 LT |
91 | */ |
92 | void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq) | |
93 | { | |
94 | struct mmc_command *cmd = mrq->cmd; | |
920e70c5 RK |
95 | int err = cmd->error; |
96 | ||
af517150 DB |
97 | if (err && cmd->retries && mmc_host_is_spi(host)) { |
98 | if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND) | |
99 | cmd->retries = 0; | |
100 | } | |
101 | ||
1da177e4 | 102 | if (err && cmd->retries) { |
e4d21708 PO |
103 | pr_debug("%s: req failed (CMD%u): %d, retrying...\n", |
104 | mmc_hostname(host), cmd->opcode, err); | |
105 | ||
1da177e4 LT |
106 | cmd->retries--; |
107 | cmd->error = 0; | |
108 | host->ops->request(host, mrq); | |
e4d21708 | 109 | } else { |
af8350c7 PO |
110 | led_trigger_event(host->led, LED_OFF); |
111 | ||
e4d21708 PO |
112 | pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n", |
113 | mmc_hostname(host), cmd->opcode, err, | |
114 | cmd->resp[0], cmd->resp[1], | |
115 | cmd->resp[2], cmd->resp[3]); | |
116 | ||
117 | if (mrq->data) { | |
118 | pr_debug("%s: %d bytes transferred: %d\n", | |
119 | mmc_hostname(host), | |
120 | mrq->data->bytes_xfered, mrq->data->error); | |
121 | } | |
122 | ||
123 | if (mrq->stop) { | |
124 | pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n", | |
125 | mmc_hostname(host), mrq->stop->opcode, | |
126 | mrq->stop->error, | |
127 | mrq->stop->resp[0], mrq->stop->resp[1], | |
128 | mrq->stop->resp[2], mrq->stop->resp[3]); | |
129 | } | |
130 | ||
131 | if (mrq->done) | |
132 | mrq->done(mrq); | |
1da177e4 LT |
133 | } |
134 | } | |
135 | ||
136 | EXPORT_SYMBOL(mmc_request_done); | |
137 | ||
39361851 | 138 | static void |
1da177e4 LT |
139 | mmc_start_request(struct mmc_host *host, struct mmc_request *mrq) |
140 | { | |
976d9276 PO |
141 | #ifdef CONFIG_MMC_DEBUG |
142 | unsigned int i, sz; | |
a84756c5 | 143 | struct scatterlist *sg; |
976d9276 PO |
144 | #endif |
145 | ||
920e70c5 RK |
146 | pr_debug("%s: starting CMD%u arg %08x flags %08x\n", |
147 | mmc_hostname(host), mrq->cmd->opcode, | |
148 | mrq->cmd->arg, mrq->cmd->flags); | |
1da177e4 | 149 | |
e4d21708 PO |
150 | if (mrq->data) { |
151 | pr_debug("%s: blksz %d blocks %d flags %08x " | |
152 | "tsac %d ms nsac %d\n", | |
153 | mmc_hostname(host), mrq->data->blksz, | |
154 | mrq->data->blocks, mrq->data->flags, | |
ce252edd | 155 | mrq->data->timeout_ns / 1000000, |
e4d21708 PO |
156 | mrq->data->timeout_clks); |
157 | } | |
158 | ||
159 | if (mrq->stop) { | |
160 | pr_debug("%s: CMD%u arg %08x flags %08x\n", | |
161 | mmc_hostname(host), mrq->stop->opcode, | |
162 | mrq->stop->arg, mrq->stop->flags); | |
163 | } | |
164 | ||
f22ee4ed | 165 | WARN_ON(!host->claimed); |
1da177e4 | 166 | |
af8350c7 PO |
167 | led_trigger_event(host->led, LED_FULL); |
168 | ||
1da177e4 LT |
169 | mrq->cmd->error = 0; |
170 | mrq->cmd->mrq = mrq; | |
171 | if (mrq->data) { | |
fe4a3c7a | 172 | BUG_ON(mrq->data->blksz > host->max_blk_size); |
55db890a PO |
173 | BUG_ON(mrq->data->blocks > host->max_blk_count); |
174 | BUG_ON(mrq->data->blocks * mrq->data->blksz > | |
175 | host->max_req_size); | |
fe4a3c7a | 176 | |
976d9276 PO |
177 | #ifdef CONFIG_MMC_DEBUG |
178 | sz = 0; | |
a84756c5 PO |
179 | for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i) |
180 | sz += sg->length; | |
976d9276 PO |
181 | BUG_ON(sz != mrq->data->blocks * mrq->data->blksz); |
182 | #endif | |
183 | ||
1da177e4 LT |
184 | mrq->cmd->data = mrq->data; |
185 | mrq->data->error = 0; | |
186 | mrq->data->mrq = mrq; | |
187 | if (mrq->stop) { | |
188 | mrq->data->stop = mrq->stop; | |
189 | mrq->stop->error = 0; | |
190 | mrq->stop->mrq = mrq; | |
191 | } | |
192 | } | |
193 | host->ops->request(host, mrq); | |
194 | } | |
195 | ||
1da177e4 LT |
196 | static void mmc_wait_done(struct mmc_request *mrq) |
197 | { | |
198 | complete(mrq->done_data); | |
199 | } | |
200 | ||
67a61c48 PO |
201 | /** |
202 | * mmc_wait_for_req - start a request and wait for completion | |
203 | * @host: MMC host to start command | |
204 | * @mrq: MMC request to start | |
205 | * | |
206 | * Start a new MMC custom command request for a host, and wait | |
207 | * for the command to complete. Does not attempt to parse the | |
208 | * response. | |
209 | */ | |
210 | void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq) | |
1da177e4 | 211 | { |
0afffc72 | 212 | DECLARE_COMPLETION_ONSTACK(complete); |
1da177e4 LT |
213 | |
214 | mrq->done_data = &complete; | |
215 | mrq->done = mmc_wait_done; | |
216 | ||
217 | mmc_start_request(host, mrq); | |
218 | ||
219 | wait_for_completion(&complete); | |
1da177e4 LT |
220 | } |
221 | ||
222 | EXPORT_SYMBOL(mmc_wait_for_req); | |
223 | ||
224 | /** | |
225 | * mmc_wait_for_cmd - start a command and wait for completion | |
226 | * @host: MMC host to start command | |
227 | * @cmd: MMC command to start | |
228 | * @retries: maximum number of retries | |
229 | * | |
230 | * Start a new MMC command for a host, and wait for the command | |
231 | * to complete. Return any error that occurred while the command | |
232 | * was executing. Do not attempt to parse the response. | |
233 | */ | |
234 | int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries) | |
235 | { | |
236 | struct mmc_request mrq; | |
237 | ||
d84075c8 | 238 | WARN_ON(!host->claimed); |
1da177e4 LT |
239 | |
240 | memset(&mrq, 0, sizeof(struct mmc_request)); | |
241 | ||
242 | memset(cmd->resp, 0, sizeof(cmd->resp)); | |
243 | cmd->retries = retries; | |
244 | ||
245 | mrq.cmd = cmd; | |
246 | cmd->data = NULL; | |
247 | ||
248 | mmc_wait_for_req(host, &mrq); | |
249 | ||
250 | return cmd->error; | |
251 | } | |
252 | ||
253 | EXPORT_SYMBOL(mmc_wait_for_cmd); | |
254 | ||
d773d725 RK |
255 | /** |
256 | * mmc_set_data_timeout - set the timeout for a data command | |
257 | * @data: data phase for command | |
258 | * @card: the MMC card associated with the data transfer | |
67a61c48 PO |
259 | * |
260 | * Computes the data timeout parameters according to the | |
261 | * correct algorithm given the card type. | |
d773d725 | 262 | */ |
b146d26a | 263 | void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card) |
d773d725 RK |
264 | { |
265 | unsigned int mult; | |
266 | ||
e6f918bf PO |
267 | /* |
268 | * SDIO cards only define an upper 1 s limit on access. | |
269 | */ | |
270 | if (mmc_card_sdio(card)) { | |
271 | data->timeout_ns = 1000000000; | |
272 | data->timeout_clks = 0; | |
273 | return; | |
274 | } | |
275 | ||
d773d725 RK |
276 | /* |
277 | * SD cards use a 100 multiplier rather than 10 | |
278 | */ | |
279 | mult = mmc_card_sd(card) ? 100 : 10; | |
280 | ||
281 | /* | |
282 | * Scale up the multiplier (and therefore the timeout) by | |
283 | * the r2w factor for writes. | |
284 | */ | |
b146d26a | 285 | if (data->flags & MMC_DATA_WRITE) |
d773d725 RK |
286 | mult <<= card->csd.r2w_factor; |
287 | ||
288 | data->timeout_ns = card->csd.tacc_ns * mult; | |
289 | data->timeout_clks = card->csd.tacc_clks * mult; | |
290 | ||
291 | /* | |
292 | * SD cards also have an upper limit on the timeout. | |
293 | */ | |
294 | if (mmc_card_sd(card)) { | |
295 | unsigned int timeout_us, limit_us; | |
296 | ||
297 | timeout_us = data->timeout_ns / 1000; | |
298 | timeout_us += data->timeout_clks * 1000 / | |
299 | (card->host->ios.clock / 1000); | |
300 | ||
b146d26a | 301 | if (data->flags & MMC_DATA_WRITE) |
493890e7 PO |
302 | /* |
303 | * The limit is really 250 ms, but that is | |
304 | * insufficient for some crappy cards. | |
305 | */ | |
306 | limit_us = 300000; | |
d773d725 RK |
307 | else |
308 | limit_us = 100000; | |
309 | ||
fba68bd2 PL |
310 | /* |
311 | * SDHC cards always use these fixed values. | |
312 | */ | |
313 | if (timeout_us > limit_us || mmc_card_blockaddr(card)) { | |
d773d725 RK |
314 | data->timeout_ns = limit_us * 1000; |
315 | data->timeout_clks = 0; | |
316 | } | |
317 | } | |
c0c88871 WM |
318 | /* |
319 | * Some cards need very high timeouts if driven in SPI mode. | |
320 | * The worst observed timeout was 900ms after writing a | |
321 | * continuous stream of data until the internal logic | |
322 | * overflowed. | |
323 | */ | |
324 | if (mmc_host_is_spi(card->host)) { | |
325 | if (data->flags & MMC_DATA_WRITE) { | |
326 | if (data->timeout_ns < 1000000000) | |
327 | data->timeout_ns = 1000000000; /* 1s */ | |
328 | } else { | |
329 | if (data->timeout_ns < 100000000) | |
330 | data->timeout_ns = 100000000; /* 100ms */ | |
331 | } | |
332 | } | |
d773d725 RK |
333 | } |
334 | EXPORT_SYMBOL(mmc_set_data_timeout); | |
335 | ||
ad3868b2 PO |
336 | /** |
337 | * mmc_align_data_size - pads a transfer size to a more optimal value | |
338 | * @card: the MMC card associated with the data transfer | |
339 | * @sz: original transfer size | |
340 | * | |
341 | * Pads the original data size with a number of extra bytes in | |
342 | * order to avoid controller bugs and/or performance hits | |
343 | * (e.g. some controllers revert to PIO for certain sizes). | |
344 | * | |
345 | * Returns the improved size, which might be unmodified. | |
346 | * | |
347 | * Note that this function is only relevant when issuing a | |
348 | * single scatter gather entry. | |
349 | */ | |
350 | unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz) | |
351 | { | |
352 | /* | |
353 | * FIXME: We don't have a system for the controller to tell | |
354 | * the core about its problems yet, so for now we just 32-bit | |
355 | * align the size. | |
356 | */ | |
357 | sz = ((sz + 3) / 4) * 4; | |
358 | ||
359 | return sz; | |
360 | } | |
361 | EXPORT_SYMBOL(mmc_align_data_size); | |
362 | ||
8ea926b2 AH |
363 | /** |
364 | * mmc_host_enable - enable a host. | |
365 | * @host: mmc host to enable | |
366 | * | |
367 | * Hosts that support power saving can use the 'enable' and 'disable' | |
368 | * methods to exit and enter power saving states. For more information | |
369 | * see comments for struct mmc_host_ops. | |
370 | */ | |
371 | int mmc_host_enable(struct mmc_host *host) | |
372 | { | |
373 | if (!(host->caps & MMC_CAP_DISABLE)) | |
374 | return 0; | |
375 | ||
376 | if (host->en_dis_recurs) | |
377 | return 0; | |
378 | ||
379 | if (host->nesting_cnt++) | |
380 | return 0; | |
381 | ||
382 | cancel_delayed_work_sync(&host->disable); | |
383 | ||
384 | if (host->enabled) | |
385 | return 0; | |
386 | ||
387 | if (host->ops->enable) { | |
388 | int err; | |
389 | ||
390 | host->en_dis_recurs = 1; | |
391 | err = host->ops->enable(host); | |
392 | host->en_dis_recurs = 0; | |
393 | ||
394 | if (err) { | |
395 | pr_debug("%s: enable error %d\n", | |
396 | mmc_hostname(host), err); | |
397 | return err; | |
398 | } | |
399 | } | |
400 | host->enabled = 1; | |
401 | return 0; | |
402 | } | |
403 | EXPORT_SYMBOL(mmc_host_enable); | |
404 | ||
405 | static int mmc_host_do_disable(struct mmc_host *host, int lazy) | |
406 | { | |
407 | if (host->ops->disable) { | |
408 | int err; | |
409 | ||
410 | host->en_dis_recurs = 1; | |
411 | err = host->ops->disable(host, lazy); | |
412 | host->en_dis_recurs = 0; | |
413 | ||
414 | if (err < 0) { | |
415 | pr_debug("%s: disable error %d\n", | |
416 | mmc_hostname(host), err); | |
417 | return err; | |
418 | } | |
419 | if (err > 0) { | |
420 | unsigned long delay = msecs_to_jiffies(err); | |
421 | ||
422 | mmc_schedule_delayed_work(&host->disable, delay); | |
423 | } | |
424 | } | |
425 | host->enabled = 0; | |
426 | return 0; | |
427 | } | |
428 | ||
429 | /** | |
430 | * mmc_host_disable - disable a host. | |
431 | * @host: mmc host to disable | |
432 | * | |
433 | * Hosts that support power saving can use the 'enable' and 'disable' | |
434 | * methods to exit and enter power saving states. For more information | |
435 | * see comments for struct mmc_host_ops. | |
436 | */ | |
437 | int mmc_host_disable(struct mmc_host *host) | |
438 | { | |
439 | int err; | |
440 | ||
441 | if (!(host->caps & MMC_CAP_DISABLE)) | |
442 | return 0; | |
443 | ||
444 | if (host->en_dis_recurs) | |
445 | return 0; | |
446 | ||
447 | if (--host->nesting_cnt) | |
448 | return 0; | |
449 | ||
450 | if (!host->enabled) | |
451 | return 0; | |
452 | ||
453 | err = mmc_host_do_disable(host, 0); | |
454 | return err; | |
455 | } | |
456 | EXPORT_SYMBOL(mmc_host_disable); | |
457 | ||
1da177e4 | 458 | /** |
2342f332 | 459 | * __mmc_claim_host - exclusively claim a host |
1da177e4 | 460 | * @host: mmc host to claim |
2342f332 | 461 | * @abort: whether or not the operation should be aborted |
1da177e4 | 462 | * |
2342f332 NP |
463 | * Claim a host for a set of operations. If @abort is non null and |
464 | * dereference a non-zero value then this will return prematurely with | |
465 | * that non-zero value without acquiring the lock. Returns zero | |
466 | * with the lock held otherwise. | |
1da177e4 | 467 | */ |
2342f332 | 468 | int __mmc_claim_host(struct mmc_host *host, atomic_t *abort) |
1da177e4 LT |
469 | { |
470 | DECLARE_WAITQUEUE(wait, current); | |
471 | unsigned long flags; | |
2342f332 | 472 | int stop; |
1da177e4 | 473 | |
cf795bfb PO |
474 | might_sleep(); |
475 | ||
1da177e4 LT |
476 | add_wait_queue(&host->wq, &wait); |
477 | spin_lock_irqsave(&host->lock, flags); | |
478 | while (1) { | |
479 | set_current_state(TASK_UNINTERRUPTIBLE); | |
2342f332 | 480 | stop = abort ? atomic_read(abort) : 0; |
319a3f14 | 481 | if (stop || !host->claimed || host->claimer == current) |
1da177e4 LT |
482 | break; |
483 | spin_unlock_irqrestore(&host->lock, flags); | |
484 | schedule(); | |
485 | spin_lock_irqsave(&host->lock, flags); | |
486 | } | |
487 | set_current_state(TASK_RUNNING); | |
319a3f14 | 488 | if (!stop) { |
2342f332 | 489 | host->claimed = 1; |
319a3f14 AH |
490 | host->claimer = current; |
491 | host->claim_cnt += 1; | |
492 | } else | |
2342f332 | 493 | wake_up(&host->wq); |
1da177e4 LT |
494 | spin_unlock_irqrestore(&host->lock, flags); |
495 | remove_wait_queue(&host->wq, &wait); | |
8ea926b2 AH |
496 | if (!stop) |
497 | mmc_host_enable(host); | |
2342f332 | 498 | return stop; |
1da177e4 LT |
499 | } |
500 | ||
2342f332 | 501 | EXPORT_SYMBOL(__mmc_claim_host); |
1da177e4 | 502 | |
319a3f14 AH |
503 | /** |
504 | * mmc_try_claim_host - try exclusively to claim a host | |
505 | * @host: mmc host to claim | |
506 | * | |
507 | * Returns %1 if the host is claimed, %0 otherwise. | |
508 | */ | |
509 | int mmc_try_claim_host(struct mmc_host *host) | |
8ea926b2 AH |
510 | { |
511 | int claimed_host = 0; | |
512 | unsigned long flags; | |
513 | ||
514 | spin_lock_irqsave(&host->lock, flags); | |
319a3f14 | 515 | if (!host->claimed || host->claimer == current) { |
8ea926b2 | 516 | host->claimed = 1; |
319a3f14 AH |
517 | host->claimer = current; |
518 | host->claim_cnt += 1; | |
8ea926b2 AH |
519 | claimed_host = 1; |
520 | } | |
521 | spin_unlock_irqrestore(&host->lock, flags); | |
522 | return claimed_host; | |
523 | } | |
319a3f14 | 524 | EXPORT_SYMBOL(mmc_try_claim_host); |
8ea926b2 AH |
525 | |
526 | static void mmc_do_release_host(struct mmc_host *host) | |
527 | { | |
528 | unsigned long flags; | |
529 | ||
530 | spin_lock_irqsave(&host->lock, flags); | |
319a3f14 AH |
531 | if (--host->claim_cnt) { |
532 | /* Release for nested claim */ | |
533 | spin_unlock_irqrestore(&host->lock, flags); | |
534 | } else { | |
535 | host->claimed = 0; | |
536 | host->claimer = NULL; | |
537 | spin_unlock_irqrestore(&host->lock, flags); | |
538 | wake_up(&host->wq); | |
539 | } | |
8ea926b2 AH |
540 | } |
541 | ||
542 | void mmc_host_deeper_disable(struct work_struct *work) | |
543 | { | |
544 | struct mmc_host *host = | |
545 | container_of(work, struct mmc_host, disable.work); | |
546 | ||
547 | /* If the host is claimed then we do not want to disable it anymore */ | |
548 | if (!mmc_try_claim_host(host)) | |
549 | return; | |
550 | mmc_host_do_disable(host, 1); | |
551 | mmc_do_release_host(host); | |
552 | } | |
553 | ||
554 | /** | |
555 | * mmc_host_lazy_disable - lazily disable a host. | |
556 | * @host: mmc host to disable | |
557 | * | |
558 | * Hosts that support power saving can use the 'enable' and 'disable' | |
559 | * methods to exit and enter power saving states. For more information | |
560 | * see comments for struct mmc_host_ops. | |
561 | */ | |
562 | int mmc_host_lazy_disable(struct mmc_host *host) | |
563 | { | |
564 | if (!(host->caps & MMC_CAP_DISABLE)) | |
565 | return 0; | |
566 | ||
567 | if (host->en_dis_recurs) | |
568 | return 0; | |
569 | ||
570 | if (--host->nesting_cnt) | |
571 | return 0; | |
572 | ||
573 | if (!host->enabled) | |
574 | return 0; | |
575 | ||
576 | if (host->disable_delay) { | |
577 | mmc_schedule_delayed_work(&host->disable, | |
578 | msecs_to_jiffies(host->disable_delay)); | |
579 | return 0; | |
580 | } else | |
581 | return mmc_host_do_disable(host, 1); | |
582 | } | |
583 | EXPORT_SYMBOL(mmc_host_lazy_disable); | |
584 | ||
1da177e4 LT |
585 | /** |
586 | * mmc_release_host - release a host | |
587 | * @host: mmc host to release | |
588 | * | |
589 | * Release a MMC host, allowing others to claim the host | |
590 | * for their operations. | |
591 | */ | |
592 | void mmc_release_host(struct mmc_host *host) | |
593 | { | |
d84075c8 | 594 | WARN_ON(!host->claimed); |
1da177e4 | 595 | |
8ea926b2 | 596 | mmc_host_lazy_disable(host); |
1da177e4 | 597 | |
8ea926b2 | 598 | mmc_do_release_host(host); |
1da177e4 LT |
599 | } |
600 | ||
601 | EXPORT_SYMBOL(mmc_release_host); | |
602 | ||
7ea239d9 PO |
603 | /* |
604 | * Internal function that does the actual ios call to the host driver, | |
605 | * optionally printing some debug output. | |
606 | */ | |
920e70c5 RK |
607 | static inline void mmc_set_ios(struct mmc_host *host) |
608 | { | |
609 | struct mmc_ios *ios = &host->ios; | |
610 | ||
cd9277c0 PO |
611 | pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u " |
612 | "width %u timing %u\n", | |
920e70c5 RK |
613 | mmc_hostname(host), ios->clock, ios->bus_mode, |
614 | ios->power_mode, ios->chip_select, ios->vdd, | |
cd9277c0 | 615 | ios->bus_width, ios->timing); |
fba68bd2 | 616 | |
920e70c5 RK |
617 | host->ops->set_ios(host, ios); |
618 | } | |
619 | ||
7ea239d9 PO |
620 | /* |
621 | * Control chip select pin on a host. | |
622 | */ | |
da7fbe58 | 623 | void mmc_set_chip_select(struct mmc_host *host, int mode) |
1da177e4 | 624 | { |
da7fbe58 PO |
625 | host->ios.chip_select = mode; |
626 | mmc_set_ios(host); | |
1da177e4 LT |
627 | } |
628 | ||
7ea239d9 PO |
629 | /* |
630 | * Sets the host clock to the highest possible frequency that | |
631 | * is below "hz". | |
632 | */ | |
633 | void mmc_set_clock(struct mmc_host *host, unsigned int hz) | |
634 | { | |
635 | WARN_ON(hz < host->f_min); | |
636 | ||
637 | if (hz > host->f_max) | |
638 | hz = host->f_max; | |
639 | ||
640 | host->ios.clock = hz; | |
641 | mmc_set_ios(host); | |
642 | } | |
643 | ||
644 | /* | |
645 | * Change the bus mode (open drain/push-pull) of a host. | |
646 | */ | |
647 | void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode) | |
648 | { | |
649 | host->ios.bus_mode = mode; | |
650 | mmc_set_ios(host); | |
651 | } | |
652 | ||
653 | /* | |
654 | * Change data bus width of a host. | |
655 | */ | |
656 | void mmc_set_bus_width(struct mmc_host *host, unsigned int width) | |
657 | { | |
658 | host->ios.bus_width = width; | |
659 | mmc_set_ios(host); | |
660 | } | |
661 | ||
86e8286a AV |
662 | /** |
663 | * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number | |
664 | * @vdd: voltage (mV) | |
665 | * @low_bits: prefer low bits in boundary cases | |
666 | * | |
667 | * This function returns the OCR bit number according to the provided @vdd | |
668 | * value. If conversion is not possible a negative errno value returned. | |
669 | * | |
670 | * Depending on the @low_bits flag the function prefers low or high OCR bits | |
671 | * on boundary voltages. For example, | |
672 | * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33); | |
673 | * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34); | |
674 | * | |
675 | * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21). | |
676 | */ | |
677 | static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits) | |
678 | { | |
679 | const int max_bit = ilog2(MMC_VDD_35_36); | |
680 | int bit; | |
681 | ||
682 | if (vdd < 1650 || vdd > 3600) | |
683 | return -EINVAL; | |
684 | ||
685 | if (vdd >= 1650 && vdd <= 1950) | |
686 | return ilog2(MMC_VDD_165_195); | |
687 | ||
688 | if (low_bits) | |
689 | vdd -= 1; | |
690 | ||
691 | /* Base 2000 mV, step 100 mV, bit's base 8. */ | |
692 | bit = (vdd - 2000) / 100 + 8; | |
693 | if (bit > max_bit) | |
694 | return max_bit; | |
695 | return bit; | |
696 | } | |
697 | ||
698 | /** | |
699 | * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask | |
700 | * @vdd_min: minimum voltage value (mV) | |
701 | * @vdd_max: maximum voltage value (mV) | |
702 | * | |
703 | * This function returns the OCR mask bits according to the provided @vdd_min | |
704 | * and @vdd_max values. If conversion is not possible the function returns 0. | |
705 | * | |
706 | * Notes wrt boundary cases: | |
707 | * This function sets the OCR bits for all boundary voltages, for example | |
708 | * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 | | |
709 | * MMC_VDD_34_35 mask. | |
710 | */ | |
711 | u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max) | |
712 | { | |
713 | u32 mask = 0; | |
714 | ||
715 | if (vdd_max < vdd_min) | |
716 | return 0; | |
717 | ||
718 | /* Prefer high bits for the boundary vdd_max values. */ | |
719 | vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false); | |
720 | if (vdd_max < 0) | |
721 | return 0; | |
722 | ||
723 | /* Prefer low bits for the boundary vdd_min values. */ | |
724 | vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true); | |
725 | if (vdd_min < 0) | |
726 | return 0; | |
727 | ||
728 | /* Fill the mask, from max bit to min bit. */ | |
729 | while (vdd_max >= vdd_min) | |
730 | mask |= 1 << vdd_max--; | |
731 | ||
732 | return mask; | |
733 | } | |
734 | EXPORT_SYMBOL(mmc_vddrange_to_ocrmask); | |
735 | ||
5c13941a DB |
736 | #ifdef CONFIG_REGULATOR |
737 | ||
738 | /** | |
739 | * mmc_regulator_get_ocrmask - return mask of supported voltages | |
740 | * @supply: regulator to use | |
741 | * | |
742 | * This returns either a negative errno, or a mask of voltages that | |
743 | * can be provided to MMC/SD/SDIO devices using the specified voltage | |
744 | * regulator. This would normally be called before registering the | |
745 | * MMC host adapter. | |
746 | */ | |
747 | int mmc_regulator_get_ocrmask(struct regulator *supply) | |
748 | { | |
749 | int result = 0; | |
750 | int count; | |
751 | int i; | |
752 | ||
753 | count = regulator_count_voltages(supply); | |
754 | if (count < 0) | |
755 | return count; | |
756 | ||
757 | for (i = 0; i < count; i++) { | |
758 | int vdd_uV; | |
759 | int vdd_mV; | |
760 | ||
761 | vdd_uV = regulator_list_voltage(supply, i); | |
762 | if (vdd_uV <= 0) | |
763 | continue; | |
764 | ||
765 | vdd_mV = vdd_uV / 1000; | |
766 | result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV); | |
767 | } | |
768 | ||
769 | return result; | |
770 | } | |
771 | EXPORT_SYMBOL(mmc_regulator_get_ocrmask); | |
772 | ||
773 | /** | |
774 | * mmc_regulator_set_ocr - set regulator to match host->ios voltage | |
775 | * @vdd_bit: zero for power off, else a bit number (host->ios.vdd) | |
776 | * @supply: regulator to use | |
777 | * | |
778 | * Returns zero on success, else negative errno. | |
779 | * | |
780 | * MMC host drivers may use this to enable or disable a regulator using | |
781 | * a particular supply voltage. This would normally be called from the | |
782 | * set_ios() method. | |
783 | */ | |
784 | int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit) | |
785 | { | |
786 | int result = 0; | |
787 | int min_uV, max_uV; | |
788 | int enabled; | |
789 | ||
790 | enabled = regulator_is_enabled(supply); | |
791 | if (enabled < 0) | |
792 | return enabled; | |
793 | ||
794 | if (vdd_bit) { | |
795 | int tmp; | |
796 | int voltage; | |
797 | ||
798 | /* REVISIT mmc_vddrange_to_ocrmask() may have set some | |
799 | * bits this regulator doesn't quite support ... don't | |
800 | * be too picky, most cards and regulators are OK with | |
801 | * a 0.1V range goof (it's a small error percentage). | |
802 | */ | |
803 | tmp = vdd_bit - ilog2(MMC_VDD_165_195); | |
804 | if (tmp == 0) { | |
805 | min_uV = 1650 * 1000; | |
806 | max_uV = 1950 * 1000; | |
807 | } else { | |
808 | min_uV = 1900 * 1000 + tmp * 100 * 1000; | |
809 | max_uV = min_uV + 100 * 1000; | |
810 | } | |
811 | ||
812 | /* avoid needless changes to this voltage; the regulator | |
813 | * might not allow this operation | |
814 | */ | |
815 | voltage = regulator_get_voltage(supply); | |
816 | if (voltage < 0) | |
817 | result = voltage; | |
818 | else if (voltage < min_uV || voltage > max_uV) | |
819 | result = regulator_set_voltage(supply, min_uV, max_uV); | |
820 | else | |
821 | result = 0; | |
822 | ||
823 | if (result == 0 && !enabled) | |
824 | result = regulator_enable(supply); | |
825 | } else if (enabled) { | |
826 | result = regulator_disable(supply); | |
827 | } | |
828 | ||
829 | return result; | |
830 | } | |
831 | EXPORT_SYMBOL(mmc_regulator_set_ocr); | |
832 | ||
833 | #endif | |
834 | ||
1da177e4 LT |
835 | /* |
836 | * Mask off any voltages we don't support and select | |
837 | * the lowest voltage | |
838 | */ | |
7ea239d9 | 839 | u32 mmc_select_voltage(struct mmc_host *host, u32 ocr) |
1da177e4 LT |
840 | { |
841 | int bit; | |
842 | ||
843 | ocr &= host->ocr_avail; | |
844 | ||
845 | bit = ffs(ocr); | |
846 | if (bit) { | |
847 | bit -= 1; | |
848 | ||
63ef731a | 849 | ocr &= 3 << bit; |
1da177e4 LT |
850 | |
851 | host->ios.vdd = bit; | |
920e70c5 | 852 | mmc_set_ios(host); |
1da177e4 | 853 | } else { |
f6e10b86 DB |
854 | pr_warning("%s: host doesn't support card's voltages\n", |
855 | mmc_hostname(host)); | |
1da177e4 LT |
856 | ocr = 0; |
857 | } | |
858 | ||
859 | return ocr; | |
860 | } | |
861 | ||
b57c43ad | 862 | /* |
7ea239d9 | 863 | * Select timing parameters for host. |
b57c43ad | 864 | */ |
7ea239d9 | 865 | void mmc_set_timing(struct mmc_host *host, unsigned int timing) |
b57c43ad | 866 | { |
7ea239d9 PO |
867 | host->ios.timing = timing; |
868 | mmc_set_ios(host); | |
b57c43ad PO |
869 | } |
870 | ||
1da177e4 | 871 | /* |
45f8245b RK |
872 | * Apply power to the MMC stack. This is a two-stage process. |
873 | * First, we enable power to the card without the clock running. | |
874 | * We then wait a bit for the power to stabilise. Finally, | |
875 | * enable the bus drivers and clock to the card. | |
876 | * | |
877 | * We must _NOT_ enable the clock prior to power stablising. | |
878 | * | |
879 | * If a host does all the power sequencing itself, ignore the | |
880 | * initial MMC_POWER_UP stage. | |
1da177e4 LT |
881 | */ |
882 | static void mmc_power_up(struct mmc_host *host) | |
883 | { | |
500f3564 BR |
884 | int bit; |
885 | ||
886 | /* If ocr is set, we use it */ | |
887 | if (host->ocr) | |
888 | bit = ffs(host->ocr) - 1; | |
889 | else | |
890 | bit = fls(host->ocr_avail) - 1; | |
1da177e4 LT |
891 | |
892 | host->ios.vdd = bit; | |
af517150 DB |
893 | if (mmc_host_is_spi(host)) { |
894 | host->ios.chip_select = MMC_CS_HIGH; | |
895 | host->ios.bus_mode = MMC_BUSMODE_PUSHPULL; | |
896 | } else { | |
897 | host->ios.chip_select = MMC_CS_DONTCARE; | |
898 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; | |
899 | } | |
1da177e4 | 900 | host->ios.power_mode = MMC_POWER_UP; |
f218278a | 901 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
cd9277c0 | 902 | host->ios.timing = MMC_TIMING_LEGACY; |
920e70c5 | 903 | mmc_set_ios(host); |
1da177e4 | 904 | |
f9996aee PO |
905 | /* |
906 | * This delay should be sufficient to allow the power supply | |
907 | * to reach the minimum voltage. | |
908 | */ | |
79bccc5a | 909 | mmc_delay(10); |
1da177e4 | 910 | |
88ae8b86 | 911 | host->ios.clock = host->f_init; |
8dfd0374 | 912 | |
1da177e4 | 913 | host->ios.power_mode = MMC_POWER_ON; |
920e70c5 | 914 | mmc_set_ios(host); |
1da177e4 | 915 | |
f9996aee PO |
916 | /* |
917 | * This delay must be at least 74 clock sizes, or 1 ms, or the | |
918 | * time required to reach a stable voltage. | |
919 | */ | |
79bccc5a | 920 | mmc_delay(10); |
1da177e4 LT |
921 | } |
922 | ||
923 | static void mmc_power_off(struct mmc_host *host) | |
924 | { | |
925 | host->ios.clock = 0; | |
926 | host->ios.vdd = 0; | |
af517150 DB |
927 | if (!mmc_host_is_spi(host)) { |
928 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; | |
929 | host->ios.chip_select = MMC_CS_DONTCARE; | |
930 | } | |
1da177e4 | 931 | host->ios.power_mode = MMC_POWER_OFF; |
f218278a | 932 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
cd9277c0 | 933 | host->ios.timing = MMC_TIMING_LEGACY; |
920e70c5 | 934 | mmc_set_ios(host); |
1da177e4 LT |
935 | } |
936 | ||
39361851 AB |
937 | /* |
938 | * Cleanup when the last reference to the bus operator is dropped. | |
939 | */ | |
261172fd | 940 | static void __mmc_release_bus(struct mmc_host *host) |
39361851 AB |
941 | { |
942 | BUG_ON(!host); | |
943 | BUG_ON(host->bus_refs); | |
944 | BUG_ON(!host->bus_dead); | |
945 | ||
946 | host->bus_ops = NULL; | |
947 | } | |
948 | ||
949 | /* | |
950 | * Increase reference count of bus operator | |
951 | */ | |
952 | static inline void mmc_bus_get(struct mmc_host *host) | |
953 | { | |
954 | unsigned long flags; | |
955 | ||
956 | spin_lock_irqsave(&host->lock, flags); | |
957 | host->bus_refs++; | |
958 | spin_unlock_irqrestore(&host->lock, flags); | |
959 | } | |
960 | ||
961 | /* | |
962 | * Decrease reference count of bus operator and free it if | |
963 | * it is the last reference. | |
964 | */ | |
965 | static inline void mmc_bus_put(struct mmc_host *host) | |
966 | { | |
967 | unsigned long flags; | |
968 | ||
969 | spin_lock_irqsave(&host->lock, flags); | |
970 | host->bus_refs--; | |
971 | if ((host->bus_refs == 0) && host->bus_ops) | |
972 | __mmc_release_bus(host); | |
973 | spin_unlock_irqrestore(&host->lock, flags); | |
974 | } | |
975 | ||
1da177e4 | 976 | /* |
7ea239d9 PO |
977 | * Assign a mmc bus handler to a host. Only one bus handler may control a |
978 | * host at any given time. | |
1da177e4 | 979 | */ |
7ea239d9 | 980 | void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops) |
1da177e4 | 981 | { |
7ea239d9 | 982 | unsigned long flags; |
e45a1bd2 | 983 | |
7ea239d9 PO |
984 | BUG_ON(!host); |
985 | BUG_ON(!ops); | |
b855885e | 986 | |
d84075c8 | 987 | WARN_ON(!host->claimed); |
bce40a36 | 988 | |
7ea239d9 | 989 | spin_lock_irqsave(&host->lock, flags); |
bce40a36 | 990 | |
7ea239d9 PO |
991 | BUG_ON(host->bus_ops); |
992 | BUG_ON(host->bus_refs); | |
b57c43ad | 993 | |
7ea239d9 PO |
994 | host->bus_ops = ops; |
995 | host->bus_refs = 1; | |
996 | host->bus_dead = 0; | |
b57c43ad | 997 | |
7ea239d9 | 998 | spin_unlock_irqrestore(&host->lock, flags); |
b57c43ad PO |
999 | } |
1000 | ||
7ea239d9 PO |
1001 | /* |
1002 | * Remove the current bus handler from a host. Assumes that there are | |
1003 | * no interesting cards left, so the bus is powered down. | |
1004 | */ | |
1005 | void mmc_detach_bus(struct mmc_host *host) | |
7ccd266e | 1006 | { |
7ea239d9 | 1007 | unsigned long flags; |
7ccd266e | 1008 | |
7ea239d9 | 1009 | BUG_ON(!host); |
7ccd266e | 1010 | |
d84075c8 PO |
1011 | WARN_ON(!host->claimed); |
1012 | WARN_ON(!host->bus_ops); | |
cd9277c0 | 1013 | |
7ea239d9 | 1014 | spin_lock_irqsave(&host->lock, flags); |
7ccd266e | 1015 | |
7ea239d9 | 1016 | host->bus_dead = 1; |
7ccd266e | 1017 | |
7ea239d9 | 1018 | spin_unlock_irqrestore(&host->lock, flags); |
1da177e4 | 1019 | |
7ea239d9 | 1020 | mmc_power_off(host); |
1da177e4 | 1021 | |
7ea239d9 | 1022 | mmc_bus_put(host); |
1da177e4 LT |
1023 | } |
1024 | ||
1da177e4 LT |
1025 | /** |
1026 | * mmc_detect_change - process change of state on a MMC socket | |
1027 | * @host: host which changed state. | |
8dc00335 | 1028 | * @delay: optional delay to wait before detection (jiffies) |
1da177e4 | 1029 | * |
67a61c48 PO |
1030 | * MMC drivers should call this when they detect a card has been |
1031 | * inserted or removed. The MMC layer will confirm that any | |
1032 | * present card is still functional, and initialize any newly | |
1033 | * inserted. | |
1da177e4 | 1034 | */ |
8dc00335 | 1035 | void mmc_detect_change(struct mmc_host *host, unsigned long delay) |
1da177e4 | 1036 | { |
3b91e550 | 1037 | #ifdef CONFIG_MMC_DEBUG |
1efd48b3 | 1038 | unsigned long flags; |
01f41ec7 | 1039 | spin_lock_irqsave(&host->lock, flags); |
d84075c8 | 1040 | WARN_ON(host->removed); |
01f41ec7 | 1041 | spin_unlock_irqrestore(&host->lock, flags); |
3b91e550 PO |
1042 | #endif |
1043 | ||
c4028958 | 1044 | mmc_schedule_delayed_work(&host->detect, delay); |
1da177e4 LT |
1045 | } |
1046 | ||
1047 | EXPORT_SYMBOL(mmc_detect_change); | |
1048 | ||
dfe86cba AH |
1049 | void mmc_init_erase(struct mmc_card *card) |
1050 | { | |
1051 | unsigned int sz; | |
1052 | ||
1053 | if (is_power_of_2(card->erase_size)) | |
1054 | card->erase_shift = ffs(card->erase_size) - 1; | |
1055 | else | |
1056 | card->erase_shift = 0; | |
1057 | ||
1058 | /* | |
1059 | * It is possible to erase an arbitrarily large area of an SD or MMC | |
1060 | * card. That is not desirable because it can take a long time | |
1061 | * (minutes) potentially delaying more important I/O, and also the | |
1062 | * timeout calculations become increasingly hugely over-estimated. | |
1063 | * Consequently, 'pref_erase' is defined as a guide to limit erases | |
1064 | * to that size and alignment. | |
1065 | * | |
1066 | * For SD cards that define Allocation Unit size, limit erases to one | |
1067 | * Allocation Unit at a time. For MMC cards that define High Capacity | |
1068 | * Erase Size, whether it is switched on or not, limit to that size. | |
1069 | * Otherwise just have a stab at a good value. For modern cards it | |
1070 | * will end up being 4MiB. Note that if the value is too small, it | |
1071 | * can end up taking longer to erase. | |
1072 | */ | |
1073 | if (mmc_card_sd(card) && card->ssr.au) { | |
1074 | card->pref_erase = card->ssr.au; | |
1075 | card->erase_shift = ffs(card->ssr.au) - 1; | |
1076 | } else if (card->ext_csd.hc_erase_size) { | |
1077 | card->pref_erase = card->ext_csd.hc_erase_size; | |
1078 | } else { | |
1079 | sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11; | |
1080 | if (sz < 128) | |
1081 | card->pref_erase = 512 * 1024 / 512; | |
1082 | else if (sz < 512) | |
1083 | card->pref_erase = 1024 * 1024 / 512; | |
1084 | else if (sz < 1024) | |
1085 | card->pref_erase = 2 * 1024 * 1024 / 512; | |
1086 | else | |
1087 | card->pref_erase = 4 * 1024 * 1024 / 512; | |
1088 | if (card->pref_erase < card->erase_size) | |
1089 | card->pref_erase = card->erase_size; | |
1090 | else { | |
1091 | sz = card->pref_erase % card->erase_size; | |
1092 | if (sz) | |
1093 | card->pref_erase += card->erase_size - sz; | |
1094 | } | |
1095 | } | |
1096 | } | |
1097 | ||
1098 | static void mmc_set_mmc_erase_timeout(struct mmc_card *card, | |
1099 | struct mmc_command *cmd, | |
1100 | unsigned int arg, unsigned int qty) | |
1101 | { | |
1102 | unsigned int erase_timeout; | |
1103 | ||
1104 | if (card->ext_csd.erase_group_def & 1) { | |
1105 | /* High Capacity Erase Group Size uses HC timeouts */ | |
1106 | if (arg == MMC_TRIM_ARG) | |
1107 | erase_timeout = card->ext_csd.trim_timeout; | |
1108 | else | |
1109 | erase_timeout = card->ext_csd.hc_erase_timeout; | |
1110 | } else { | |
1111 | /* CSD Erase Group Size uses write timeout */ | |
1112 | unsigned int mult = (10 << card->csd.r2w_factor); | |
1113 | unsigned int timeout_clks = card->csd.tacc_clks * mult; | |
1114 | unsigned int timeout_us; | |
1115 | ||
1116 | /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */ | |
1117 | if (card->csd.tacc_ns < 1000000) | |
1118 | timeout_us = (card->csd.tacc_ns * mult) / 1000; | |
1119 | else | |
1120 | timeout_us = (card->csd.tacc_ns / 1000) * mult; | |
1121 | ||
1122 | /* | |
1123 | * ios.clock is only a target. The real clock rate might be | |
1124 | * less but not that much less, so fudge it by multiplying by 2. | |
1125 | */ | |
1126 | timeout_clks <<= 1; | |
1127 | timeout_us += (timeout_clks * 1000) / | |
1128 | (card->host->ios.clock / 1000); | |
1129 | ||
1130 | erase_timeout = timeout_us / 1000; | |
1131 | ||
1132 | /* | |
1133 | * Theoretically, the calculation could underflow so round up | |
1134 | * to 1ms in that case. | |
1135 | */ | |
1136 | if (!erase_timeout) | |
1137 | erase_timeout = 1; | |
1138 | } | |
1139 | ||
1140 | /* Multiplier for secure operations */ | |
1141 | if (arg & MMC_SECURE_ARGS) { | |
1142 | if (arg == MMC_SECURE_ERASE_ARG) | |
1143 | erase_timeout *= card->ext_csd.sec_erase_mult; | |
1144 | else | |
1145 | erase_timeout *= card->ext_csd.sec_trim_mult; | |
1146 | } | |
1147 | ||
1148 | erase_timeout *= qty; | |
1149 | ||
1150 | /* | |
1151 | * Ensure at least a 1 second timeout for SPI as per | |
1152 | * 'mmc_set_data_timeout()' | |
1153 | */ | |
1154 | if (mmc_host_is_spi(card->host) && erase_timeout < 1000) | |
1155 | erase_timeout = 1000; | |
1156 | ||
1157 | cmd->erase_timeout = erase_timeout; | |
1158 | } | |
1159 | ||
1160 | static void mmc_set_sd_erase_timeout(struct mmc_card *card, | |
1161 | struct mmc_command *cmd, unsigned int arg, | |
1162 | unsigned int qty) | |
1163 | { | |
1164 | if (card->ssr.erase_timeout) { | |
1165 | /* Erase timeout specified in SD Status Register (SSR) */ | |
1166 | cmd->erase_timeout = card->ssr.erase_timeout * qty + | |
1167 | card->ssr.erase_offset; | |
1168 | } else { | |
1169 | /* | |
1170 | * Erase timeout not specified in SD Status Register (SSR) so | |
1171 | * use 250ms per write block. | |
1172 | */ | |
1173 | cmd->erase_timeout = 250 * qty; | |
1174 | } | |
1175 | ||
1176 | /* Must not be less than 1 second */ | |
1177 | if (cmd->erase_timeout < 1000) | |
1178 | cmd->erase_timeout = 1000; | |
1179 | } | |
1180 | ||
1181 | static void mmc_set_erase_timeout(struct mmc_card *card, | |
1182 | struct mmc_command *cmd, unsigned int arg, | |
1183 | unsigned int qty) | |
1184 | { | |
1185 | if (mmc_card_sd(card)) | |
1186 | mmc_set_sd_erase_timeout(card, cmd, arg, qty); | |
1187 | else | |
1188 | mmc_set_mmc_erase_timeout(card, cmd, arg, qty); | |
1189 | } | |
1190 | ||
1191 | static int mmc_do_erase(struct mmc_card *card, unsigned int from, | |
1192 | unsigned int to, unsigned int arg) | |
1193 | { | |
1194 | struct mmc_command cmd; | |
1195 | unsigned int qty = 0; | |
1196 | int err; | |
1197 | ||
1198 | /* | |
1199 | * qty is used to calculate the erase timeout which depends on how many | |
1200 | * erase groups (or allocation units in SD terminology) are affected. | |
1201 | * We count erasing part of an erase group as one erase group. | |
1202 | * For SD, the allocation units are always a power of 2. For MMC, the | |
1203 | * erase group size is almost certainly also power of 2, but it does not | |
1204 | * seem to insist on that in the JEDEC standard, so we fall back to | |
1205 | * division in that case. SD may not specify an allocation unit size, | |
1206 | * in which case the timeout is based on the number of write blocks. | |
1207 | * | |
1208 | * Note that the timeout for secure trim 2 will only be correct if the | |
1209 | * number of erase groups specified is the same as the total of all | |
1210 | * preceding secure trim 1 commands. Since the power may have been | |
1211 | * lost since the secure trim 1 commands occurred, it is generally | |
1212 | * impossible to calculate the secure trim 2 timeout correctly. | |
1213 | */ | |
1214 | if (card->erase_shift) | |
1215 | qty += ((to >> card->erase_shift) - | |
1216 | (from >> card->erase_shift)) + 1; | |
1217 | else if (mmc_card_sd(card)) | |
1218 | qty += to - from + 1; | |
1219 | else | |
1220 | qty += ((to / card->erase_size) - | |
1221 | (from / card->erase_size)) + 1; | |
1222 | ||
1223 | if (!mmc_card_blockaddr(card)) { | |
1224 | from <<= 9; | |
1225 | to <<= 9; | |
1226 | } | |
1227 | ||
1228 | memset(&cmd, 0, sizeof(struct mmc_command)); | |
1229 | if (mmc_card_sd(card)) | |
1230 | cmd.opcode = SD_ERASE_WR_BLK_START; | |
1231 | else | |
1232 | cmd.opcode = MMC_ERASE_GROUP_START; | |
1233 | cmd.arg = from; | |
1234 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; | |
1235 | err = mmc_wait_for_cmd(card->host, &cmd, 0); | |
1236 | if (err) { | |
1237 | printk(KERN_ERR "mmc_erase: group start error %d, " | |
1238 | "status %#x\n", err, cmd.resp[0]); | |
1239 | err = -EINVAL; | |
1240 | goto out; | |
1241 | } | |
1242 | ||
1243 | memset(&cmd, 0, sizeof(struct mmc_command)); | |
1244 | if (mmc_card_sd(card)) | |
1245 | cmd.opcode = SD_ERASE_WR_BLK_END; | |
1246 | else | |
1247 | cmd.opcode = MMC_ERASE_GROUP_END; | |
1248 | cmd.arg = to; | |
1249 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; | |
1250 | err = mmc_wait_for_cmd(card->host, &cmd, 0); | |
1251 | if (err) { | |
1252 | printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n", | |
1253 | err, cmd.resp[0]); | |
1254 | err = -EINVAL; | |
1255 | goto out; | |
1256 | } | |
1257 | ||
1258 | memset(&cmd, 0, sizeof(struct mmc_command)); | |
1259 | cmd.opcode = MMC_ERASE; | |
1260 | cmd.arg = arg; | |
1261 | cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; | |
1262 | mmc_set_erase_timeout(card, &cmd, arg, qty); | |
1263 | err = mmc_wait_for_cmd(card->host, &cmd, 0); | |
1264 | if (err) { | |
1265 | printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n", | |
1266 | err, cmd.resp[0]); | |
1267 | err = -EIO; | |
1268 | goto out; | |
1269 | } | |
1270 | ||
1271 | if (mmc_host_is_spi(card->host)) | |
1272 | goto out; | |
1273 | ||
1274 | do { | |
1275 | memset(&cmd, 0, sizeof(struct mmc_command)); | |
1276 | cmd.opcode = MMC_SEND_STATUS; | |
1277 | cmd.arg = card->rca << 16; | |
1278 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; | |
1279 | /* Do not retry else we can't see errors */ | |
1280 | err = mmc_wait_for_cmd(card->host, &cmd, 0); | |
1281 | if (err || (cmd.resp[0] & 0xFDF92000)) { | |
1282 | printk(KERN_ERR "error %d requesting status %#x\n", | |
1283 | err, cmd.resp[0]); | |
1284 | err = -EIO; | |
1285 | goto out; | |
1286 | } | |
1287 | } while (!(cmd.resp[0] & R1_READY_FOR_DATA) || | |
1288 | R1_CURRENT_STATE(cmd.resp[0]) == 7); | |
1289 | out: | |
1290 | return err; | |
1291 | } | |
1292 | ||
1293 | /** | |
1294 | * mmc_erase - erase sectors. | |
1295 | * @card: card to erase | |
1296 | * @from: first sector to erase | |
1297 | * @nr: number of sectors to erase | |
1298 | * @arg: erase command argument (SD supports only %MMC_ERASE_ARG) | |
1299 | * | |
1300 | * Caller must claim host before calling this function. | |
1301 | */ | |
1302 | int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr, | |
1303 | unsigned int arg) | |
1304 | { | |
1305 | unsigned int rem, to = from + nr; | |
1306 | ||
1307 | if (!(card->host->caps & MMC_CAP_ERASE) || | |
1308 | !(card->csd.cmdclass & CCC_ERASE)) | |
1309 | return -EOPNOTSUPP; | |
1310 | ||
1311 | if (!card->erase_size) | |
1312 | return -EOPNOTSUPP; | |
1313 | ||
1314 | if (mmc_card_sd(card) && arg != MMC_ERASE_ARG) | |
1315 | return -EOPNOTSUPP; | |
1316 | ||
1317 | if ((arg & MMC_SECURE_ARGS) && | |
1318 | !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)) | |
1319 | return -EOPNOTSUPP; | |
1320 | ||
1321 | if ((arg & MMC_TRIM_ARGS) && | |
1322 | !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)) | |
1323 | return -EOPNOTSUPP; | |
1324 | ||
1325 | if (arg == MMC_SECURE_ERASE_ARG) { | |
1326 | if (from % card->erase_size || nr % card->erase_size) | |
1327 | return -EINVAL; | |
1328 | } | |
1329 | ||
1330 | if (arg == MMC_ERASE_ARG) { | |
1331 | rem = from % card->erase_size; | |
1332 | if (rem) { | |
1333 | rem = card->erase_size - rem; | |
1334 | from += rem; | |
1335 | if (nr > rem) | |
1336 | nr -= rem; | |
1337 | else | |
1338 | return 0; | |
1339 | } | |
1340 | rem = nr % card->erase_size; | |
1341 | if (rem) | |
1342 | nr -= rem; | |
1343 | } | |
1344 | ||
1345 | if (nr == 0) | |
1346 | return 0; | |
1347 | ||
1348 | to = from + nr; | |
1349 | ||
1350 | if (to <= from) | |
1351 | return -EINVAL; | |
1352 | ||
1353 | /* 'from' and 'to' are inclusive */ | |
1354 | to -= 1; | |
1355 | ||
1356 | return mmc_do_erase(card, from, to, arg); | |
1357 | } | |
1358 | EXPORT_SYMBOL(mmc_erase); | |
1359 | ||
1360 | int mmc_can_erase(struct mmc_card *card) | |
1361 | { | |
1362 | if ((card->host->caps & MMC_CAP_ERASE) && | |
1363 | (card->csd.cmdclass & CCC_ERASE) && card->erase_size) | |
1364 | return 1; | |
1365 | return 0; | |
1366 | } | |
1367 | EXPORT_SYMBOL(mmc_can_erase); | |
1368 | ||
1369 | int mmc_can_trim(struct mmc_card *card) | |
1370 | { | |
1371 | if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN) | |
1372 | return 1; | |
1373 | return 0; | |
1374 | } | |
1375 | EXPORT_SYMBOL(mmc_can_trim); | |
1376 | ||
1377 | int mmc_can_secure_erase_trim(struct mmc_card *card) | |
1378 | { | |
1379 | if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN) | |
1380 | return 1; | |
1381 | return 0; | |
1382 | } | |
1383 | EXPORT_SYMBOL(mmc_can_secure_erase_trim); | |
1384 | ||
1385 | int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from, | |
1386 | unsigned int nr) | |
1387 | { | |
1388 | if (!card->erase_size) | |
1389 | return 0; | |
1390 | if (from % card->erase_size || nr % card->erase_size) | |
1391 | return 0; | |
1392 | return 1; | |
1393 | } | |
1394 | EXPORT_SYMBOL(mmc_erase_group_aligned); | |
1da177e4 | 1395 | |
b93931a6 | 1396 | void mmc_rescan(struct work_struct *work) |
1da177e4 | 1397 | { |
c4028958 DH |
1398 | struct mmc_host *host = |
1399 | container_of(work, struct mmc_host, detect.work); | |
7ea239d9 PO |
1400 | u32 ocr; |
1401 | int err; | |
4c2ef25f | 1402 | unsigned long flags; |
88ae8b86 HT |
1403 | int i; |
1404 | const unsigned freqs[] = { 400000, 300000, 200000, 100000 }; | |
4c2ef25f ML |
1405 | |
1406 | spin_lock_irqsave(&host->lock, flags); | |
1407 | ||
1408 | if (host->rescan_disable) { | |
1409 | spin_unlock_irqrestore(&host->lock, flags); | |
1410 | return; | |
1411 | } | |
1412 | ||
1413 | spin_unlock_irqrestore(&host->lock, flags); | |
1414 | ||
1da177e4 | 1415 | |
7ea239d9 | 1416 | mmc_bus_get(host); |
b855885e | 1417 | |
94d89efb JS |
1418 | /* if there is a card registered, check whether it is still present */ |
1419 | if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead) | |
1420 | host->bus_ops->detect(host); | |
1421 | ||
1422 | mmc_bus_put(host); | |
1423 | ||
1424 | ||
1425 | mmc_bus_get(host); | |
1426 | ||
1427 | /* if there still is a card present, stop here */ | |
1428 | if (host->bus_ops != NULL) { | |
7ea239d9 | 1429 | mmc_bus_put(host); |
94d89efb JS |
1430 | goto out; |
1431 | } | |
1da177e4 | 1432 | |
94d89efb | 1433 | /* detect a newly inserted card */ |
28f52482 | 1434 | |
94d89efb JS |
1435 | /* |
1436 | * Only we can add a new handler, so it's safe to | |
1437 | * release the lock here. | |
1438 | */ | |
1439 | mmc_bus_put(host); | |
1da177e4 | 1440 | |
94d89efb JS |
1441 | if (host->ops->get_cd && host->ops->get_cd(host) == 0) |
1442 | goto out; | |
1da177e4 | 1443 | |
88ae8b86 HT |
1444 | for (i = 0; i < ARRAY_SIZE(freqs); i++) { |
1445 | mmc_claim_host(host); | |
1da177e4 | 1446 | |
88ae8b86 HT |
1447 | if (freqs[i] >= host->f_min) |
1448 | host->f_init = freqs[i]; | |
1449 | else if (!i || freqs[i-1] > host->f_min) | |
1450 | host->f_init = host->f_min; | |
1451 | else { | |
1452 | mmc_release_host(host); | |
1453 | goto out; | |
1454 | } | |
4d0b8611 | 1455 | #ifdef CONFIG_MMC_DEBUG |
88ae8b86 HT |
1456 | pr_info("%s: %s: trying to init card at %u Hz\n", |
1457 | mmc_hostname(host), __func__, host->f_init); | |
4d0b8611 | 1458 | #endif |
88ae8b86 HT |
1459 | mmc_power_up(host); |
1460 | sdio_reset(host); | |
1461 | mmc_go_idle(host); | |
5c4e6f13 | 1462 | |
88ae8b86 | 1463 | mmc_send_if_cond(host, host->ocr_avail); |
7310ece8 | 1464 | |
88ae8b86 HT |
1465 | /* |
1466 | * First we search for SDIO... | |
1467 | */ | |
1468 | err = mmc_send_io_op_cond(host, 0, &ocr); | |
1469 | if (!err) { | |
1470 | if (mmc_attach_sdio(host, ocr)) { | |
1471 | mmc_claim_host(host); | |
1472 | /* | |
1473 | * Try SDMEM (but not MMC) even if SDIO | |
1474 | * is broken. | |
1475 | */ | |
1476 | if (mmc_send_app_op_cond(host, 0, &ocr)) | |
1477 | goto out_fail; | |
1478 | ||
1479 | if (mmc_attach_sd(host, ocr)) | |
1480 | mmc_power_off(host); | |
1481 | } | |
1482 | goto out; | |
1483 | } | |
1484 | ||
1485 | /* | |
1486 | * ...then normal SD... | |
1487 | */ | |
1488 | err = mmc_send_app_op_cond(host, 0, &ocr); | |
1489 | if (!err) { | |
7310ece8 MM |
1490 | if (mmc_attach_sd(host, ocr)) |
1491 | mmc_power_off(host); | |
88ae8b86 | 1492 | goto out; |
7310ece8 | 1493 | } |
5c4e6f13 | 1494 | |
88ae8b86 HT |
1495 | /* |
1496 | * ...and finally MMC. | |
1497 | */ | |
1498 | err = mmc_send_op_cond(host, 0, &ocr); | |
1499 | if (!err) { | |
1500 | if (mmc_attach_mmc(host, ocr)) | |
1501 | mmc_power_off(host); | |
1502 | goto out; | |
1503 | } | |
94d89efb | 1504 | |
7310ece8 | 1505 | out_fail: |
88ae8b86 HT |
1506 | mmc_release_host(host); |
1507 | mmc_power_off(host); | |
1508 | } | |
28f52482 AV |
1509 | out: |
1510 | if (host->caps & MMC_CAP_NEEDS_POLL) | |
1511 | mmc_schedule_delayed_work(&host->detect, HZ); | |
1da177e4 LT |
1512 | } |
1513 | ||
b93931a6 | 1514 | void mmc_start_host(struct mmc_host *host) |
1da177e4 | 1515 | { |
b93931a6 PO |
1516 | mmc_power_off(host); |
1517 | mmc_detect_change(host, 0); | |
1da177e4 LT |
1518 | } |
1519 | ||
b93931a6 | 1520 | void mmc_stop_host(struct mmc_host *host) |
1da177e4 | 1521 | { |
3b91e550 | 1522 | #ifdef CONFIG_MMC_DEBUG |
1efd48b3 PO |
1523 | unsigned long flags; |
1524 | spin_lock_irqsave(&host->lock, flags); | |
3b91e550 | 1525 | host->removed = 1; |
1efd48b3 | 1526 | spin_unlock_irqrestore(&host->lock, flags); |
3b91e550 PO |
1527 | #endif |
1528 | ||
8ea926b2 AH |
1529 | if (host->caps & MMC_CAP_DISABLE) |
1530 | cancel_delayed_work(&host->disable); | |
7de427d0 | 1531 | cancel_delayed_work(&host->detect); |
3b91e550 PO |
1532 | mmc_flush_scheduled_work(); |
1533 | ||
da68c4eb NP |
1534 | /* clear pm flags now and let card drivers set them as needed */ |
1535 | host->pm_flags = 0; | |
1536 | ||
7ea239d9 PO |
1537 | mmc_bus_get(host); |
1538 | if (host->bus_ops && !host->bus_dead) { | |
1539 | if (host->bus_ops->remove) | |
1540 | host->bus_ops->remove(host); | |
1541 | ||
1542 | mmc_claim_host(host); | |
1543 | mmc_detach_bus(host); | |
1544 | mmc_release_host(host); | |
53509f0f DK |
1545 | mmc_bus_put(host); |
1546 | return; | |
1da177e4 | 1547 | } |
7ea239d9 PO |
1548 | mmc_bus_put(host); |
1549 | ||
1550 | BUG_ON(host->card); | |
1da177e4 LT |
1551 | |
1552 | mmc_power_off(host); | |
1553 | } | |
1554 | ||
eae1aeee AH |
1555 | void mmc_power_save_host(struct mmc_host *host) |
1556 | { | |
1557 | mmc_bus_get(host); | |
1558 | ||
1559 | if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) { | |
1560 | mmc_bus_put(host); | |
1561 | return; | |
1562 | } | |
1563 | ||
1564 | if (host->bus_ops->power_save) | |
1565 | host->bus_ops->power_save(host); | |
1566 | ||
1567 | mmc_bus_put(host); | |
1568 | ||
1569 | mmc_power_off(host); | |
1570 | } | |
1571 | EXPORT_SYMBOL(mmc_power_save_host); | |
1572 | ||
1573 | void mmc_power_restore_host(struct mmc_host *host) | |
1574 | { | |
1575 | mmc_bus_get(host); | |
1576 | ||
1577 | if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) { | |
1578 | mmc_bus_put(host); | |
1579 | return; | |
1580 | } | |
1581 | ||
1582 | mmc_power_up(host); | |
1583 | host->bus_ops->power_restore(host); | |
1584 | ||
1585 | mmc_bus_put(host); | |
1586 | } | |
1587 | EXPORT_SYMBOL(mmc_power_restore_host); | |
1588 | ||
b1ebe384 JL |
1589 | int mmc_card_awake(struct mmc_host *host) |
1590 | { | |
1591 | int err = -ENOSYS; | |
1592 | ||
1593 | mmc_bus_get(host); | |
1594 | ||
1595 | if (host->bus_ops && !host->bus_dead && host->bus_ops->awake) | |
1596 | err = host->bus_ops->awake(host); | |
1597 | ||
1598 | mmc_bus_put(host); | |
1599 | ||
1600 | return err; | |
1601 | } | |
1602 | EXPORT_SYMBOL(mmc_card_awake); | |
1603 | ||
1604 | int mmc_card_sleep(struct mmc_host *host) | |
1605 | { | |
1606 | int err = -ENOSYS; | |
1607 | ||
1608 | mmc_bus_get(host); | |
1609 | ||
1610 | if (host->bus_ops && !host->bus_dead && host->bus_ops->awake) | |
1611 | err = host->bus_ops->sleep(host); | |
1612 | ||
1613 | mmc_bus_put(host); | |
1614 | ||
1615 | return err; | |
1616 | } | |
1617 | EXPORT_SYMBOL(mmc_card_sleep); | |
1618 | ||
1619 | int mmc_card_can_sleep(struct mmc_host *host) | |
1620 | { | |
1621 | struct mmc_card *card = host->card; | |
1622 | ||
1623 | if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3) | |
1624 | return 1; | |
1625 | return 0; | |
1626 | } | |
1627 | EXPORT_SYMBOL(mmc_card_can_sleep); | |
1628 | ||
1da177e4 LT |
1629 | #ifdef CONFIG_PM |
1630 | ||
1631 | /** | |
1632 | * mmc_suspend_host - suspend a host | |
1633 | * @host: mmc host | |
1da177e4 | 1634 | */ |
1a13f8fa | 1635 | int mmc_suspend_host(struct mmc_host *host) |
1da177e4 | 1636 | { |
95cdfb72 NP |
1637 | int err = 0; |
1638 | ||
8ea926b2 AH |
1639 | if (host->caps & MMC_CAP_DISABLE) |
1640 | cancel_delayed_work(&host->disable); | |
7de427d0 | 1641 | cancel_delayed_work(&host->detect); |
b5af25be PO |
1642 | mmc_flush_scheduled_work(); |
1643 | ||
7ea239d9 PO |
1644 | mmc_bus_get(host); |
1645 | if (host->bus_ops && !host->bus_dead) { | |
6abaa0c9 | 1646 | if (host->bus_ops->suspend) |
95cdfb72 | 1647 | err = host->bus_ops->suspend(host); |
1c8cf9c9 OBC |
1648 | if (err == -ENOSYS || !host->bus_ops->resume) { |
1649 | /* | |
1650 | * We simply "remove" the card in this case. | |
1651 | * It will be redetected on resume. | |
1652 | */ | |
1653 | if (host->bus_ops->remove) | |
1654 | host->bus_ops->remove(host); | |
1655 | mmc_claim_host(host); | |
1656 | mmc_detach_bus(host); | |
1657 | mmc_release_host(host); | |
1658 | host->pm_flags = 0; | |
1659 | err = 0; | |
1660 | } | |
b5af25be | 1661 | } |
7ea239d9 PO |
1662 | mmc_bus_put(host); |
1663 | ||
da68c4eb | 1664 | if (!err && !(host->pm_flags & MMC_PM_KEEP_POWER)) |
95cdfb72 | 1665 | mmc_power_off(host); |
1da177e4 | 1666 | |
95cdfb72 | 1667 | return err; |
1da177e4 LT |
1668 | } |
1669 | ||
1670 | EXPORT_SYMBOL(mmc_suspend_host); | |
1671 | ||
1672 | /** | |
1673 | * mmc_resume_host - resume a previously suspended host | |
1674 | * @host: mmc host | |
1675 | */ | |
1676 | int mmc_resume_host(struct mmc_host *host) | |
1677 | { | |
95cdfb72 NP |
1678 | int err = 0; |
1679 | ||
6abaa0c9 PO |
1680 | mmc_bus_get(host); |
1681 | if (host->bus_ops && !host->bus_dead) { | |
da68c4eb NP |
1682 | if (!(host->pm_flags & MMC_PM_KEEP_POWER)) { |
1683 | mmc_power_up(host); | |
1684 | mmc_select_voltage(host, host->ocr); | |
1685 | } | |
6abaa0c9 | 1686 | BUG_ON(!host->bus_ops->resume); |
95cdfb72 NP |
1687 | err = host->bus_ops->resume(host); |
1688 | if (err) { | |
1689 | printk(KERN_WARNING "%s: error %d during resume " | |
1690 | "(card was removed?)\n", | |
1691 | mmc_hostname(host), err); | |
95cdfb72 NP |
1692 | err = 0; |
1693 | } | |
6abaa0c9 PO |
1694 | } |
1695 | mmc_bus_put(host); | |
1696 | ||
95cdfb72 | 1697 | return err; |
1da177e4 | 1698 | } |
1da177e4 LT |
1699 | EXPORT_SYMBOL(mmc_resume_host); |
1700 | ||
4c2ef25f ML |
1701 | /* Do the card removal on suspend if card is assumed removeable |
1702 | * Do that in pm notifier while userspace isn't yet frozen, so we will be able | |
1703 | to sync the card. | |
1704 | */ | |
1705 | int mmc_pm_notify(struct notifier_block *notify_block, | |
1706 | unsigned long mode, void *unused) | |
1707 | { | |
1708 | struct mmc_host *host = container_of( | |
1709 | notify_block, struct mmc_host, pm_notify); | |
1710 | unsigned long flags; | |
1711 | ||
1712 | ||
1713 | switch (mode) { | |
1714 | case PM_HIBERNATION_PREPARE: | |
1715 | case PM_SUSPEND_PREPARE: | |
1716 | ||
1717 | spin_lock_irqsave(&host->lock, flags); | |
1718 | host->rescan_disable = 1; | |
1719 | spin_unlock_irqrestore(&host->lock, flags); | |
1720 | cancel_delayed_work_sync(&host->detect); | |
1721 | ||
1722 | if (!host->bus_ops || host->bus_ops->suspend) | |
1723 | break; | |
1724 | ||
1725 | mmc_claim_host(host); | |
1726 | ||
1727 | if (host->bus_ops->remove) | |
1728 | host->bus_ops->remove(host); | |
1729 | ||
1730 | mmc_detach_bus(host); | |
1731 | mmc_release_host(host); | |
1732 | host->pm_flags = 0; | |
1733 | break; | |
1734 | ||
1735 | case PM_POST_SUSPEND: | |
1736 | case PM_POST_HIBERNATION: | |
1737 | ||
1738 | spin_lock_irqsave(&host->lock, flags); | |
1739 | host->rescan_disable = 0; | |
1740 | spin_unlock_irqrestore(&host->lock, flags); | |
1741 | mmc_detect_change(host, 0); | |
1742 | ||
1743 | } | |
1744 | ||
1745 | return 0; | |
1746 | } | |
1da177e4 LT |
1747 | #endif |
1748 | ||
ffce2e7e PO |
1749 | static int __init mmc_init(void) |
1750 | { | |
1751 | int ret; | |
1752 | ||
1753 | workqueue = create_singlethread_workqueue("kmmcd"); | |
1754 | if (!workqueue) | |
1755 | return -ENOMEM; | |
1756 | ||
1757 | ret = mmc_register_bus(); | |
e29a7d73 PO |
1758 | if (ret) |
1759 | goto destroy_workqueue; | |
1760 | ||
1761 | ret = mmc_register_host_class(); | |
1762 | if (ret) | |
1763 | goto unregister_bus; | |
1764 | ||
1765 | ret = sdio_register_bus(); | |
1766 | if (ret) | |
1767 | goto unregister_host_class; | |
1768 | ||
1769 | return 0; | |
1770 | ||
1771 | unregister_host_class: | |
1772 | mmc_unregister_host_class(); | |
1773 | unregister_bus: | |
1774 | mmc_unregister_bus(); | |
1775 | destroy_workqueue: | |
1776 | destroy_workqueue(workqueue); | |
1777 | ||
ffce2e7e PO |
1778 | return ret; |
1779 | } | |
1780 | ||
1781 | static void __exit mmc_exit(void) | |
1782 | { | |
e29a7d73 | 1783 | sdio_unregister_bus(); |
ffce2e7e PO |
1784 | mmc_unregister_host_class(); |
1785 | mmc_unregister_bus(); | |
1786 | destroy_workqueue(workqueue); | |
1787 | } | |
1788 | ||
26074962 | 1789 | subsys_initcall(mmc_init); |
ffce2e7e PO |
1790 | module_exit(mmc_exit); |
1791 | ||
1da177e4 | 1792 | MODULE_LICENSE("GPL"); |