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