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