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> |
1da177e4 LT |
23 | |
24 | #include <linux/mmc/card.h> | |
25 | #include <linux/mmc/host.h> | |
da7fbe58 PO |
26 | #include <linux/mmc/mmc.h> |
27 | #include <linux/mmc/sd.h> | |
1da177e4 | 28 | |
aaac1b47 | 29 | #include "core.h" |
ffce2e7e PO |
30 | #include "bus.h" |
31 | #include "host.h" | |
e29a7d73 | 32 | #include "sdio_bus.h" |
da7fbe58 PO |
33 | |
34 | #include "mmc_ops.h" | |
35 | #include "sd_ops.h" | |
5c4e6f13 | 36 | #include "sdio_ops.h" |
1da177e4 | 37 | |
ffce2e7e PO |
38 | static struct workqueue_struct *workqueue; |
39 | ||
af517150 DB |
40 | /* |
41 | * Enabling software CRCs on the data blocks can be a significant (30%) | |
42 | * performance cost, and for other reasons may not always be desired. | |
43 | * So we allow it it to be disabled. | |
44 | */ | |
45 | int use_spi_crc = 1; | |
46 | module_param(use_spi_crc, bool, 0); | |
47 | ||
ffce2e7e PO |
48 | /* |
49 | * Internal function. Schedule delayed work in the MMC work queue. | |
50 | */ | |
51 | static int mmc_schedule_delayed_work(struct delayed_work *work, | |
52 | unsigned long delay) | |
53 | { | |
54 | return queue_delayed_work(workqueue, work, delay); | |
55 | } | |
56 | ||
57 | /* | |
58 | * Internal function. Flush all scheduled work from the MMC work queue. | |
59 | */ | |
60 | static void mmc_flush_scheduled_work(void) | |
61 | { | |
62 | flush_workqueue(workqueue); | |
63 | } | |
64 | ||
1da177e4 | 65 | /** |
fe10c6ab RK |
66 | * mmc_request_done - finish processing an MMC request |
67 | * @host: MMC host which completed request | |
68 | * @mrq: MMC request which request | |
1da177e4 LT |
69 | * |
70 | * MMC drivers should call this function when they have completed | |
fe10c6ab | 71 | * their processing of a request. |
1da177e4 LT |
72 | */ |
73 | void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq) | |
74 | { | |
75 | struct mmc_command *cmd = mrq->cmd; | |
920e70c5 RK |
76 | int err = cmd->error; |
77 | ||
af517150 DB |
78 | if (err && cmd->retries && mmc_host_is_spi(host)) { |
79 | if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND) | |
80 | cmd->retries = 0; | |
81 | } | |
82 | ||
1da177e4 | 83 | if (err && cmd->retries) { |
e4d21708 PO |
84 | pr_debug("%s: req failed (CMD%u): %d, retrying...\n", |
85 | mmc_hostname(host), cmd->opcode, err); | |
86 | ||
1da177e4 LT |
87 | cmd->retries--; |
88 | cmd->error = 0; | |
89 | host->ops->request(host, mrq); | |
e4d21708 | 90 | } else { |
af8350c7 PO |
91 | led_trigger_event(host->led, LED_OFF); |
92 | ||
e4d21708 PO |
93 | pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n", |
94 | mmc_hostname(host), cmd->opcode, err, | |
95 | cmd->resp[0], cmd->resp[1], | |
96 | cmd->resp[2], cmd->resp[3]); | |
97 | ||
98 | if (mrq->data) { | |
99 | pr_debug("%s: %d bytes transferred: %d\n", | |
100 | mmc_hostname(host), | |
101 | mrq->data->bytes_xfered, mrq->data->error); | |
102 | } | |
103 | ||
104 | if (mrq->stop) { | |
105 | pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n", | |
106 | mmc_hostname(host), mrq->stop->opcode, | |
107 | mrq->stop->error, | |
108 | mrq->stop->resp[0], mrq->stop->resp[1], | |
109 | mrq->stop->resp[2], mrq->stop->resp[3]); | |
110 | } | |
111 | ||
112 | if (mrq->done) | |
113 | mrq->done(mrq); | |
1da177e4 LT |
114 | } |
115 | } | |
116 | ||
117 | EXPORT_SYMBOL(mmc_request_done); | |
118 | ||
39361851 | 119 | static void |
1da177e4 LT |
120 | mmc_start_request(struct mmc_host *host, struct mmc_request *mrq) |
121 | { | |
976d9276 PO |
122 | #ifdef CONFIG_MMC_DEBUG |
123 | unsigned int i, sz; | |
a84756c5 | 124 | struct scatterlist *sg; |
976d9276 PO |
125 | #endif |
126 | ||
920e70c5 RK |
127 | pr_debug("%s: starting CMD%u arg %08x flags %08x\n", |
128 | mmc_hostname(host), mrq->cmd->opcode, | |
129 | mrq->cmd->arg, mrq->cmd->flags); | |
1da177e4 | 130 | |
e4d21708 PO |
131 | if (mrq->data) { |
132 | pr_debug("%s: blksz %d blocks %d flags %08x " | |
133 | "tsac %d ms nsac %d\n", | |
134 | mmc_hostname(host), mrq->data->blksz, | |
135 | mrq->data->blocks, mrq->data->flags, | |
ce252edd | 136 | mrq->data->timeout_ns / 1000000, |
e4d21708 PO |
137 | mrq->data->timeout_clks); |
138 | } | |
139 | ||
140 | if (mrq->stop) { | |
141 | pr_debug("%s: CMD%u arg %08x flags %08x\n", | |
142 | mmc_hostname(host), mrq->stop->opcode, | |
143 | mrq->stop->arg, mrq->stop->flags); | |
144 | } | |
145 | ||
f22ee4ed | 146 | WARN_ON(!host->claimed); |
1da177e4 | 147 | |
af8350c7 PO |
148 | led_trigger_event(host->led, LED_FULL); |
149 | ||
1da177e4 LT |
150 | mrq->cmd->error = 0; |
151 | mrq->cmd->mrq = mrq; | |
152 | if (mrq->data) { | |
fe4a3c7a | 153 | BUG_ON(mrq->data->blksz > host->max_blk_size); |
55db890a PO |
154 | BUG_ON(mrq->data->blocks > host->max_blk_count); |
155 | BUG_ON(mrq->data->blocks * mrq->data->blksz > | |
156 | host->max_req_size); | |
fe4a3c7a | 157 | |
976d9276 PO |
158 | #ifdef CONFIG_MMC_DEBUG |
159 | sz = 0; | |
a84756c5 PO |
160 | for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i) |
161 | sz += sg->length; | |
976d9276 PO |
162 | BUG_ON(sz != mrq->data->blocks * mrq->data->blksz); |
163 | #endif | |
164 | ||
1da177e4 LT |
165 | mrq->cmd->data = mrq->data; |
166 | mrq->data->error = 0; | |
167 | mrq->data->mrq = mrq; | |
168 | if (mrq->stop) { | |
169 | mrq->data->stop = mrq->stop; | |
170 | mrq->stop->error = 0; | |
171 | mrq->stop->mrq = mrq; | |
172 | } | |
173 | } | |
174 | host->ops->request(host, mrq); | |
175 | } | |
176 | ||
1da177e4 LT |
177 | static void mmc_wait_done(struct mmc_request *mrq) |
178 | { | |
179 | complete(mrq->done_data); | |
180 | } | |
181 | ||
67a61c48 PO |
182 | /** |
183 | * mmc_wait_for_req - start a request and wait for completion | |
184 | * @host: MMC host to start command | |
185 | * @mrq: MMC request to start | |
186 | * | |
187 | * Start a new MMC custom command request for a host, and wait | |
188 | * for the command to complete. Does not attempt to parse the | |
189 | * response. | |
190 | */ | |
191 | void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq) | |
1da177e4 | 192 | { |
0afffc72 | 193 | DECLARE_COMPLETION_ONSTACK(complete); |
1da177e4 LT |
194 | |
195 | mrq->done_data = &complete; | |
196 | mrq->done = mmc_wait_done; | |
197 | ||
198 | mmc_start_request(host, mrq); | |
199 | ||
200 | wait_for_completion(&complete); | |
1da177e4 LT |
201 | } |
202 | ||
203 | EXPORT_SYMBOL(mmc_wait_for_req); | |
204 | ||
205 | /** | |
206 | * mmc_wait_for_cmd - start a command and wait for completion | |
207 | * @host: MMC host to start command | |
208 | * @cmd: MMC command to start | |
209 | * @retries: maximum number of retries | |
210 | * | |
211 | * Start a new MMC command for a host, and wait for the command | |
212 | * to complete. Return any error that occurred while the command | |
213 | * was executing. Do not attempt to parse the response. | |
214 | */ | |
215 | int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries) | |
216 | { | |
217 | struct mmc_request mrq; | |
218 | ||
d84075c8 | 219 | WARN_ON(!host->claimed); |
1da177e4 LT |
220 | |
221 | memset(&mrq, 0, sizeof(struct mmc_request)); | |
222 | ||
223 | memset(cmd->resp, 0, sizeof(cmd->resp)); | |
224 | cmd->retries = retries; | |
225 | ||
226 | mrq.cmd = cmd; | |
227 | cmd->data = NULL; | |
228 | ||
229 | mmc_wait_for_req(host, &mrq); | |
230 | ||
231 | return cmd->error; | |
232 | } | |
233 | ||
234 | EXPORT_SYMBOL(mmc_wait_for_cmd); | |
235 | ||
d773d725 RK |
236 | /** |
237 | * mmc_set_data_timeout - set the timeout for a data command | |
238 | * @data: data phase for command | |
239 | * @card: the MMC card associated with the data transfer | |
67a61c48 PO |
240 | * |
241 | * Computes the data timeout parameters according to the | |
242 | * correct algorithm given the card type. | |
d773d725 | 243 | */ |
b146d26a | 244 | void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card) |
d773d725 RK |
245 | { |
246 | unsigned int mult; | |
247 | ||
e6f918bf PO |
248 | /* |
249 | * SDIO cards only define an upper 1 s limit on access. | |
250 | */ | |
251 | if (mmc_card_sdio(card)) { | |
252 | data->timeout_ns = 1000000000; | |
253 | data->timeout_clks = 0; | |
254 | return; | |
255 | } | |
256 | ||
d773d725 RK |
257 | /* |
258 | * SD cards use a 100 multiplier rather than 10 | |
259 | */ | |
260 | mult = mmc_card_sd(card) ? 100 : 10; | |
261 | ||
262 | /* | |
263 | * Scale up the multiplier (and therefore the timeout) by | |
264 | * the r2w factor for writes. | |
265 | */ | |
b146d26a | 266 | if (data->flags & MMC_DATA_WRITE) |
d773d725 RK |
267 | mult <<= card->csd.r2w_factor; |
268 | ||
269 | data->timeout_ns = card->csd.tacc_ns * mult; | |
270 | data->timeout_clks = card->csd.tacc_clks * mult; | |
271 | ||
272 | /* | |
273 | * SD cards also have an upper limit on the timeout. | |
274 | */ | |
275 | if (mmc_card_sd(card)) { | |
276 | unsigned int timeout_us, limit_us; | |
277 | ||
278 | timeout_us = data->timeout_ns / 1000; | |
279 | timeout_us += data->timeout_clks * 1000 / | |
280 | (card->host->ios.clock / 1000); | |
281 | ||
b146d26a | 282 | if (data->flags & MMC_DATA_WRITE) |
493890e7 PO |
283 | /* |
284 | * The limit is really 250 ms, but that is | |
285 | * insufficient for some crappy cards. | |
286 | */ | |
287 | limit_us = 300000; | |
d773d725 RK |
288 | else |
289 | limit_us = 100000; | |
290 | ||
fba68bd2 PL |
291 | /* |
292 | * SDHC cards always use these fixed values. | |
293 | */ | |
294 | if (timeout_us > limit_us || mmc_card_blockaddr(card)) { | |
d773d725 RK |
295 | data->timeout_ns = limit_us * 1000; |
296 | data->timeout_clks = 0; | |
297 | } | |
298 | } | |
299 | } | |
300 | EXPORT_SYMBOL(mmc_set_data_timeout); | |
301 | ||
ad3868b2 PO |
302 | /** |
303 | * mmc_align_data_size - pads a transfer size to a more optimal value | |
304 | * @card: the MMC card associated with the data transfer | |
305 | * @sz: original transfer size | |
306 | * | |
307 | * Pads the original data size with a number of extra bytes in | |
308 | * order to avoid controller bugs and/or performance hits | |
309 | * (e.g. some controllers revert to PIO for certain sizes). | |
310 | * | |
311 | * Returns the improved size, which might be unmodified. | |
312 | * | |
313 | * Note that this function is only relevant when issuing a | |
314 | * single scatter gather entry. | |
315 | */ | |
316 | unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz) | |
317 | { | |
318 | /* | |
319 | * FIXME: We don't have a system for the controller to tell | |
320 | * the core about its problems yet, so for now we just 32-bit | |
321 | * align the size. | |
322 | */ | |
323 | sz = ((sz + 3) / 4) * 4; | |
324 | ||
325 | return sz; | |
326 | } | |
327 | EXPORT_SYMBOL(mmc_align_data_size); | |
328 | ||
1da177e4 | 329 | /** |
2342f332 | 330 | * __mmc_claim_host - exclusively claim a host |
1da177e4 | 331 | * @host: mmc host to claim |
2342f332 | 332 | * @abort: whether or not the operation should be aborted |
1da177e4 | 333 | * |
2342f332 NP |
334 | * Claim a host for a set of operations. If @abort is non null and |
335 | * dereference a non-zero value then this will return prematurely with | |
336 | * that non-zero value without acquiring the lock. Returns zero | |
337 | * with the lock held otherwise. | |
1da177e4 | 338 | */ |
2342f332 | 339 | int __mmc_claim_host(struct mmc_host *host, atomic_t *abort) |
1da177e4 LT |
340 | { |
341 | DECLARE_WAITQUEUE(wait, current); | |
342 | unsigned long flags; | |
2342f332 | 343 | int stop; |
1da177e4 | 344 | |
cf795bfb PO |
345 | might_sleep(); |
346 | ||
1da177e4 LT |
347 | add_wait_queue(&host->wq, &wait); |
348 | spin_lock_irqsave(&host->lock, flags); | |
349 | while (1) { | |
350 | set_current_state(TASK_UNINTERRUPTIBLE); | |
2342f332 NP |
351 | stop = abort ? atomic_read(abort) : 0; |
352 | if (stop || !host->claimed) | |
1da177e4 LT |
353 | break; |
354 | spin_unlock_irqrestore(&host->lock, flags); | |
355 | schedule(); | |
356 | spin_lock_irqsave(&host->lock, flags); | |
357 | } | |
358 | set_current_state(TASK_RUNNING); | |
2342f332 NP |
359 | if (!stop) |
360 | host->claimed = 1; | |
361 | else | |
362 | wake_up(&host->wq); | |
1da177e4 LT |
363 | spin_unlock_irqrestore(&host->lock, flags); |
364 | remove_wait_queue(&host->wq, &wait); | |
2342f332 | 365 | return stop; |
1da177e4 LT |
366 | } |
367 | ||
2342f332 | 368 | EXPORT_SYMBOL(__mmc_claim_host); |
1da177e4 LT |
369 | |
370 | /** | |
371 | * mmc_release_host - release a host | |
372 | * @host: mmc host to release | |
373 | * | |
374 | * Release a MMC host, allowing others to claim the host | |
375 | * for their operations. | |
376 | */ | |
377 | void mmc_release_host(struct mmc_host *host) | |
378 | { | |
379 | unsigned long flags; | |
380 | ||
d84075c8 | 381 | WARN_ON(!host->claimed); |
1da177e4 LT |
382 | |
383 | spin_lock_irqsave(&host->lock, flags); | |
f22ee4ed | 384 | host->claimed = 0; |
1da177e4 LT |
385 | spin_unlock_irqrestore(&host->lock, flags); |
386 | ||
387 | wake_up(&host->wq); | |
388 | } | |
389 | ||
390 | EXPORT_SYMBOL(mmc_release_host); | |
391 | ||
7ea239d9 PO |
392 | /* |
393 | * Internal function that does the actual ios call to the host driver, | |
394 | * optionally printing some debug output. | |
395 | */ | |
920e70c5 RK |
396 | static inline void mmc_set_ios(struct mmc_host *host) |
397 | { | |
398 | struct mmc_ios *ios = &host->ios; | |
399 | ||
cd9277c0 PO |
400 | pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u " |
401 | "width %u timing %u\n", | |
920e70c5 RK |
402 | mmc_hostname(host), ios->clock, ios->bus_mode, |
403 | ios->power_mode, ios->chip_select, ios->vdd, | |
cd9277c0 | 404 | ios->bus_width, ios->timing); |
fba68bd2 | 405 | |
920e70c5 RK |
406 | host->ops->set_ios(host, ios); |
407 | } | |
408 | ||
7ea239d9 PO |
409 | /* |
410 | * Control chip select pin on a host. | |
411 | */ | |
da7fbe58 | 412 | void mmc_set_chip_select(struct mmc_host *host, int mode) |
1da177e4 | 413 | { |
da7fbe58 PO |
414 | host->ios.chip_select = mode; |
415 | mmc_set_ios(host); | |
1da177e4 LT |
416 | } |
417 | ||
7ea239d9 PO |
418 | /* |
419 | * Sets the host clock to the highest possible frequency that | |
420 | * is below "hz". | |
421 | */ | |
422 | void mmc_set_clock(struct mmc_host *host, unsigned int hz) | |
423 | { | |
424 | WARN_ON(hz < host->f_min); | |
425 | ||
426 | if (hz > host->f_max) | |
427 | hz = host->f_max; | |
428 | ||
429 | host->ios.clock = hz; | |
430 | mmc_set_ios(host); | |
431 | } | |
432 | ||
433 | /* | |
434 | * Change the bus mode (open drain/push-pull) of a host. | |
435 | */ | |
436 | void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode) | |
437 | { | |
438 | host->ios.bus_mode = mode; | |
439 | mmc_set_ios(host); | |
440 | } | |
441 | ||
442 | /* | |
443 | * Change data bus width of a host. | |
444 | */ | |
445 | void mmc_set_bus_width(struct mmc_host *host, unsigned int width) | |
446 | { | |
447 | host->ios.bus_width = width; | |
448 | mmc_set_ios(host); | |
449 | } | |
450 | ||
1da177e4 LT |
451 | /* |
452 | * Mask off any voltages we don't support and select | |
453 | * the lowest voltage | |
454 | */ | |
7ea239d9 | 455 | u32 mmc_select_voltage(struct mmc_host *host, u32 ocr) |
1da177e4 LT |
456 | { |
457 | int bit; | |
458 | ||
459 | ocr &= host->ocr_avail; | |
460 | ||
461 | bit = ffs(ocr); | |
462 | if (bit) { | |
463 | bit -= 1; | |
464 | ||
63ef731a | 465 | ocr &= 3 << bit; |
1da177e4 LT |
466 | |
467 | host->ios.vdd = bit; | |
920e70c5 | 468 | mmc_set_ios(host); |
1da177e4 LT |
469 | } else { |
470 | ocr = 0; | |
471 | } | |
472 | ||
473 | return ocr; | |
474 | } | |
475 | ||
b57c43ad | 476 | /* |
7ea239d9 | 477 | * Select timing parameters for host. |
b57c43ad | 478 | */ |
7ea239d9 | 479 | void mmc_set_timing(struct mmc_host *host, unsigned int timing) |
b57c43ad | 480 | { |
7ea239d9 PO |
481 | host->ios.timing = timing; |
482 | mmc_set_ios(host); | |
b57c43ad PO |
483 | } |
484 | ||
1da177e4 | 485 | /* |
45f8245b RK |
486 | * Apply power to the MMC stack. This is a two-stage process. |
487 | * First, we enable power to the card without the clock running. | |
488 | * We then wait a bit for the power to stabilise. Finally, | |
489 | * enable the bus drivers and clock to the card. | |
490 | * | |
491 | * We must _NOT_ enable the clock prior to power stablising. | |
492 | * | |
493 | * If a host does all the power sequencing itself, ignore the | |
494 | * initial MMC_POWER_UP stage. | |
1da177e4 LT |
495 | */ |
496 | static void mmc_power_up(struct mmc_host *host) | |
497 | { | |
498 | int bit = fls(host->ocr_avail) - 1; | |
499 | ||
500 | host->ios.vdd = bit; | |
af517150 DB |
501 | if (mmc_host_is_spi(host)) { |
502 | host->ios.chip_select = MMC_CS_HIGH; | |
503 | host->ios.bus_mode = MMC_BUSMODE_PUSHPULL; | |
504 | } else { | |
505 | host->ios.chip_select = MMC_CS_DONTCARE; | |
506 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; | |
507 | } | |
1da177e4 | 508 | host->ios.power_mode = MMC_POWER_UP; |
f218278a | 509 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
cd9277c0 | 510 | host->ios.timing = MMC_TIMING_LEGACY; |
920e70c5 | 511 | mmc_set_ios(host); |
1da177e4 | 512 | |
f9996aee PO |
513 | /* |
514 | * This delay should be sufficient to allow the power supply | |
515 | * to reach the minimum voltage. | |
516 | */ | |
517 | mmc_delay(2); | |
1da177e4 LT |
518 | |
519 | host->ios.clock = host->f_min; | |
520 | host->ios.power_mode = MMC_POWER_ON; | |
920e70c5 | 521 | mmc_set_ios(host); |
1da177e4 | 522 | |
f9996aee PO |
523 | /* |
524 | * This delay must be at least 74 clock sizes, or 1 ms, or the | |
525 | * time required to reach a stable voltage. | |
526 | */ | |
1da177e4 LT |
527 | mmc_delay(2); |
528 | } | |
529 | ||
530 | static void mmc_power_off(struct mmc_host *host) | |
531 | { | |
532 | host->ios.clock = 0; | |
533 | host->ios.vdd = 0; | |
af517150 DB |
534 | if (!mmc_host_is_spi(host)) { |
535 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; | |
536 | host->ios.chip_select = MMC_CS_DONTCARE; | |
537 | } | |
1da177e4 | 538 | host->ios.power_mode = MMC_POWER_OFF; |
f218278a | 539 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
cd9277c0 | 540 | host->ios.timing = MMC_TIMING_LEGACY; |
920e70c5 | 541 | mmc_set_ios(host); |
1da177e4 LT |
542 | } |
543 | ||
39361851 AB |
544 | /* |
545 | * Cleanup when the last reference to the bus operator is dropped. | |
546 | */ | |
261172fd | 547 | static void __mmc_release_bus(struct mmc_host *host) |
39361851 AB |
548 | { |
549 | BUG_ON(!host); | |
550 | BUG_ON(host->bus_refs); | |
551 | BUG_ON(!host->bus_dead); | |
552 | ||
553 | host->bus_ops = NULL; | |
554 | } | |
555 | ||
556 | /* | |
557 | * Increase reference count of bus operator | |
558 | */ | |
559 | static inline void mmc_bus_get(struct mmc_host *host) | |
560 | { | |
561 | unsigned long flags; | |
562 | ||
563 | spin_lock_irqsave(&host->lock, flags); | |
564 | host->bus_refs++; | |
565 | spin_unlock_irqrestore(&host->lock, flags); | |
566 | } | |
567 | ||
568 | /* | |
569 | * Decrease reference count of bus operator and free it if | |
570 | * it is the last reference. | |
571 | */ | |
572 | static inline void mmc_bus_put(struct mmc_host *host) | |
573 | { | |
574 | unsigned long flags; | |
575 | ||
576 | spin_lock_irqsave(&host->lock, flags); | |
577 | host->bus_refs--; | |
578 | if ((host->bus_refs == 0) && host->bus_ops) | |
579 | __mmc_release_bus(host); | |
580 | spin_unlock_irqrestore(&host->lock, flags); | |
581 | } | |
582 | ||
1da177e4 | 583 | /* |
7ea239d9 PO |
584 | * Assign a mmc bus handler to a host. Only one bus handler may control a |
585 | * host at any given time. | |
1da177e4 | 586 | */ |
7ea239d9 | 587 | void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops) |
1da177e4 | 588 | { |
7ea239d9 | 589 | unsigned long flags; |
e45a1bd2 | 590 | |
7ea239d9 PO |
591 | BUG_ON(!host); |
592 | BUG_ON(!ops); | |
b855885e | 593 | |
d84075c8 | 594 | WARN_ON(!host->claimed); |
bce40a36 | 595 | |
7ea239d9 | 596 | spin_lock_irqsave(&host->lock, flags); |
bce40a36 | 597 | |
7ea239d9 PO |
598 | BUG_ON(host->bus_ops); |
599 | BUG_ON(host->bus_refs); | |
b57c43ad | 600 | |
7ea239d9 PO |
601 | host->bus_ops = ops; |
602 | host->bus_refs = 1; | |
603 | host->bus_dead = 0; | |
b57c43ad | 604 | |
7ea239d9 | 605 | spin_unlock_irqrestore(&host->lock, flags); |
b57c43ad PO |
606 | } |
607 | ||
7ea239d9 PO |
608 | /* |
609 | * Remove the current bus handler from a host. Assumes that there are | |
610 | * no interesting cards left, so the bus is powered down. | |
611 | */ | |
612 | void mmc_detach_bus(struct mmc_host *host) | |
7ccd266e | 613 | { |
7ea239d9 | 614 | unsigned long flags; |
7ccd266e | 615 | |
7ea239d9 | 616 | BUG_ON(!host); |
7ccd266e | 617 | |
d84075c8 PO |
618 | WARN_ON(!host->claimed); |
619 | WARN_ON(!host->bus_ops); | |
cd9277c0 | 620 | |
7ea239d9 | 621 | spin_lock_irqsave(&host->lock, flags); |
7ccd266e | 622 | |
7ea239d9 | 623 | host->bus_dead = 1; |
7ccd266e | 624 | |
7ea239d9 | 625 | spin_unlock_irqrestore(&host->lock, flags); |
1da177e4 | 626 | |
7ea239d9 | 627 | mmc_power_off(host); |
1da177e4 | 628 | |
7ea239d9 | 629 | mmc_bus_put(host); |
1da177e4 LT |
630 | } |
631 | ||
1da177e4 LT |
632 | /** |
633 | * mmc_detect_change - process change of state on a MMC socket | |
634 | * @host: host which changed state. | |
8dc00335 | 635 | * @delay: optional delay to wait before detection (jiffies) |
1da177e4 | 636 | * |
67a61c48 PO |
637 | * MMC drivers should call this when they detect a card has been |
638 | * inserted or removed. The MMC layer will confirm that any | |
639 | * present card is still functional, and initialize any newly | |
640 | * inserted. | |
1da177e4 | 641 | */ |
8dc00335 | 642 | void mmc_detect_change(struct mmc_host *host, unsigned long delay) |
1da177e4 | 643 | { |
3b91e550 | 644 | #ifdef CONFIG_MMC_DEBUG |
1efd48b3 | 645 | unsigned long flags; |
01f41ec7 | 646 | spin_lock_irqsave(&host->lock, flags); |
d84075c8 | 647 | WARN_ON(host->removed); |
01f41ec7 | 648 | spin_unlock_irqrestore(&host->lock, flags); |
3b91e550 PO |
649 | #endif |
650 | ||
c4028958 | 651 | mmc_schedule_delayed_work(&host->detect, delay); |
1da177e4 LT |
652 | } |
653 | ||
654 | EXPORT_SYMBOL(mmc_detect_change); | |
655 | ||
656 | ||
b93931a6 | 657 | void mmc_rescan(struct work_struct *work) |
1da177e4 | 658 | { |
c4028958 DH |
659 | struct mmc_host *host = |
660 | container_of(work, struct mmc_host, detect.work); | |
7ea239d9 PO |
661 | u32 ocr; |
662 | int err; | |
1da177e4 | 663 | |
7ea239d9 | 664 | mmc_bus_get(host); |
b855885e | 665 | |
7ea239d9 | 666 | if (host->bus_ops == NULL) { |
1da177e4 | 667 | /* |
7ea239d9 PO |
668 | * Only we can add a new handler, so it's safe to |
669 | * release the lock here. | |
1da177e4 | 670 | */ |
7ea239d9 | 671 | mmc_bus_put(host); |
1da177e4 | 672 | |
28f52482 AV |
673 | if (host->ops->get_cd && host->ops->get_cd(host) == 0) |
674 | goto out; | |
675 | ||
7ea239d9 | 676 | mmc_claim_host(host); |
1da177e4 | 677 | |
7ea239d9 PO |
678 | mmc_power_up(host); |
679 | mmc_go_idle(host); | |
1da177e4 | 680 | |
7ea239d9 | 681 | mmc_send_if_cond(host, host->ocr_avail); |
1da177e4 | 682 | |
5c4e6f13 PO |
683 | /* |
684 | * First we search for SDIO... | |
685 | */ | |
686 | err = mmc_send_io_op_cond(host, 0, &ocr); | |
687 | if (!err) { | |
688 | if (mmc_attach_sdio(host, ocr)) | |
689 | mmc_power_off(host); | |
28f52482 | 690 | goto out; |
5c4e6f13 PO |
691 | } |
692 | ||
693 | /* | |
694 | * ...then normal SD... | |
695 | */ | |
7ea239d9 | 696 | err = mmc_send_app_op_cond(host, 0, &ocr); |
17b0429d | 697 | if (!err) { |
7ea239d9 PO |
698 | if (mmc_attach_sd(host, ocr)) |
699 | mmc_power_off(host); | |
28f52482 | 700 | goto out; |
5c4e6f13 PO |
701 | } |
702 | ||
703 | /* | |
704 | * ...and finally MMC. | |
705 | */ | |
706 | err = mmc_send_op_cond(host, 0, &ocr); | |
707 | if (!err) { | |
708 | if (mmc_attach_mmc(host, ocr)) | |
7ea239d9 | 709 | mmc_power_off(host); |
28f52482 | 710 | goto out; |
7ea239d9 | 711 | } |
5c4e6f13 PO |
712 | |
713 | mmc_release_host(host); | |
714 | mmc_power_off(host); | |
7ea239d9 PO |
715 | } else { |
716 | if (host->bus_ops->detect && !host->bus_dead) | |
717 | host->bus_ops->detect(host); | |
718 | ||
719 | mmc_bus_put(host); | |
720 | } | |
28f52482 AV |
721 | out: |
722 | if (host->caps & MMC_CAP_NEEDS_POLL) | |
723 | mmc_schedule_delayed_work(&host->detect, HZ); | |
1da177e4 LT |
724 | } |
725 | ||
b93931a6 | 726 | void mmc_start_host(struct mmc_host *host) |
1da177e4 | 727 | { |
b93931a6 PO |
728 | mmc_power_off(host); |
729 | mmc_detect_change(host, 0); | |
1da177e4 LT |
730 | } |
731 | ||
b93931a6 | 732 | void mmc_stop_host(struct mmc_host *host) |
1da177e4 | 733 | { |
3b91e550 | 734 | #ifdef CONFIG_MMC_DEBUG |
1efd48b3 PO |
735 | unsigned long flags; |
736 | spin_lock_irqsave(&host->lock, flags); | |
3b91e550 | 737 | host->removed = 1; |
1efd48b3 | 738 | spin_unlock_irqrestore(&host->lock, flags); |
3b91e550 PO |
739 | #endif |
740 | ||
741 | mmc_flush_scheduled_work(); | |
742 | ||
7ea239d9 PO |
743 | mmc_bus_get(host); |
744 | if (host->bus_ops && !host->bus_dead) { | |
745 | if (host->bus_ops->remove) | |
746 | host->bus_ops->remove(host); | |
747 | ||
748 | mmc_claim_host(host); | |
749 | mmc_detach_bus(host); | |
750 | mmc_release_host(host); | |
1da177e4 | 751 | } |
7ea239d9 PO |
752 | mmc_bus_put(host); |
753 | ||
754 | BUG_ON(host->card); | |
1da177e4 LT |
755 | |
756 | mmc_power_off(host); | |
757 | } | |
758 | ||
1da177e4 LT |
759 | #ifdef CONFIG_PM |
760 | ||
761 | /** | |
762 | * mmc_suspend_host - suspend a host | |
763 | * @host: mmc host | |
764 | * @state: suspend mode (PM_SUSPEND_xxx) | |
765 | */ | |
e5378ca8 | 766 | int mmc_suspend_host(struct mmc_host *host, pm_message_t state) |
1da177e4 | 767 | { |
b5af25be PO |
768 | mmc_flush_scheduled_work(); |
769 | ||
7ea239d9 PO |
770 | mmc_bus_get(host); |
771 | if (host->bus_ops && !host->bus_dead) { | |
6abaa0c9 PO |
772 | if (host->bus_ops->suspend) |
773 | host->bus_ops->suspend(host); | |
774 | if (!host->bus_ops->resume) { | |
775 | if (host->bus_ops->remove) | |
776 | host->bus_ops->remove(host); | |
777 | ||
778 | mmc_claim_host(host); | |
779 | mmc_detach_bus(host); | |
780 | mmc_release_host(host); | |
781 | } | |
b5af25be | 782 | } |
7ea239d9 PO |
783 | mmc_bus_put(host); |
784 | ||
1da177e4 | 785 | mmc_power_off(host); |
1da177e4 LT |
786 | |
787 | return 0; | |
788 | } | |
789 | ||
790 | EXPORT_SYMBOL(mmc_suspend_host); | |
791 | ||
792 | /** | |
793 | * mmc_resume_host - resume a previously suspended host | |
794 | * @host: mmc host | |
795 | */ | |
796 | int mmc_resume_host(struct mmc_host *host) | |
797 | { | |
6abaa0c9 PO |
798 | mmc_bus_get(host); |
799 | if (host->bus_ops && !host->bus_dead) { | |
800 | mmc_power_up(host); | |
801 | BUG_ON(!host->bus_ops->resume); | |
802 | host->bus_ops->resume(host); | |
803 | } | |
804 | mmc_bus_put(host); | |
805 | ||
806 | /* | |
807 | * We add a slight delay here so that resume can progress | |
808 | * in parallel. | |
809 | */ | |
810 | mmc_detect_change(host, 1); | |
1da177e4 LT |
811 | |
812 | return 0; | |
813 | } | |
814 | ||
815 | EXPORT_SYMBOL(mmc_resume_host); | |
816 | ||
817 | #endif | |
818 | ||
ffce2e7e PO |
819 | static int __init mmc_init(void) |
820 | { | |
821 | int ret; | |
822 | ||
823 | workqueue = create_singlethread_workqueue("kmmcd"); | |
824 | if (!workqueue) | |
825 | return -ENOMEM; | |
826 | ||
827 | ret = mmc_register_bus(); | |
e29a7d73 PO |
828 | if (ret) |
829 | goto destroy_workqueue; | |
830 | ||
831 | ret = mmc_register_host_class(); | |
832 | if (ret) | |
833 | goto unregister_bus; | |
834 | ||
835 | ret = sdio_register_bus(); | |
836 | if (ret) | |
837 | goto unregister_host_class; | |
838 | ||
839 | return 0; | |
840 | ||
841 | unregister_host_class: | |
842 | mmc_unregister_host_class(); | |
843 | unregister_bus: | |
844 | mmc_unregister_bus(); | |
845 | destroy_workqueue: | |
846 | destroy_workqueue(workqueue); | |
847 | ||
ffce2e7e PO |
848 | return ret; |
849 | } | |
850 | ||
851 | static void __exit mmc_exit(void) | |
852 | { | |
e29a7d73 | 853 | sdio_unregister_bus(); |
ffce2e7e PO |
854 | mmc_unregister_host_class(); |
855 | mmc_unregister_bus(); | |
856 | destroy_workqueue(workqueue); | |
857 | } | |
858 | ||
26074962 | 859 | subsys_initcall(mmc_init); |
ffce2e7e PO |
860 | module_exit(mmc_exit); |
861 | ||
1da177e4 | 862 | MODULE_LICENSE("GPL"); |