Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/drivers/mmc/mmc.c | |
3 | * | |
4 | * Copyright (C) 2003-2004 Russell King, All Rights Reserved. | |
5b4fd9ae PO |
5 | * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved. |
6 | * SD support Copyright (C) 2005 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> | |
26 | #include <linux/mmc/protocol.h> | |
27 | ||
28 | #include "mmc.h" | |
29 | ||
1da177e4 LT |
30 | #define CMD_RETRIES 3 |
31 | ||
32 | /* | |
33 | * OCR Bit positions to 10s of Vdd mV. | |
34 | */ | |
35 | static const unsigned short mmc_ocr_bit_to_vdd[] = { | |
36 | 150, 155, 160, 165, 170, 180, 190, 200, | |
37 | 210, 220, 230, 240, 250, 260, 270, 280, | |
38 | 290, 300, 310, 320, 330, 340, 350, 360 | |
39 | }; | |
40 | ||
41 | static const unsigned int tran_exp[] = { | |
42 | 10000, 100000, 1000000, 10000000, | |
43 | 0, 0, 0, 0 | |
44 | }; | |
45 | ||
46 | static const unsigned char tran_mant[] = { | |
47 | 0, 10, 12, 13, 15, 20, 25, 30, | |
48 | 35, 40, 45, 50, 55, 60, 70, 80, | |
49 | }; | |
50 | ||
51 | static const unsigned int tacc_exp[] = { | |
52 | 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, | |
53 | }; | |
54 | ||
55 | static const unsigned int tacc_mant[] = { | |
56 | 0, 10, 12, 13, 15, 20, 25, 30, | |
57 | 35, 40, 45, 50, 55, 60, 70, 80, | |
58 | }; | |
59 | ||
60 | ||
61 | /** | |
fe10c6ab RK |
62 | * mmc_request_done - finish processing an MMC request |
63 | * @host: MMC host which completed request | |
64 | * @mrq: MMC request which request | |
1da177e4 LT |
65 | * |
66 | * MMC drivers should call this function when they have completed | |
fe10c6ab | 67 | * their processing of a request. |
1da177e4 LT |
68 | */ |
69 | void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq) | |
70 | { | |
71 | struct mmc_command *cmd = mrq->cmd; | |
920e70c5 RK |
72 | int err = cmd->error; |
73 | ||
74 | pr_debug("%s: req done (CMD%u): %d/%d/%d: %08x %08x %08x %08x\n", | |
75 | mmc_hostname(host), cmd->opcode, err, | |
76 | mrq->data ? mrq->data->error : 0, | |
77 | mrq->stop ? mrq->stop->error : 0, | |
78 | cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]); | |
1da177e4 LT |
79 | |
80 | if (err && cmd->retries) { | |
81 | cmd->retries--; | |
82 | cmd->error = 0; | |
83 | host->ops->request(host, mrq); | |
84 | } else if (mrq->done) { | |
85 | mrq->done(mrq); | |
86 | } | |
87 | } | |
88 | ||
89 | EXPORT_SYMBOL(mmc_request_done); | |
90 | ||
91 | /** | |
92 | * mmc_start_request - start a command on a host | |
93 | * @host: MMC host to start command on | |
94 | * @mrq: MMC request to start | |
95 | * | |
96 | * Queue a command on the specified host. We expect the | |
97 | * caller to be holding the host lock with interrupts disabled. | |
98 | */ | |
99 | void | |
100 | mmc_start_request(struct mmc_host *host, struct mmc_request *mrq) | |
101 | { | |
920e70c5 RK |
102 | pr_debug("%s: starting CMD%u arg %08x flags %08x\n", |
103 | mmc_hostname(host), mrq->cmd->opcode, | |
104 | mrq->cmd->arg, mrq->cmd->flags); | |
1da177e4 | 105 | |
f22ee4ed | 106 | WARN_ON(!host->claimed); |
1da177e4 LT |
107 | |
108 | mrq->cmd->error = 0; | |
109 | mrq->cmd->mrq = mrq; | |
110 | if (mrq->data) { | |
fe4a3c7a | 111 | BUG_ON(mrq->data->blksz > host->max_blk_size); |
55db890a PO |
112 | BUG_ON(mrq->data->blocks > host->max_blk_count); |
113 | BUG_ON(mrq->data->blocks * mrq->data->blksz > | |
114 | host->max_req_size); | |
fe4a3c7a | 115 | |
1da177e4 LT |
116 | mrq->cmd->data = mrq->data; |
117 | mrq->data->error = 0; | |
118 | mrq->data->mrq = mrq; | |
119 | if (mrq->stop) { | |
120 | mrq->data->stop = mrq->stop; | |
121 | mrq->stop->error = 0; | |
122 | mrq->stop->mrq = mrq; | |
123 | } | |
124 | } | |
125 | host->ops->request(host, mrq); | |
126 | } | |
127 | ||
128 | EXPORT_SYMBOL(mmc_start_request); | |
129 | ||
130 | static void mmc_wait_done(struct mmc_request *mrq) | |
131 | { | |
132 | complete(mrq->done_data); | |
133 | } | |
134 | ||
135 | int mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq) | |
136 | { | |
0afffc72 | 137 | DECLARE_COMPLETION_ONSTACK(complete); |
1da177e4 LT |
138 | |
139 | mrq->done_data = &complete; | |
140 | mrq->done = mmc_wait_done; | |
141 | ||
142 | mmc_start_request(host, mrq); | |
143 | ||
144 | wait_for_completion(&complete); | |
145 | ||
146 | return 0; | |
147 | } | |
148 | ||
149 | EXPORT_SYMBOL(mmc_wait_for_req); | |
150 | ||
151 | /** | |
152 | * mmc_wait_for_cmd - start a command and wait for completion | |
153 | * @host: MMC host to start command | |
154 | * @cmd: MMC command to start | |
155 | * @retries: maximum number of retries | |
156 | * | |
157 | * Start a new MMC command for a host, and wait for the command | |
158 | * to complete. Return any error that occurred while the command | |
159 | * was executing. Do not attempt to parse the response. | |
160 | */ | |
161 | int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries) | |
162 | { | |
163 | struct mmc_request mrq; | |
164 | ||
f22ee4ed | 165 | BUG_ON(!host->claimed); |
1da177e4 LT |
166 | |
167 | memset(&mrq, 0, sizeof(struct mmc_request)); | |
168 | ||
169 | memset(cmd->resp, 0, sizeof(cmd->resp)); | |
170 | cmd->retries = retries; | |
171 | ||
172 | mrq.cmd = cmd; | |
173 | cmd->data = NULL; | |
174 | ||
175 | mmc_wait_for_req(host, &mrq); | |
176 | ||
177 | return cmd->error; | |
178 | } | |
179 | ||
180 | EXPORT_SYMBOL(mmc_wait_for_cmd); | |
181 | ||
335eadf2 PO |
182 | /** |
183 | * mmc_wait_for_app_cmd - start an application command and wait for | |
184 | completion | |
185 | * @host: MMC host to start command | |
186 | * @rca: RCA to send MMC_APP_CMD to | |
187 | * @cmd: MMC command to start | |
188 | * @retries: maximum number of retries | |
189 | * | |
190 | * Sends a MMC_APP_CMD, checks the card response, sends the command | |
191 | * in the parameter and waits for it to complete. Return any error | |
192 | * that occurred while the command was executing. Do not attempt to | |
193 | * parse the response. | |
194 | */ | |
195 | int mmc_wait_for_app_cmd(struct mmc_host *host, unsigned int rca, | |
196 | struct mmc_command *cmd, int retries) | |
197 | { | |
198 | struct mmc_request mrq; | |
199 | struct mmc_command appcmd; | |
200 | ||
201 | int i, err; | |
202 | ||
f22ee4ed | 203 | BUG_ON(!host->claimed); |
335eadf2 PO |
204 | BUG_ON(retries < 0); |
205 | ||
206 | err = MMC_ERR_INVALID; | |
207 | ||
208 | /* | |
209 | * We have to resend MMC_APP_CMD for each attempt so | |
210 | * we cannot use the retries field in mmc_command. | |
211 | */ | |
212 | for (i = 0;i <= retries;i++) { | |
213 | memset(&mrq, 0, sizeof(struct mmc_request)); | |
214 | ||
215 | appcmd.opcode = MMC_APP_CMD; | |
216 | appcmd.arg = rca << 16; | |
e9225176 | 217 | appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
335eadf2 PO |
218 | appcmd.retries = 0; |
219 | memset(appcmd.resp, 0, sizeof(appcmd.resp)); | |
220 | appcmd.data = NULL; | |
221 | ||
222 | mrq.cmd = &appcmd; | |
223 | appcmd.data = NULL; | |
224 | ||
225 | mmc_wait_for_req(host, &mrq); | |
226 | ||
227 | if (appcmd.error) { | |
228 | err = appcmd.error; | |
229 | continue; | |
230 | } | |
231 | ||
232 | /* Check that card supported application commands */ | |
233 | if (!(appcmd.resp[0] & R1_APP_CMD)) | |
234 | return MMC_ERR_FAILED; | |
235 | ||
236 | memset(&mrq, 0, sizeof(struct mmc_request)); | |
237 | ||
238 | memset(cmd->resp, 0, sizeof(cmd->resp)); | |
239 | cmd->retries = 0; | |
240 | ||
241 | mrq.cmd = cmd; | |
242 | cmd->data = NULL; | |
1da177e4 | 243 | |
335eadf2 PO |
244 | mmc_wait_for_req(host, &mrq); |
245 | ||
246 | err = cmd->error; | |
247 | if (cmd->error == MMC_ERR_NONE) | |
248 | break; | |
249 | } | |
250 | ||
251 | return err; | |
252 | } | |
253 | ||
254 | EXPORT_SYMBOL(mmc_wait_for_app_cmd); | |
1da177e4 | 255 | |
d773d725 RK |
256 | /** |
257 | * mmc_set_data_timeout - set the timeout for a data command | |
258 | * @data: data phase for command | |
259 | * @card: the MMC card associated with the data transfer | |
260 | * @write: flag to differentiate reads from writes | |
261 | */ | |
262 | void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card, | |
263 | int write) | |
264 | { | |
265 | unsigned int mult; | |
266 | ||
267 | /* | |
268 | * SD cards use a 100 multiplier rather than 10 | |
269 | */ | |
270 | mult = mmc_card_sd(card) ? 100 : 10; | |
271 | ||
272 | /* | |
273 | * Scale up the multiplier (and therefore the timeout) by | |
274 | * the r2w factor for writes. | |
275 | */ | |
276 | if (write) | |
277 | mult <<= card->csd.r2w_factor; | |
278 | ||
279 | data->timeout_ns = card->csd.tacc_ns * mult; | |
280 | data->timeout_clks = card->csd.tacc_clks * mult; | |
281 | ||
282 | /* | |
283 | * SD cards also have an upper limit on the timeout. | |
284 | */ | |
285 | if (mmc_card_sd(card)) { | |
286 | unsigned int timeout_us, limit_us; | |
287 | ||
288 | timeout_us = data->timeout_ns / 1000; | |
289 | timeout_us += data->timeout_clks * 1000 / | |
290 | (card->host->ios.clock / 1000); | |
291 | ||
292 | if (write) | |
293 | limit_us = 250000; | |
294 | else | |
295 | limit_us = 100000; | |
296 | ||
fba68bd2 PL |
297 | /* |
298 | * SDHC cards always use these fixed values. | |
299 | */ | |
300 | if (timeout_us > limit_us || mmc_card_blockaddr(card)) { | |
d773d725 RK |
301 | data->timeout_ns = limit_us * 1000; |
302 | data->timeout_clks = 0; | |
303 | } | |
304 | } | |
305 | } | |
306 | EXPORT_SYMBOL(mmc_set_data_timeout); | |
307 | ||
b57c43ad PO |
308 | static int mmc_select_card(struct mmc_host *host, struct mmc_card *card); |
309 | ||
1da177e4 LT |
310 | /** |
311 | * __mmc_claim_host - exclusively claim a host | |
312 | * @host: mmc host to claim | |
313 | * @card: mmc card to claim host for | |
314 | * | |
315 | * Claim a host for a set of operations. If a valid card | |
316 | * is passed and this wasn't the last card selected, select | |
317 | * the card before returning. | |
318 | * | |
319 | * Note: you should use mmc_card_claim_host or mmc_claim_host. | |
320 | */ | |
321 | int __mmc_claim_host(struct mmc_host *host, struct mmc_card *card) | |
322 | { | |
323 | DECLARE_WAITQUEUE(wait, current); | |
324 | unsigned long flags; | |
325 | int err = 0; | |
326 | ||
327 | add_wait_queue(&host->wq, &wait); | |
328 | spin_lock_irqsave(&host->lock, flags); | |
329 | while (1) { | |
330 | set_current_state(TASK_UNINTERRUPTIBLE); | |
f22ee4ed | 331 | if (!host->claimed) |
1da177e4 LT |
332 | break; |
333 | spin_unlock_irqrestore(&host->lock, flags); | |
334 | schedule(); | |
335 | spin_lock_irqsave(&host->lock, flags); | |
336 | } | |
337 | set_current_state(TASK_RUNNING); | |
f22ee4ed | 338 | host->claimed = 1; |
1da177e4 LT |
339 | spin_unlock_irqrestore(&host->lock, flags); |
340 | remove_wait_queue(&host->wq, &wait); | |
341 | ||
b57c43ad PO |
342 | if (card != (void *)-1) { |
343 | err = mmc_select_card(host, card); | |
344 | if (err != MMC_ERR_NONE) | |
345 | return err; | |
1da177e4 LT |
346 | } |
347 | ||
348 | return err; | |
349 | } | |
350 | ||
351 | EXPORT_SYMBOL(__mmc_claim_host); | |
352 | ||
353 | /** | |
354 | * mmc_release_host - release a host | |
355 | * @host: mmc host to release | |
356 | * | |
357 | * Release a MMC host, allowing others to claim the host | |
358 | * for their operations. | |
359 | */ | |
360 | void mmc_release_host(struct mmc_host *host) | |
361 | { | |
362 | unsigned long flags; | |
363 | ||
f22ee4ed | 364 | BUG_ON(!host->claimed); |
1da177e4 LT |
365 | |
366 | spin_lock_irqsave(&host->lock, flags); | |
f22ee4ed | 367 | host->claimed = 0; |
1da177e4 LT |
368 | spin_unlock_irqrestore(&host->lock, flags); |
369 | ||
370 | wake_up(&host->wq); | |
371 | } | |
372 | ||
373 | EXPORT_SYMBOL(mmc_release_host); | |
374 | ||
920e70c5 RK |
375 | static inline void mmc_set_ios(struct mmc_host *host) |
376 | { | |
377 | struct mmc_ios *ios = &host->ios; | |
378 | ||
cd9277c0 PO |
379 | pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u " |
380 | "width %u timing %u\n", | |
920e70c5 RK |
381 | mmc_hostname(host), ios->clock, ios->bus_mode, |
382 | ios->power_mode, ios->chip_select, ios->vdd, | |
cd9277c0 | 383 | ios->bus_width, ios->timing); |
fba68bd2 | 384 | |
920e70c5 RK |
385 | host->ops->set_ios(host, ios); |
386 | } | |
387 | ||
b57c43ad PO |
388 | static int mmc_select_card(struct mmc_host *host, struct mmc_card *card) |
389 | { | |
390 | int err; | |
391 | struct mmc_command cmd; | |
392 | ||
f22ee4ed | 393 | BUG_ON(!host->claimed); |
b57c43ad PO |
394 | |
395 | if (host->card_selected == card) | |
396 | return MMC_ERR_NONE; | |
397 | ||
398 | host->card_selected = card; | |
399 | ||
400 | cmd.opcode = MMC_SELECT_CARD; | |
401 | cmd.arg = card->rca << 16; | |
e9225176 | 402 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
b57c43ad PO |
403 | |
404 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); | |
405 | if (err != MMC_ERR_NONE) | |
406 | return err; | |
407 | ||
f218278a | 408 | /* |
e45a1bd2 PL |
409 | * We can only change the bus width of SD cards when |
410 | * they are selected so we have to put the handling | |
f218278a | 411 | * here. |
e45a1bd2 PL |
412 | * |
413 | * The card is in 1 bit mode by default so | |
414 | * we only need to change if it supports the | |
415 | * wider version. | |
f218278a | 416 | */ |
e45a1bd2 PL |
417 | if (mmc_card_sd(card) && |
418 | (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) { | |
419 | ||
f218278a | 420 | /* |
e45a1bd2 PL |
421 | * Default bus width is 1 bit. |
422 | */ | |
423 | host->ios.bus_width = MMC_BUS_WIDTH_1; | |
424 | ||
425 | if (host->caps & MMC_CAP_4_BIT_DATA) { | |
f218278a PO |
426 | struct mmc_command cmd; |
427 | cmd.opcode = SD_APP_SET_BUS_WIDTH; | |
428 | cmd.arg = SD_BUS_WIDTH_4; | |
e9225176 | 429 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
f218278a PO |
430 | |
431 | err = mmc_wait_for_app_cmd(host, card->rca, &cmd, | |
432 | CMD_RETRIES); | |
433 | if (err != MMC_ERR_NONE) | |
434 | return err; | |
435 | ||
436 | host->ios.bus_width = MMC_BUS_WIDTH_4; | |
437 | } | |
438 | } | |
439 | ||
920e70c5 | 440 | mmc_set_ios(host); |
f218278a | 441 | |
b57c43ad PO |
442 | return MMC_ERR_NONE; |
443 | } | |
444 | ||
1da177e4 LT |
445 | /* |
446 | * Ensure that no card is selected. | |
447 | */ | |
448 | static void mmc_deselect_cards(struct mmc_host *host) | |
449 | { | |
450 | struct mmc_command cmd; | |
451 | ||
452 | if (host->card_selected) { | |
453 | host->card_selected = NULL; | |
454 | ||
455 | cmd.opcode = MMC_SELECT_CARD; | |
456 | cmd.arg = 0; | |
e9225176 | 457 | cmd.flags = MMC_RSP_NONE | MMC_CMD_AC; |
1da177e4 LT |
458 | |
459 | mmc_wait_for_cmd(host, &cmd, 0); | |
460 | } | |
461 | } | |
462 | ||
463 | ||
464 | static inline void mmc_delay(unsigned int ms) | |
465 | { | |
73778120 PO |
466 | if (ms < 1000 / HZ) { |
467 | cond_resched(); | |
1da177e4 LT |
468 | mdelay(ms); |
469 | } else { | |
73778120 | 470 | msleep(ms); |
1da177e4 LT |
471 | } |
472 | } | |
473 | ||
474 | /* | |
475 | * Mask off any voltages we don't support and select | |
476 | * the lowest voltage | |
477 | */ | |
478 | static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr) | |
479 | { | |
480 | int bit; | |
481 | ||
482 | ocr &= host->ocr_avail; | |
483 | ||
484 | bit = ffs(ocr); | |
485 | if (bit) { | |
486 | bit -= 1; | |
487 | ||
63ef731a | 488 | ocr &= 3 << bit; |
1da177e4 LT |
489 | |
490 | host->ios.vdd = bit; | |
920e70c5 | 491 | mmc_set_ios(host); |
1da177e4 LT |
492 | } else { |
493 | ocr = 0; | |
494 | } | |
495 | ||
496 | return ocr; | |
497 | } | |
498 | ||
499 | #define UNSTUFF_BITS(resp,start,size) \ | |
500 | ({ \ | |
501 | const int __size = size; \ | |
502 | const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \ | |
503 | const int __off = 3 - ((start) / 32); \ | |
504 | const int __shft = (start) & 31; \ | |
505 | u32 __res; \ | |
506 | \ | |
507 | __res = resp[__off] >> __shft; \ | |
508 | if (__size + __shft > 32) \ | |
509 | __res |= resp[__off-1] << ((32 - __shft) % 32); \ | |
510 | __res & __mask; \ | |
511 | }) | |
512 | ||
513 | /* | |
514 | * Given the decoded CSD structure, decode the raw CID to our CID structure. | |
515 | */ | |
516 | static void mmc_decode_cid(struct mmc_card *card) | |
517 | { | |
518 | u32 *resp = card->raw_cid; | |
519 | ||
520 | memset(&card->cid, 0, sizeof(struct mmc_cid)); | |
521 | ||
335eadf2 PO |
522 | if (mmc_card_sd(card)) { |
523 | /* | |
524 | * SD doesn't currently have a version field so we will | |
525 | * have to assume we can parse this. | |
526 | */ | |
527 | card->cid.manfid = UNSTUFF_BITS(resp, 120, 8); | |
528 | card->cid.oemid = UNSTUFF_BITS(resp, 104, 16); | |
529 | card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); | |
530 | card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); | |
531 | card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); | |
532 | card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); | |
533 | card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); | |
534 | card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4); | |
535 | card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4); | |
536 | card->cid.serial = UNSTUFF_BITS(resp, 24, 32); | |
537 | card->cid.year = UNSTUFF_BITS(resp, 12, 8); | |
538 | card->cid.month = UNSTUFF_BITS(resp, 8, 4); | |
539 | ||
540 | card->cid.year += 2000; /* SD cards year offset */ | |
a00fc090 | 541 | } else { |
335eadf2 PO |
542 | /* |
543 | * The selection of the format here is based upon published | |
544 | * specs from sandisk and from what people have reported. | |
545 | */ | |
546 | switch (card->csd.mmca_vsn) { | |
547 | case 0: /* MMC v1.0 - v1.2 */ | |
548 | case 1: /* MMC v1.4 */ | |
549 | card->cid.manfid = UNSTUFF_BITS(resp, 104, 24); | |
550 | card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); | |
551 | card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); | |
552 | card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); | |
553 | card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); | |
554 | card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); | |
555 | card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8); | |
556 | card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8); | |
557 | card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4); | |
558 | card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4); | |
559 | card->cid.serial = UNSTUFF_BITS(resp, 16, 24); | |
560 | card->cid.month = UNSTUFF_BITS(resp, 12, 4); | |
561 | card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997; | |
562 | break; | |
563 | ||
564 | case 2: /* MMC v2.0 - v2.2 */ | |
565 | case 3: /* MMC v3.1 - v3.3 */ | |
cb757b4e | 566 | case 4: /* MMC v4 */ |
335eadf2 PO |
567 | card->cid.manfid = UNSTUFF_BITS(resp, 120, 8); |
568 | card->cid.oemid = UNSTUFF_BITS(resp, 104, 16); | |
569 | card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); | |
570 | card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); | |
571 | card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); | |
572 | card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); | |
573 | card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); | |
574 | card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8); | |
575 | card->cid.serial = UNSTUFF_BITS(resp, 16, 32); | |
576 | card->cid.month = UNSTUFF_BITS(resp, 12, 4); | |
577 | card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997; | |
578 | break; | |
579 | ||
580 | default: | |
581 | printk("%s: card has unknown MMCA version %d\n", | |
582 | mmc_hostname(card->host), card->csd.mmca_vsn); | |
583 | mmc_card_set_bad(card); | |
584 | break; | |
585 | } | |
1da177e4 LT |
586 | } |
587 | } | |
588 | ||
589 | /* | |
590 | * Given a 128-bit response, decode to our card CSD structure. | |
591 | */ | |
592 | static void mmc_decode_csd(struct mmc_card *card) | |
593 | { | |
594 | struct mmc_csd *csd = &card->csd; | |
595 | unsigned int e, m, csd_struct; | |
596 | u32 *resp = card->raw_csd; | |
597 | ||
335eadf2 PO |
598 | if (mmc_card_sd(card)) { |
599 | csd_struct = UNSTUFF_BITS(resp, 126, 2); | |
fba68bd2 PL |
600 | |
601 | switch (csd_struct) { | |
602 | case 0: | |
603 | m = UNSTUFF_BITS(resp, 115, 4); | |
604 | e = UNSTUFF_BITS(resp, 112, 3); | |
605 | csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10; | |
606 | csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100; | |
607 | ||
608 | m = UNSTUFF_BITS(resp, 99, 4); | |
609 | e = UNSTUFF_BITS(resp, 96, 3); | |
610 | csd->max_dtr = tran_exp[e] * tran_mant[m]; | |
611 | csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); | |
612 | ||
613 | e = UNSTUFF_BITS(resp, 47, 3); | |
614 | m = UNSTUFF_BITS(resp, 62, 12); | |
615 | csd->capacity = (1 + m) << (e + 2); | |
616 | ||
617 | csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); | |
618 | csd->read_partial = UNSTUFF_BITS(resp, 79, 1); | |
619 | csd->write_misalign = UNSTUFF_BITS(resp, 78, 1); | |
620 | csd->read_misalign = UNSTUFF_BITS(resp, 77, 1); | |
621 | csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3); | |
622 | csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); | |
623 | csd->write_partial = UNSTUFF_BITS(resp, 21, 1); | |
624 | break; | |
625 | case 1: | |
626 | /* | |
627 | * This is a block-addressed SDHC card. Most | |
628 | * interesting fields are unused and have fixed | |
629 | * values. To avoid getting tripped by buggy cards, | |
630 | * we assume those fixed values ourselves. | |
631 | */ | |
632 | mmc_card_set_blockaddr(card); | |
633 | ||
634 | csd->tacc_ns = 0; /* Unused */ | |
635 | csd->tacc_clks = 0; /* Unused */ | |
636 | ||
637 | m = UNSTUFF_BITS(resp, 99, 4); | |
638 | e = UNSTUFF_BITS(resp, 96, 3); | |
639 | csd->max_dtr = tran_exp[e] * tran_mant[m]; | |
640 | csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); | |
641 | ||
642 | m = UNSTUFF_BITS(resp, 48, 22); | |
643 | csd->capacity = (1 + m) << 10; | |
644 | ||
645 | csd->read_blkbits = 9; | |
646 | csd->read_partial = 0; | |
647 | csd->write_misalign = 0; | |
648 | csd->read_misalign = 0; | |
649 | csd->r2w_factor = 4; /* Unused */ | |
650 | csd->write_blkbits = 9; | |
651 | csd->write_partial = 0; | |
652 | break; | |
653 | default: | |
335eadf2 PO |
654 | printk("%s: unrecognised CSD structure version %d\n", |
655 | mmc_hostname(card->host), csd_struct); | |
656 | mmc_card_set_bad(card); | |
657 | return; | |
658 | } | |
a00fc090 | 659 | } else { |
335eadf2 PO |
660 | /* |
661 | * We only understand CSD structure v1.1 and v1.2. | |
662 | * v1.2 has extra information in bits 15, 11 and 10. | |
663 | */ | |
664 | csd_struct = UNSTUFF_BITS(resp, 126, 2); | |
665 | if (csd_struct != 1 && csd_struct != 2) { | |
666 | printk("%s: unrecognised CSD structure version %d\n", | |
667 | mmc_hostname(card->host), csd_struct); | |
668 | mmc_card_set_bad(card); | |
669 | return; | |
670 | } | |
1da177e4 | 671 | |
335eadf2 PO |
672 | csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4); |
673 | m = UNSTUFF_BITS(resp, 115, 4); | |
674 | e = UNSTUFF_BITS(resp, 112, 3); | |
675 | csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10; | |
676 | csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100; | |
1da177e4 | 677 | |
335eadf2 PO |
678 | m = UNSTUFF_BITS(resp, 99, 4); |
679 | e = UNSTUFF_BITS(resp, 96, 3); | |
680 | csd->max_dtr = tran_exp[e] * tran_mant[m]; | |
681 | csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); | |
1da177e4 | 682 | |
335eadf2 PO |
683 | e = UNSTUFF_BITS(resp, 47, 3); |
684 | m = UNSTUFF_BITS(resp, 62, 12); | |
685 | csd->capacity = (1 + m) << (e + 2); | |
1da177e4 | 686 | |
335eadf2 | 687 | csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); |
a6f6c96b RK |
688 | csd->read_partial = UNSTUFF_BITS(resp, 79, 1); |
689 | csd->write_misalign = UNSTUFF_BITS(resp, 78, 1); | |
690 | csd->read_misalign = UNSTUFF_BITS(resp, 77, 1); | |
37be4e78 | 691 | csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3); |
a6f6c96b RK |
692 | csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); |
693 | csd->write_partial = UNSTUFF_BITS(resp, 21, 1); | |
335eadf2 | 694 | } |
1da177e4 LT |
695 | } |
696 | ||
b57c43ad PO |
697 | /* |
698 | * Given a 64-bit response, decode to our card SCR structure. | |
699 | */ | |
700 | static void mmc_decode_scr(struct mmc_card *card) | |
701 | { | |
702 | struct sd_scr *scr = &card->scr; | |
703 | unsigned int scr_struct; | |
704 | u32 resp[4]; | |
705 | ||
706 | BUG_ON(!mmc_card_sd(card)); | |
707 | ||
708 | resp[3] = card->raw_scr[1]; | |
709 | resp[2] = card->raw_scr[0]; | |
710 | ||
711 | scr_struct = UNSTUFF_BITS(resp, 60, 4); | |
712 | if (scr_struct != 0) { | |
713 | printk("%s: unrecognised SCR structure version %d\n", | |
714 | mmc_hostname(card->host), scr_struct); | |
715 | mmc_card_set_bad(card); | |
716 | return; | |
717 | } | |
718 | ||
719 | scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4); | |
720 | scr->bus_widths = UNSTUFF_BITS(resp, 48, 4); | |
721 | } | |
722 | ||
1da177e4 LT |
723 | /* |
724 | * Locate a MMC card on this MMC host given a raw CID. | |
725 | */ | |
726 | static struct mmc_card *mmc_find_card(struct mmc_host *host, u32 *raw_cid) | |
727 | { | |
728 | struct mmc_card *card; | |
729 | ||
730 | list_for_each_entry(card, &host->cards, node) { | |
731 | if (memcmp(card->raw_cid, raw_cid, sizeof(card->raw_cid)) == 0) | |
732 | return card; | |
733 | } | |
734 | return NULL; | |
735 | } | |
736 | ||
737 | /* | |
738 | * Allocate a new MMC card, and assign a unique RCA. | |
739 | */ | |
740 | static struct mmc_card * | |
741 | mmc_alloc_card(struct mmc_host *host, u32 *raw_cid, unsigned int *frca) | |
742 | { | |
743 | struct mmc_card *card, *c; | |
744 | unsigned int rca = *frca; | |
745 | ||
746 | card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL); | |
747 | if (!card) | |
748 | return ERR_PTR(-ENOMEM); | |
749 | ||
750 | mmc_init_card(card, host); | |
751 | memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid)); | |
752 | ||
753 | again: | |
754 | list_for_each_entry(c, &host->cards, node) | |
755 | if (c->rca == rca) { | |
756 | rca++; | |
757 | goto again; | |
758 | } | |
759 | ||
760 | card->rca = rca; | |
761 | ||
762 | *frca = rca; | |
763 | ||
764 | return card; | |
765 | } | |
766 | ||
767 | /* | |
768 | * Tell attached cards to go to IDLE state | |
769 | */ | |
770 | static void mmc_idle_cards(struct mmc_host *host) | |
771 | { | |
772 | struct mmc_command cmd; | |
773 | ||
865e9f13 | 774 | host->ios.chip_select = MMC_CS_HIGH; |
920e70c5 | 775 | mmc_set_ios(host); |
865e9f13 PO |
776 | |
777 | mmc_delay(1); | |
778 | ||
1da177e4 LT |
779 | cmd.opcode = MMC_GO_IDLE_STATE; |
780 | cmd.arg = 0; | |
e9225176 | 781 | cmd.flags = MMC_RSP_NONE | MMC_CMD_BC; |
1da177e4 LT |
782 | |
783 | mmc_wait_for_cmd(host, &cmd, 0); | |
784 | ||
785 | mmc_delay(1); | |
865e9f13 PO |
786 | |
787 | host->ios.chip_select = MMC_CS_DONTCARE; | |
920e70c5 | 788 | mmc_set_ios(host); |
865e9f13 PO |
789 | |
790 | mmc_delay(1); | |
1da177e4 LT |
791 | } |
792 | ||
793 | /* | |
45f8245b RK |
794 | * Apply power to the MMC stack. This is a two-stage process. |
795 | * First, we enable power to the card without the clock running. | |
796 | * We then wait a bit for the power to stabilise. Finally, | |
797 | * enable the bus drivers and clock to the card. | |
798 | * | |
799 | * We must _NOT_ enable the clock prior to power stablising. | |
800 | * | |
801 | * If a host does all the power sequencing itself, ignore the | |
802 | * initial MMC_POWER_UP stage. | |
1da177e4 LT |
803 | */ |
804 | static void mmc_power_up(struct mmc_host *host) | |
805 | { | |
806 | int bit = fls(host->ocr_avail) - 1; | |
807 | ||
808 | host->ios.vdd = bit; | |
809 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; | |
865e9f13 | 810 | host->ios.chip_select = MMC_CS_DONTCARE; |
1da177e4 | 811 | host->ios.power_mode = MMC_POWER_UP; |
f218278a | 812 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
cd9277c0 | 813 | host->ios.timing = MMC_TIMING_LEGACY; |
920e70c5 | 814 | mmc_set_ios(host); |
1da177e4 LT |
815 | |
816 | mmc_delay(1); | |
817 | ||
818 | host->ios.clock = host->f_min; | |
819 | host->ios.power_mode = MMC_POWER_ON; | |
920e70c5 | 820 | mmc_set_ios(host); |
1da177e4 LT |
821 | |
822 | mmc_delay(2); | |
823 | } | |
824 | ||
825 | static void mmc_power_off(struct mmc_host *host) | |
826 | { | |
827 | host->ios.clock = 0; | |
828 | host->ios.vdd = 0; | |
829 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; | |
865e9f13 | 830 | host->ios.chip_select = MMC_CS_DONTCARE; |
1da177e4 | 831 | host->ios.power_mode = MMC_POWER_OFF; |
f218278a | 832 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
cd9277c0 | 833 | host->ios.timing = MMC_TIMING_LEGACY; |
920e70c5 | 834 | mmc_set_ios(host); |
1da177e4 LT |
835 | } |
836 | ||
837 | static int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr) | |
838 | { | |
839 | struct mmc_command cmd; | |
840 | int i, err = 0; | |
841 | ||
842 | cmd.opcode = MMC_SEND_OP_COND; | |
843 | cmd.arg = ocr; | |
e9225176 | 844 | cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; |
1da177e4 LT |
845 | |
846 | for (i = 100; i; i--) { | |
847 | err = mmc_wait_for_cmd(host, &cmd, 0); | |
848 | if (err != MMC_ERR_NONE) | |
849 | break; | |
850 | ||
851 | if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0) | |
852 | break; | |
853 | ||
854 | err = MMC_ERR_TIMEOUT; | |
855 | ||
856 | mmc_delay(10); | |
857 | } | |
858 | ||
859 | if (rocr) | |
860 | *rocr = cmd.resp[0]; | |
861 | ||
862 | return err; | |
863 | } | |
864 | ||
335eadf2 PO |
865 | static int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr) |
866 | { | |
867 | struct mmc_command cmd; | |
868 | int i, err = 0; | |
869 | ||
870 | cmd.opcode = SD_APP_OP_COND; | |
871 | cmd.arg = ocr; | |
e9225176 | 872 | cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; |
335eadf2 PO |
873 | |
874 | for (i = 100; i; i--) { | |
875 | err = mmc_wait_for_app_cmd(host, 0, &cmd, CMD_RETRIES); | |
876 | if (err != MMC_ERR_NONE) | |
877 | break; | |
878 | ||
879 | if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0) | |
880 | break; | |
881 | ||
882 | err = MMC_ERR_TIMEOUT; | |
883 | ||
884 | mmc_delay(10); | |
885 | } | |
886 | ||
887 | if (rocr) | |
888 | *rocr = cmd.resp[0]; | |
889 | ||
890 | return err; | |
891 | } | |
892 | ||
fba68bd2 PL |
893 | static int mmc_send_if_cond(struct mmc_host *host, u32 ocr, int *rsd2) |
894 | { | |
895 | struct mmc_command cmd; | |
896 | int err, sd2; | |
897 | static const u8 test_pattern = 0xAA; | |
898 | ||
899 | /* | |
900 | * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND | |
901 | * before SD_APP_OP_COND. This command will harmlessly fail for | |
902 | * SD 1.0 cards. | |
903 | */ | |
904 | cmd.opcode = SD_SEND_IF_COND; | |
905 | cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | test_pattern; | |
906 | cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR; | |
907 | ||
908 | err = mmc_wait_for_cmd(host, &cmd, 0); | |
909 | if (err == MMC_ERR_NONE) { | |
910 | if ((cmd.resp[0] & 0xFF) == test_pattern) { | |
911 | sd2 = 1; | |
912 | } else { | |
913 | sd2 = 0; | |
914 | err = MMC_ERR_FAILED; | |
915 | } | |
916 | } else { | |
917 | /* | |
918 | * Treat errors as SD 1.0 card. | |
919 | */ | |
920 | sd2 = 0; | |
921 | err = MMC_ERR_NONE; | |
922 | } | |
923 | if (rsd2) | |
924 | *rsd2 = sd2; | |
925 | return err; | |
926 | } | |
927 | ||
1da177e4 LT |
928 | /* |
929 | * Discover cards by requesting their CID. If this command | |
930 | * times out, it is not an error; there are no further cards | |
931 | * to be discovered. Add new cards to the list. | |
932 | * | |
933 | * Create a mmc_card entry for each discovered card, assigning | |
934 | * it an RCA, and save the raw CID for decoding later. | |
935 | */ | |
936 | static void mmc_discover_cards(struct mmc_host *host) | |
937 | { | |
938 | struct mmc_card *card; | |
939 | unsigned int first_rca = 1, err; | |
940 | ||
941 | while (1) { | |
942 | struct mmc_command cmd; | |
943 | ||
944 | cmd.opcode = MMC_ALL_SEND_CID; | |
945 | cmd.arg = 0; | |
e9225176 | 946 | cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; |
1da177e4 LT |
947 | |
948 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); | |
949 | if (err == MMC_ERR_TIMEOUT) { | |
950 | err = MMC_ERR_NONE; | |
951 | break; | |
952 | } | |
953 | if (err != MMC_ERR_NONE) { | |
954 | printk(KERN_ERR "%s: error requesting CID: %d\n", | |
d366b643 | 955 | mmc_hostname(host), err); |
1da177e4 LT |
956 | break; |
957 | } | |
958 | ||
959 | card = mmc_find_card(host, cmd.resp); | |
960 | if (!card) { | |
961 | card = mmc_alloc_card(host, cmd.resp, &first_rca); | |
962 | if (IS_ERR(card)) { | |
963 | err = PTR_ERR(card); | |
964 | break; | |
965 | } | |
966 | list_add(&card->node, &host->cards); | |
967 | } | |
968 | ||
969 | card->state &= ~MMC_STATE_DEAD; | |
970 | ||
335eadf2 PO |
971 | if (host->mode == MMC_MODE_SD) { |
972 | mmc_card_set_sd(card); | |
1da177e4 | 973 | |
335eadf2 PO |
974 | cmd.opcode = SD_SEND_RELATIVE_ADDR; |
975 | cmd.arg = 0; | |
e9225176 | 976 | cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR; |
335eadf2 PO |
977 | |
978 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); | |
979 | if (err != MMC_ERR_NONE) | |
980 | mmc_card_set_dead(card); | |
a00fc090 | 981 | else { |
335eadf2 | 982 | card->rca = cmd.resp[0] >> 16; |
a00fc090 PO |
983 | |
984 | if (!host->ops->get_ro) { | |
985 | printk(KERN_WARNING "%s: host does not " | |
986 | "support reading read-only " | |
987 | "switch. assuming write-enable.\n", | |
988 | mmc_hostname(host)); | |
989 | } else { | |
990 | if (host->ops->get_ro(host)) | |
991 | mmc_card_set_readonly(card); | |
992 | } | |
993 | } | |
994 | } else { | |
335eadf2 PO |
995 | cmd.opcode = MMC_SET_RELATIVE_ADDR; |
996 | cmd.arg = card->rca << 16; | |
e9225176 | 997 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
335eadf2 PO |
998 | |
999 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); | |
1000 | if (err != MMC_ERR_NONE) | |
1001 | mmc_card_set_dead(card); | |
1002 | } | |
1da177e4 LT |
1003 | } |
1004 | } | |
1005 | ||
1006 | static void mmc_read_csds(struct mmc_host *host) | |
1007 | { | |
1008 | struct mmc_card *card; | |
1009 | ||
1010 | list_for_each_entry(card, &host->cards, node) { | |
1011 | struct mmc_command cmd; | |
1012 | int err; | |
1013 | ||
1014 | if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT)) | |
1015 | continue; | |
1016 | ||
1017 | cmd.opcode = MMC_SEND_CSD; | |
1018 | cmd.arg = card->rca << 16; | |
e9225176 | 1019 | cmd.flags = MMC_RSP_R2 | MMC_CMD_AC; |
1da177e4 LT |
1020 | |
1021 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); | |
1022 | if (err != MMC_ERR_NONE) { | |
1023 | mmc_card_set_dead(card); | |
1024 | continue; | |
1025 | } | |
1026 | ||
1027 | memcpy(card->raw_csd, cmd.resp, sizeof(card->raw_csd)); | |
1028 | ||
1029 | mmc_decode_csd(card); | |
1030 | mmc_decode_cid(card); | |
1031 | } | |
1032 | } | |
1033 | ||
bce40a36 PL |
1034 | static void mmc_process_ext_csds(struct mmc_host *host) |
1035 | { | |
1036 | int err; | |
1037 | struct mmc_card *card; | |
1038 | ||
1039 | struct mmc_request mrq; | |
1040 | struct mmc_command cmd; | |
1041 | struct mmc_data data; | |
1042 | ||
1043 | struct scatterlist sg; | |
1044 | ||
1045 | /* | |
1046 | * As the ext_csd is so large and mostly unused, we don't store the | |
1047 | * raw block in mmc_card. | |
1048 | */ | |
1049 | u8 *ext_csd; | |
1050 | ext_csd = kmalloc(512, GFP_KERNEL); | |
1051 | if (!ext_csd) { | |
1052 | printk("%s: could not allocate a buffer to receive the ext_csd." | |
1053 | "mmc v4 cards will be treated as v3.\n", | |
1054 | mmc_hostname(host)); | |
1055 | return; | |
1056 | } | |
1057 | ||
1058 | list_for_each_entry(card, &host->cards, node) { | |
1059 | if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT)) | |
1060 | continue; | |
1061 | if (mmc_card_sd(card)) | |
1062 | continue; | |
1063 | if (card->csd.mmca_vsn < CSD_SPEC_VER_4) | |
1064 | continue; | |
1065 | ||
1066 | err = mmc_select_card(host, card); | |
1067 | if (err != MMC_ERR_NONE) { | |
1068 | mmc_card_set_dead(card); | |
1069 | continue; | |
1070 | } | |
1071 | ||
1072 | memset(&cmd, 0, sizeof(struct mmc_command)); | |
1073 | ||
1074 | cmd.opcode = MMC_SEND_EXT_CSD; | |
1075 | cmd.arg = 0; | |
1076 | cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; | |
1077 | ||
1078 | memset(&data, 0, sizeof(struct mmc_data)); | |
1079 | ||
1080 | mmc_set_data_timeout(&data, card, 0); | |
1081 | ||
1082 | data.blksz = 512; | |
1083 | data.blocks = 1; | |
1084 | data.flags = MMC_DATA_READ; | |
1085 | data.sg = &sg; | |
1086 | data.sg_len = 1; | |
1087 | ||
1088 | memset(&mrq, 0, sizeof(struct mmc_request)); | |
1089 | ||
1090 | mrq.cmd = &cmd; | |
1091 | mrq.data = &data; | |
1092 | ||
1093 | sg_init_one(&sg, ext_csd, 512); | |
1094 | ||
1095 | mmc_wait_for_req(host, &mrq); | |
1096 | ||
1097 | if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) { | |
ae06eaf9 PO |
1098 | printk("%s: unable to read EXT_CSD, performance " |
1099 | "might suffer.\n", mmc_hostname(card->host)); | |
bce40a36 PL |
1100 | continue; |
1101 | } | |
1102 | ||
1103 | switch (ext_csd[EXT_CSD_CARD_TYPE]) { | |
1104 | case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26: | |
1105 | card->ext_csd.hs_max_dtr = 52000000; | |
1106 | break; | |
1107 | case EXT_CSD_CARD_TYPE_26: | |
1108 | card->ext_csd.hs_max_dtr = 26000000; | |
1109 | break; | |
1110 | default: | |
1111 | /* MMC v4 spec says this cannot happen */ | |
1112 | printk("%s: card is mmc v4 but doesn't support " | |
1113 | "any high-speed modes.\n", | |
1114 | mmc_hostname(card->host)); | |
bce40a36 PL |
1115 | continue; |
1116 | } | |
1117 | ||
cd9277c0 PO |
1118 | if (host->caps & MMC_CAP_MMC_HIGHSPEED) { |
1119 | /* Activate highspeed support. */ | |
1120 | cmd.opcode = MMC_SWITCH; | |
1121 | cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | | |
1122 | (EXT_CSD_HS_TIMING << 16) | | |
1123 | (1 << 8) | | |
1124 | EXT_CSD_CMD_SET_NORMAL; | |
1125 | cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; | |
bce40a36 | 1126 | |
cd9277c0 PO |
1127 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); |
1128 | if (err != MMC_ERR_NONE) { | |
1129 | printk("%s: failed to switch card to mmc v4 " | |
1130 | "high-speed mode.\n", | |
1131 | mmc_hostname(card->host)); | |
1132 | continue; | |
1133 | } | |
bce40a36 | 1134 | |
cd9277c0 | 1135 | mmc_card_set_highspeed(card); |
e45a1bd2 | 1136 | |
cd9277c0 PO |
1137 | host->ios.timing = MMC_TIMING_SD_HS; |
1138 | mmc_set_ios(host); | |
e45a1bd2 PL |
1139 | } |
1140 | ||
cd9277c0 PO |
1141 | /* Check for host support for wide-bus modes. */ |
1142 | if (host->caps & MMC_CAP_4_BIT_DATA) { | |
1143 | /* Activate 4-bit support. */ | |
1144 | cmd.opcode = MMC_SWITCH; | |
1145 | cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | | |
1146 | (EXT_CSD_BUS_WIDTH << 16) | | |
1147 | (EXT_CSD_BUS_WIDTH_4 << 8) | | |
1148 | EXT_CSD_CMD_SET_NORMAL; | |
1149 | cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; | |
e45a1bd2 | 1150 | |
cd9277c0 PO |
1151 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); |
1152 | if (err != MMC_ERR_NONE) { | |
1153 | printk("%s: failed to switch card to " | |
1154 | "mmc v4 4-bit bus mode.\n", | |
1155 | mmc_hostname(card->host)); | |
1156 | continue; | |
1157 | } | |
e45a1bd2 | 1158 | |
cd9277c0 PO |
1159 | host->ios.bus_width = MMC_BUS_WIDTH_4; |
1160 | mmc_set_ios(host); | |
1161 | } | |
bce40a36 PL |
1162 | } |
1163 | ||
1164 | kfree(ext_csd); | |
1165 | ||
1166 | mmc_deselect_cards(host); | |
1167 | } | |
1168 | ||
b57c43ad PO |
1169 | static void mmc_read_scrs(struct mmc_host *host) |
1170 | { | |
1171 | int err; | |
1172 | struct mmc_card *card; | |
b57c43ad PO |
1173 | struct mmc_request mrq; |
1174 | struct mmc_command cmd; | |
1175 | struct mmc_data data; | |
b57c43ad PO |
1176 | struct scatterlist sg; |
1177 | ||
1178 | list_for_each_entry(card, &host->cards, node) { | |
1179 | if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT)) | |
1180 | continue; | |
1181 | if (!mmc_card_sd(card)) | |
1182 | continue; | |
1183 | ||
1184 | err = mmc_select_card(host, card); | |
1185 | if (err != MMC_ERR_NONE) { | |
1186 | mmc_card_set_dead(card); | |
1187 | continue; | |
1188 | } | |
1189 | ||
1190 | memset(&cmd, 0, sizeof(struct mmc_command)); | |
1191 | ||
1192 | cmd.opcode = MMC_APP_CMD; | |
1193 | cmd.arg = card->rca << 16; | |
e9225176 | 1194 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
b57c43ad PO |
1195 | |
1196 | err = mmc_wait_for_cmd(host, &cmd, 0); | |
1197 | if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD)) { | |
1198 | mmc_card_set_dead(card); | |
1199 | continue; | |
1200 | } | |
1201 | ||
1202 | memset(&cmd, 0, sizeof(struct mmc_command)); | |
1203 | ||
1204 | cmd.opcode = SD_APP_SEND_SCR; | |
1205 | cmd.arg = 0; | |
e9225176 | 1206 | cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; |
b57c43ad PO |
1207 | |
1208 | memset(&data, 0, sizeof(struct mmc_data)); | |
1209 | ||
d773d725 | 1210 | mmc_set_data_timeout(&data, card, 0); |
385e3227 | 1211 | |
2c171bf1 | 1212 | data.blksz = 1 << 3; |
b57c43ad PO |
1213 | data.blocks = 1; |
1214 | data.flags = MMC_DATA_READ; | |
1215 | data.sg = &sg; | |
1216 | data.sg_len = 1; | |
1217 | ||
1218 | memset(&mrq, 0, sizeof(struct mmc_request)); | |
1219 | ||
1220 | mrq.cmd = &cmd; | |
1221 | mrq.data = &data; | |
1222 | ||
1223 | sg_init_one(&sg, (u8*)card->raw_scr, 8); | |
1224 | ||
e781de44 PO |
1225 | mmc_wait_for_req(host, &mrq); |
1226 | ||
1227 | if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) { | |
b57c43ad PO |
1228 | mmc_card_set_dead(card); |
1229 | continue; | |
1230 | } | |
1231 | ||
1232 | card->raw_scr[0] = ntohl(card->raw_scr[0]); | |
1233 | card->raw_scr[1] = ntohl(card->raw_scr[1]); | |
1234 | ||
1235 | mmc_decode_scr(card); | |
1236 | } | |
1237 | ||
1238 | mmc_deselect_cards(host); | |
1239 | } | |
1240 | ||
7ccd266e PO |
1241 | static void mmc_read_switch_caps(struct mmc_host *host) |
1242 | { | |
1243 | int err; | |
1244 | struct mmc_card *card; | |
1245 | struct mmc_request mrq; | |
1246 | struct mmc_command cmd; | |
1247 | struct mmc_data data; | |
1248 | unsigned char *status; | |
1249 | struct scatterlist sg; | |
1250 | ||
cd9277c0 PO |
1251 | if (!(host->caps & MMC_CAP_SD_HIGHSPEED)) |
1252 | return; | |
1253 | ||
7ccd266e PO |
1254 | status = kmalloc(64, GFP_KERNEL); |
1255 | if (!status) { | |
1256 | printk(KERN_WARNING "%s: Unable to allocate buffer for " | |
1257 | "reading switch capabilities.\n", | |
1258 | mmc_hostname(host)); | |
1259 | return; | |
1260 | } | |
1261 | ||
1262 | list_for_each_entry(card, &host->cards, node) { | |
1263 | if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT)) | |
1264 | continue; | |
1265 | if (!mmc_card_sd(card)) | |
1266 | continue; | |
1267 | if (card->scr.sda_vsn < SCR_SPEC_VER_1) | |
1268 | continue; | |
1269 | ||
1270 | err = mmc_select_card(host, card); | |
1271 | if (err != MMC_ERR_NONE) { | |
1272 | mmc_card_set_dead(card); | |
1273 | continue; | |
1274 | } | |
1275 | ||
1276 | memset(&cmd, 0, sizeof(struct mmc_command)); | |
1277 | ||
1278 | cmd.opcode = SD_SWITCH; | |
1279 | cmd.arg = 0x00FFFFF1; | |
1280 | cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; | |
1281 | ||
1282 | memset(&data, 0, sizeof(struct mmc_data)); | |
1283 | ||
1284 | mmc_set_data_timeout(&data, card, 0); | |
1285 | ||
1286 | data.blksz = 64; | |
1287 | data.blocks = 1; | |
1288 | data.flags = MMC_DATA_READ; | |
1289 | data.sg = &sg; | |
1290 | data.sg_len = 1; | |
1291 | ||
1292 | memset(&mrq, 0, sizeof(struct mmc_request)); | |
1293 | ||
1294 | mrq.cmd = &cmd; | |
1295 | mrq.data = &data; | |
1296 | ||
1297 | sg_init_one(&sg, status, 64); | |
1298 | ||
1299 | mmc_wait_for_req(host, &mrq); | |
1300 | ||
1301 | if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) { | |
ae06eaf9 PO |
1302 | printk("%s: unable to read switch capabilities, " |
1303 | "performance might suffer.\n", | |
1304 | mmc_hostname(card->host)); | |
7ccd266e PO |
1305 | continue; |
1306 | } | |
1307 | ||
1308 | if (status[13] & 0x02) | |
1309 | card->sw_caps.hs_max_dtr = 50000000; | |
1310 | ||
1311 | memset(&cmd, 0, sizeof(struct mmc_command)); | |
1312 | ||
1313 | cmd.opcode = SD_SWITCH; | |
1314 | cmd.arg = 0x80FFFFF1; | |
1315 | cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; | |
1316 | ||
1317 | memset(&data, 0, sizeof(struct mmc_data)); | |
1318 | ||
1319 | mmc_set_data_timeout(&data, card, 0); | |
1320 | ||
1321 | data.blksz = 64; | |
1322 | data.blocks = 1; | |
1323 | data.flags = MMC_DATA_READ; | |
1324 | data.sg = &sg; | |
1325 | data.sg_len = 1; | |
1326 | ||
1327 | memset(&mrq, 0, sizeof(struct mmc_request)); | |
1328 | ||
1329 | mrq.cmd = &cmd; | |
1330 | mrq.data = &data; | |
1331 | ||
1332 | sg_init_one(&sg, status, 64); | |
1333 | ||
1334 | mmc_wait_for_req(host, &mrq); | |
1335 | ||
ae06eaf9 PO |
1336 | if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE || |
1337 | (status[16] & 0xF) != 1) { | |
7ccd266e PO |
1338 | printk(KERN_WARNING "%s: Problem switching card " |
1339 | "into high-speed mode!\n", | |
1340 | mmc_hostname(host)); | |
1341 | continue; | |
1342 | } | |
1343 | ||
1344 | mmc_card_set_highspeed(card); | |
cd9277c0 PO |
1345 | |
1346 | host->ios.timing = MMC_TIMING_SD_HS; | |
1347 | mmc_set_ios(host); | |
7ccd266e PO |
1348 | } |
1349 | ||
1350 | kfree(status); | |
1351 | ||
1352 | mmc_deselect_cards(host); | |
1353 | } | |
1354 | ||
1da177e4 LT |
1355 | static unsigned int mmc_calculate_clock(struct mmc_host *host) |
1356 | { | |
1357 | struct mmc_card *card; | |
1358 | unsigned int max_dtr = host->f_max; | |
1359 | ||
1360 | list_for_each_entry(card, &host->cards, node) | |
bce40a36 | 1361 | if (!mmc_card_dead(card)) { |
7ccd266e PO |
1362 | if (mmc_card_highspeed(card) && mmc_card_sd(card)) { |
1363 | if (max_dtr > card->sw_caps.hs_max_dtr) | |
1364 | max_dtr = card->sw_caps.hs_max_dtr; | |
1365 | } else if (mmc_card_highspeed(card) && !mmc_card_sd(card)) { | |
bce40a36 | 1366 | if (max_dtr > card->ext_csd.hs_max_dtr) |
7ccd266e | 1367 | max_dtr = card->ext_csd.hs_max_dtr; |
bce40a36 PL |
1368 | } else if (max_dtr > card->csd.max_dtr) { |
1369 | max_dtr = card->csd.max_dtr; | |
1370 | } | |
1371 | } | |
1da177e4 | 1372 | |
920e70c5 RK |
1373 | pr_debug("%s: selected %d.%03dMHz transfer rate\n", |
1374 | mmc_hostname(host), | |
c6563178 | 1375 | max_dtr / 1000000, (max_dtr / 1000) % 1000); |
1da177e4 LT |
1376 | |
1377 | return max_dtr; | |
1378 | } | |
1379 | ||
1380 | /* | |
1381 | * Check whether cards we already know about are still present. | |
1382 | * We do this by requesting status, and checking whether a card | |
1383 | * responds. | |
1384 | * | |
1385 | * A request for status does not cause a state change in data | |
1386 | * transfer mode. | |
1387 | */ | |
1388 | static void mmc_check_cards(struct mmc_host *host) | |
1389 | { | |
1390 | struct list_head *l, *n; | |
1391 | ||
1392 | mmc_deselect_cards(host); | |
1393 | ||
1394 | list_for_each_safe(l, n, &host->cards) { | |
1395 | struct mmc_card *card = mmc_list_to_card(l); | |
1396 | struct mmc_command cmd; | |
1397 | int err; | |
1398 | ||
1399 | cmd.opcode = MMC_SEND_STATUS; | |
1400 | cmd.arg = card->rca << 16; | |
e9225176 | 1401 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
1da177e4 LT |
1402 | |
1403 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); | |
1404 | if (err == MMC_ERR_NONE) | |
1405 | continue; | |
1406 | ||
1407 | mmc_card_set_dead(card); | |
1408 | } | |
1409 | } | |
1410 | ||
1411 | static void mmc_setup(struct mmc_host *host) | |
1412 | { | |
1413 | if (host->ios.power_mode != MMC_POWER_ON) { | |
1414 | int err; | |
1415 | u32 ocr; | |
1416 | ||
a00fc090 | 1417 | host->mode = MMC_MODE_SD; |
335eadf2 | 1418 | |
1da177e4 LT |
1419 | mmc_power_up(host); |
1420 | mmc_idle_cards(host); | |
1421 | ||
fba68bd2 PL |
1422 | err = mmc_send_if_cond(host, host->ocr_avail, NULL); |
1423 | if (err != MMC_ERR_NONE) { | |
1424 | return; | |
1425 | } | |
a00fc090 | 1426 | err = mmc_send_app_op_cond(host, 0, &ocr); |
335eadf2 PO |
1427 | |
1428 | /* | |
a00fc090 PO |
1429 | * If we fail to detect any SD cards then try |
1430 | * searching for MMC cards. | |
335eadf2 | 1431 | */ |
a00fc090 PO |
1432 | if (err != MMC_ERR_NONE) { |
1433 | host->mode = MMC_MODE_MMC; | |
1434 | ||
1435 | err = mmc_send_op_cond(host, 0, &ocr); | |
335eadf2 PO |
1436 | if (err != MMC_ERR_NONE) |
1437 | return; | |
335eadf2 | 1438 | } |
1da177e4 LT |
1439 | |
1440 | host->ocr = mmc_select_voltage(host, ocr); | |
1441 | ||
1442 | /* | |
1443 | * Since we're changing the OCR value, we seem to | |
1444 | * need to tell some cards to go back to the idle | |
1445 | * state. We wait 1ms to give cards time to | |
1446 | * respond. | |
1447 | */ | |
1448 | if (host->ocr) | |
1449 | mmc_idle_cards(host); | |
1450 | } else { | |
1451 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; | |
1452 | host->ios.clock = host->f_min; | |
920e70c5 | 1453 | mmc_set_ios(host); |
1da177e4 LT |
1454 | |
1455 | /* | |
1456 | * We should remember the OCR mask from the existing | |
1457 | * cards, and detect the new cards OCR mask, combine | |
1458 | * the two and re-select the VDD. However, if we do | |
1459 | * change VDD, we should do an idle, and then do a | |
1460 | * full re-initialisation. We would need to notify | |
1461 | * drivers so that they can re-setup the cards as | |
1462 | * well, while keeping their queues at bay. | |
1463 | * | |
1464 | * For the moment, we take the easy way out - if the | |
1465 | * new cards don't like our currently selected VDD, | |
1466 | * they drop off the bus. | |
1467 | */ | |
1468 | } | |
1469 | ||
1470 | if (host->ocr == 0) | |
1471 | return; | |
1472 | ||
1473 | /* | |
1474 | * Send the selected OCR multiple times... until the cards | |
1475 | * all get the idea that they should be ready for CMD2. | |
1476 | * (My SanDisk card seems to need this.) | |
1477 | */ | |
fba68bd2 PL |
1478 | if (host->mode == MMC_MODE_SD) { |
1479 | int err, sd2; | |
1480 | err = mmc_send_if_cond(host, host->ocr, &sd2); | |
1481 | if (err == MMC_ERR_NONE) { | |
1482 | /* | |
1483 | * If SD_SEND_IF_COND indicates an SD 2.0 | |
1484 | * compliant card and we should set bit 30 | |
1485 | * of the ocr to indicate that we can handle | |
1486 | * block-addressed SDHC cards. | |
1487 | */ | |
1488 | mmc_send_app_op_cond(host, host->ocr | (sd2 << 30), NULL); | |
1489 | } | |
1490 | } else { | |
335eadf2 | 1491 | mmc_send_op_cond(host, host->ocr, NULL); |
fba68bd2 | 1492 | } |
1da177e4 LT |
1493 | |
1494 | mmc_discover_cards(host); | |
1495 | ||
1496 | /* | |
1497 | * Ok, now switch to push-pull mode. | |
1498 | */ | |
1499 | host->ios.bus_mode = MMC_BUSMODE_PUSHPULL; | |
920e70c5 | 1500 | mmc_set_ios(host); |
1da177e4 LT |
1501 | |
1502 | mmc_read_csds(host); | |
b57c43ad | 1503 | |
7ccd266e | 1504 | if (host->mode == MMC_MODE_SD) { |
b57c43ad | 1505 | mmc_read_scrs(host); |
7ccd266e PO |
1506 | mmc_read_switch_caps(host); |
1507 | } else | |
bce40a36 | 1508 | mmc_process_ext_csds(host); |
1da177e4 LT |
1509 | } |
1510 | ||
1511 | ||
1512 | /** | |
1513 | * mmc_detect_change - process change of state on a MMC socket | |
1514 | * @host: host which changed state. | |
8dc00335 | 1515 | * @delay: optional delay to wait before detection (jiffies) |
1da177e4 LT |
1516 | * |
1517 | * All we know is that card(s) have been inserted or removed | |
1518 | * from the socket(s). We don't know which socket or cards. | |
1519 | */ | |
8dc00335 | 1520 | void mmc_detect_change(struct mmc_host *host, unsigned long delay) |
1da177e4 | 1521 | { |
c4028958 | 1522 | mmc_schedule_delayed_work(&host->detect, delay); |
1da177e4 LT |
1523 | } |
1524 | ||
1525 | EXPORT_SYMBOL(mmc_detect_change); | |
1526 | ||
1527 | ||
c4028958 | 1528 | static void mmc_rescan(struct work_struct *work) |
1da177e4 | 1529 | { |
c4028958 DH |
1530 | struct mmc_host *host = |
1531 | container_of(work, struct mmc_host, detect.work); | |
1da177e4 | 1532 | struct list_head *l, *n; |
25a122fd | 1533 | unsigned char power_mode; |
1da177e4 LT |
1534 | |
1535 | mmc_claim_host(host); | |
1536 | ||
25a122fd TT |
1537 | /* |
1538 | * Check for removed cards and newly inserted ones. We check for | |
1539 | * removed cards first so we can intelligently re-select the VDD. | |
1540 | */ | |
1541 | power_mode = host->ios.power_mode; | |
1542 | if (power_mode == MMC_POWER_ON) | |
1da177e4 LT |
1543 | mmc_check_cards(host); |
1544 | ||
1545 | mmc_setup(host); | |
1546 | ||
25a122fd TT |
1547 | /* |
1548 | * Some broken cards process CMD1 even in stand-by state. There is | |
1549 | * no reply, but an ILLEGAL_COMMAND error is cached and returned | |
1550 | * after next command. We poll for card status here to clear any | |
1551 | * possibly pending error. | |
1552 | */ | |
1553 | if (power_mode == MMC_POWER_ON) | |
1554 | mmc_check_cards(host); | |
1555 | ||
1da177e4 LT |
1556 | if (!list_empty(&host->cards)) { |
1557 | /* | |
1558 | * (Re-)calculate the fastest clock rate which the | |
1559 | * attached cards and the host support. | |
1560 | */ | |
1561 | host->ios.clock = mmc_calculate_clock(host); | |
920e70c5 | 1562 | mmc_set_ios(host); |
1da177e4 LT |
1563 | } |
1564 | ||
1565 | mmc_release_host(host); | |
1566 | ||
1567 | list_for_each_safe(l, n, &host->cards) { | |
1568 | struct mmc_card *card = mmc_list_to_card(l); | |
1569 | ||
1570 | /* | |
1571 | * If this is a new and good card, register it. | |
1572 | */ | |
1573 | if (!mmc_card_present(card) && !mmc_card_dead(card)) { | |
1574 | if (mmc_register_card(card)) | |
1575 | mmc_card_set_dead(card); | |
1576 | else | |
1577 | mmc_card_set_present(card); | |
1578 | } | |
1579 | ||
1580 | /* | |
1581 | * If this card is dead, destroy it. | |
1582 | */ | |
1583 | if (mmc_card_dead(card)) { | |
1584 | list_del(&card->node); | |
1585 | mmc_remove_card(card); | |
1586 | } | |
1587 | } | |
1588 | ||
1589 | /* | |
1590 | * If we discover that there are no cards on the | |
1591 | * bus, turn off the clock and power down. | |
1592 | */ | |
1593 | if (list_empty(&host->cards)) | |
1594 | mmc_power_off(host); | |
1595 | } | |
1596 | ||
1597 | ||
1598 | /** | |
1599 | * mmc_alloc_host - initialise the per-host structure. | |
1600 | * @extra: sizeof private data structure | |
1601 | * @dev: pointer to host device model structure | |
1602 | * | |
1603 | * Initialise the per-host structure. | |
1604 | */ | |
1605 | struct mmc_host *mmc_alloc_host(int extra, struct device *dev) | |
1606 | { | |
1607 | struct mmc_host *host; | |
1608 | ||
00b137cf | 1609 | host = mmc_alloc_host_sysfs(extra, dev); |
1da177e4 | 1610 | if (host) { |
1da177e4 LT |
1611 | spin_lock_init(&host->lock); |
1612 | init_waitqueue_head(&host->wq); | |
1613 | INIT_LIST_HEAD(&host->cards); | |
c4028958 | 1614 | INIT_DELAYED_WORK(&host->detect, mmc_rescan); |
1da177e4 | 1615 | |
1da177e4 LT |
1616 | /* |
1617 | * By default, hosts do not support SGIO or large requests. | |
1618 | * They have to set these according to their abilities. | |
1619 | */ | |
1620 | host->max_hw_segs = 1; | |
1621 | host->max_phys_segs = 1; | |
1da177e4 | 1622 | host->max_seg_size = PAGE_CACHE_SIZE; |
fe4a3c7a | 1623 | |
55db890a | 1624 | host->max_req_size = PAGE_CACHE_SIZE; |
fe4a3c7a | 1625 | host->max_blk_size = 512; |
55db890a | 1626 | host->max_blk_count = PAGE_CACHE_SIZE / 512; |
1da177e4 LT |
1627 | } |
1628 | ||
1629 | return host; | |
1630 | } | |
1631 | ||
1632 | EXPORT_SYMBOL(mmc_alloc_host); | |
1633 | ||
1634 | /** | |
1635 | * mmc_add_host - initialise host hardware | |
1636 | * @host: mmc host | |
1637 | */ | |
1638 | int mmc_add_host(struct mmc_host *host) | |
1639 | { | |
00b137cf | 1640 | int ret; |
1da177e4 | 1641 | |
00b137cf RK |
1642 | ret = mmc_add_host_sysfs(host); |
1643 | if (ret == 0) { | |
1644 | mmc_power_off(host); | |
8dc00335 | 1645 | mmc_detect_change(host, 0); |
00b137cf | 1646 | } |
1da177e4 | 1647 | |
00b137cf | 1648 | return ret; |
1da177e4 LT |
1649 | } |
1650 | ||
1651 | EXPORT_SYMBOL(mmc_add_host); | |
1652 | ||
1653 | /** | |
1654 | * mmc_remove_host - remove host hardware | |
1655 | * @host: mmc host | |
1656 | * | |
1657 | * Unregister and remove all cards associated with this host, | |
1658 | * and power down the MMC bus. | |
1659 | */ | |
1660 | void mmc_remove_host(struct mmc_host *host) | |
1661 | { | |
1662 | struct list_head *l, *n; | |
1663 | ||
1664 | list_for_each_safe(l, n, &host->cards) { | |
1665 | struct mmc_card *card = mmc_list_to_card(l); | |
1666 | ||
1667 | mmc_remove_card(card); | |
1668 | } | |
1669 | ||
1670 | mmc_power_off(host); | |
00b137cf | 1671 | mmc_remove_host_sysfs(host); |
1da177e4 LT |
1672 | } |
1673 | ||
1674 | EXPORT_SYMBOL(mmc_remove_host); | |
1675 | ||
1676 | /** | |
1677 | * mmc_free_host - free the host structure | |
1678 | * @host: mmc host | |
1679 | * | |
1680 | * Free the host once all references to it have been dropped. | |
1681 | */ | |
1682 | void mmc_free_host(struct mmc_host *host) | |
1683 | { | |
7104e2d5 | 1684 | mmc_flush_scheduled_work(); |
00b137cf | 1685 | mmc_free_host_sysfs(host); |
1da177e4 LT |
1686 | } |
1687 | ||
1688 | EXPORT_SYMBOL(mmc_free_host); | |
1689 | ||
1690 | #ifdef CONFIG_PM | |
1691 | ||
1692 | /** | |
1693 | * mmc_suspend_host - suspend a host | |
1694 | * @host: mmc host | |
1695 | * @state: suspend mode (PM_SUSPEND_xxx) | |
1696 | */ | |
e5378ca8 | 1697 | int mmc_suspend_host(struct mmc_host *host, pm_message_t state) |
1da177e4 LT |
1698 | { |
1699 | mmc_claim_host(host); | |
1700 | mmc_deselect_cards(host); | |
1701 | mmc_power_off(host); | |
1702 | mmc_release_host(host); | |
1703 | ||
1704 | return 0; | |
1705 | } | |
1706 | ||
1707 | EXPORT_SYMBOL(mmc_suspend_host); | |
1708 | ||
1709 | /** | |
1710 | * mmc_resume_host - resume a previously suspended host | |
1711 | * @host: mmc host | |
1712 | */ | |
1713 | int mmc_resume_host(struct mmc_host *host) | |
1714 | { | |
c4028958 | 1715 | mmc_rescan(&host->detect.work); |
1da177e4 LT |
1716 | |
1717 | return 0; | |
1718 | } | |
1719 | ||
1720 | EXPORT_SYMBOL(mmc_resume_host); | |
1721 | ||
1722 | #endif | |
1723 | ||
1724 | MODULE_LICENSE("GPL"); |