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