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