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