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