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