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. | |
1da177e4 LT |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License version 2 as | |
10 | * published by the Free Software Foundation. | |
11 | */ | |
12 | #include <linux/config.h> | |
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 LT |
105 | |
106 | WARN_ON(host->card_busy == NULL); | |
107 | ||
108 | mrq->cmd->error = 0; | |
109 | mrq->cmd->mrq = mrq; | |
110 | if (mrq->data) { | |
111 | mrq->cmd->data = mrq->data; | |
112 | mrq->data->error = 0; | |
113 | mrq->data->mrq = mrq; | |
114 | if (mrq->stop) { | |
115 | mrq->data->stop = mrq->stop; | |
116 | mrq->stop->error = 0; | |
117 | mrq->stop->mrq = mrq; | |
118 | } | |
119 | } | |
120 | host->ops->request(host, mrq); | |
121 | } | |
122 | ||
123 | EXPORT_SYMBOL(mmc_start_request); | |
124 | ||
125 | static void mmc_wait_done(struct mmc_request *mrq) | |
126 | { | |
127 | complete(mrq->done_data); | |
128 | } | |
129 | ||
130 | int mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq) | |
131 | { | |
132 | DECLARE_COMPLETION(complete); | |
133 | ||
134 | mrq->done_data = &complete; | |
135 | mrq->done = mmc_wait_done; | |
136 | ||
137 | mmc_start_request(host, mrq); | |
138 | ||
139 | wait_for_completion(&complete); | |
140 | ||
141 | return 0; | |
142 | } | |
143 | ||
144 | EXPORT_SYMBOL(mmc_wait_for_req); | |
145 | ||
146 | /** | |
147 | * mmc_wait_for_cmd - start a command and wait for completion | |
148 | * @host: MMC host to start command | |
149 | * @cmd: MMC command to start | |
150 | * @retries: maximum number of retries | |
151 | * | |
152 | * Start a new MMC command for a host, and wait for the command | |
153 | * to complete. Return any error that occurred while the command | |
154 | * was executing. Do not attempt to parse the response. | |
155 | */ | |
156 | int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries) | |
157 | { | |
158 | struct mmc_request mrq; | |
159 | ||
160 | BUG_ON(host->card_busy == NULL); | |
161 | ||
162 | memset(&mrq, 0, sizeof(struct mmc_request)); | |
163 | ||
164 | memset(cmd->resp, 0, sizeof(cmd->resp)); | |
165 | cmd->retries = retries; | |
166 | ||
167 | mrq.cmd = cmd; | |
168 | cmd->data = NULL; | |
169 | ||
170 | mmc_wait_for_req(host, &mrq); | |
171 | ||
172 | return cmd->error; | |
173 | } | |
174 | ||
175 | EXPORT_SYMBOL(mmc_wait_for_cmd); | |
176 | ||
335eadf2 PO |
177 | /** |
178 | * mmc_wait_for_app_cmd - start an application command and wait for | |
179 | completion | |
180 | * @host: MMC host to start command | |
181 | * @rca: RCA to send MMC_APP_CMD to | |
182 | * @cmd: MMC command to start | |
183 | * @retries: maximum number of retries | |
184 | * | |
185 | * Sends a MMC_APP_CMD, checks the card response, sends the command | |
186 | * in the parameter and waits for it to complete. Return any error | |
187 | * that occurred while the command was executing. Do not attempt to | |
188 | * parse the response. | |
189 | */ | |
190 | int mmc_wait_for_app_cmd(struct mmc_host *host, unsigned int rca, | |
191 | struct mmc_command *cmd, int retries) | |
192 | { | |
193 | struct mmc_request mrq; | |
194 | struct mmc_command appcmd; | |
195 | ||
196 | int i, err; | |
197 | ||
198 | BUG_ON(host->card_busy == NULL); | |
199 | BUG_ON(retries < 0); | |
200 | ||
201 | err = MMC_ERR_INVALID; | |
202 | ||
203 | /* | |
204 | * We have to resend MMC_APP_CMD for each attempt so | |
205 | * we cannot use the retries field in mmc_command. | |
206 | */ | |
207 | for (i = 0;i <= retries;i++) { | |
208 | memset(&mrq, 0, sizeof(struct mmc_request)); | |
209 | ||
210 | appcmd.opcode = MMC_APP_CMD; | |
211 | appcmd.arg = rca << 16; | |
e9225176 | 212 | appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
335eadf2 PO |
213 | appcmd.retries = 0; |
214 | memset(appcmd.resp, 0, sizeof(appcmd.resp)); | |
215 | appcmd.data = NULL; | |
216 | ||
217 | mrq.cmd = &appcmd; | |
218 | appcmd.data = NULL; | |
219 | ||
220 | mmc_wait_for_req(host, &mrq); | |
221 | ||
222 | if (appcmd.error) { | |
223 | err = appcmd.error; | |
224 | continue; | |
225 | } | |
226 | ||
227 | /* Check that card supported application commands */ | |
228 | if (!(appcmd.resp[0] & R1_APP_CMD)) | |
229 | return MMC_ERR_FAILED; | |
230 | ||
231 | memset(&mrq, 0, sizeof(struct mmc_request)); | |
232 | ||
233 | memset(cmd->resp, 0, sizeof(cmd->resp)); | |
234 | cmd->retries = 0; | |
235 | ||
236 | mrq.cmd = cmd; | |
237 | cmd->data = NULL; | |
1da177e4 | 238 | |
335eadf2 PO |
239 | mmc_wait_for_req(host, &mrq); |
240 | ||
241 | err = cmd->error; | |
242 | if (cmd->error == MMC_ERR_NONE) | |
243 | break; | |
244 | } | |
245 | ||
246 | return err; | |
247 | } | |
248 | ||
249 | EXPORT_SYMBOL(mmc_wait_for_app_cmd); | |
1da177e4 | 250 | |
b57c43ad PO |
251 | static int mmc_select_card(struct mmc_host *host, struct mmc_card *card); |
252 | ||
1da177e4 LT |
253 | /** |
254 | * __mmc_claim_host - exclusively claim a host | |
255 | * @host: mmc host to claim | |
256 | * @card: mmc card to claim host for | |
257 | * | |
258 | * Claim a host for a set of operations. If a valid card | |
259 | * is passed and this wasn't the last card selected, select | |
260 | * the card before returning. | |
261 | * | |
262 | * Note: you should use mmc_card_claim_host or mmc_claim_host. | |
263 | */ | |
264 | int __mmc_claim_host(struct mmc_host *host, struct mmc_card *card) | |
265 | { | |
266 | DECLARE_WAITQUEUE(wait, current); | |
267 | unsigned long flags; | |
268 | int err = 0; | |
269 | ||
270 | add_wait_queue(&host->wq, &wait); | |
271 | spin_lock_irqsave(&host->lock, flags); | |
272 | while (1) { | |
273 | set_current_state(TASK_UNINTERRUPTIBLE); | |
274 | if (host->card_busy == NULL) | |
275 | break; | |
276 | spin_unlock_irqrestore(&host->lock, flags); | |
277 | schedule(); | |
278 | spin_lock_irqsave(&host->lock, flags); | |
279 | } | |
280 | set_current_state(TASK_RUNNING); | |
281 | host->card_busy = card; | |
282 | spin_unlock_irqrestore(&host->lock, flags); | |
283 | remove_wait_queue(&host->wq, &wait); | |
284 | ||
b57c43ad PO |
285 | if (card != (void *)-1) { |
286 | err = mmc_select_card(host, card); | |
287 | if (err != MMC_ERR_NONE) | |
288 | return err; | |
1da177e4 LT |
289 | } |
290 | ||
291 | return err; | |
292 | } | |
293 | ||
294 | EXPORT_SYMBOL(__mmc_claim_host); | |
295 | ||
296 | /** | |
297 | * mmc_release_host - release a host | |
298 | * @host: mmc host to release | |
299 | * | |
300 | * Release a MMC host, allowing others to claim the host | |
301 | * for their operations. | |
302 | */ | |
303 | void mmc_release_host(struct mmc_host *host) | |
304 | { | |
305 | unsigned long flags; | |
306 | ||
307 | BUG_ON(host->card_busy == NULL); | |
308 | ||
309 | spin_lock_irqsave(&host->lock, flags); | |
310 | host->card_busy = NULL; | |
311 | spin_unlock_irqrestore(&host->lock, flags); | |
312 | ||
313 | wake_up(&host->wq); | |
314 | } | |
315 | ||
316 | EXPORT_SYMBOL(mmc_release_host); | |
317 | ||
920e70c5 RK |
318 | static inline void mmc_set_ios(struct mmc_host *host) |
319 | { | |
320 | struct mmc_ios *ios = &host->ios; | |
321 | ||
322 | pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u width %u\n", | |
323 | mmc_hostname(host), ios->clock, ios->bus_mode, | |
324 | ios->power_mode, ios->chip_select, ios->vdd, | |
325 | ios->bus_width); | |
326 | ||
327 | host->ops->set_ios(host, ios); | |
328 | } | |
329 | ||
b57c43ad PO |
330 | static int mmc_select_card(struct mmc_host *host, struct mmc_card *card) |
331 | { | |
332 | int err; | |
333 | struct mmc_command cmd; | |
334 | ||
335 | BUG_ON(host->card_busy == NULL); | |
336 | ||
337 | if (host->card_selected == card) | |
338 | return MMC_ERR_NONE; | |
339 | ||
340 | host->card_selected = card; | |
341 | ||
342 | cmd.opcode = MMC_SELECT_CARD; | |
343 | cmd.arg = card->rca << 16; | |
e9225176 | 344 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
b57c43ad PO |
345 | |
346 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); | |
347 | if (err != MMC_ERR_NONE) | |
348 | return err; | |
349 | ||
f218278a PO |
350 | /* |
351 | * Default bus width is 1 bit. | |
352 | */ | |
353 | host->ios.bus_width = MMC_BUS_WIDTH_1; | |
354 | ||
355 | /* | |
356 | * We can only change the bus width of the selected | |
357 | * card so therefore we have to put the handling | |
358 | * here. | |
359 | */ | |
360 | if (host->caps & MMC_CAP_4_BIT_DATA) { | |
361 | /* | |
362 | * The card is in 1 bit mode by default so | |
363 | * we only need to change if it supports the | |
364 | * wider version. | |
365 | */ | |
366 | if (mmc_card_sd(card) && | |
367 | (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) { | |
368 | struct mmc_command cmd; | |
369 | cmd.opcode = SD_APP_SET_BUS_WIDTH; | |
370 | cmd.arg = SD_BUS_WIDTH_4; | |
e9225176 | 371 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
f218278a PO |
372 | |
373 | err = mmc_wait_for_app_cmd(host, card->rca, &cmd, | |
374 | CMD_RETRIES); | |
375 | if (err != MMC_ERR_NONE) | |
376 | return err; | |
377 | ||
378 | host->ios.bus_width = MMC_BUS_WIDTH_4; | |
379 | } | |
380 | } | |
381 | ||
920e70c5 | 382 | mmc_set_ios(host); |
f218278a | 383 | |
b57c43ad PO |
384 | return MMC_ERR_NONE; |
385 | } | |
386 | ||
1da177e4 LT |
387 | /* |
388 | * Ensure that no card is selected. | |
389 | */ | |
390 | static void mmc_deselect_cards(struct mmc_host *host) | |
391 | { | |
392 | struct mmc_command cmd; | |
393 | ||
394 | if (host->card_selected) { | |
395 | host->card_selected = NULL; | |
396 | ||
397 | cmd.opcode = MMC_SELECT_CARD; | |
398 | cmd.arg = 0; | |
e9225176 | 399 | cmd.flags = MMC_RSP_NONE | MMC_CMD_AC; |
1da177e4 LT |
400 | |
401 | mmc_wait_for_cmd(host, &cmd, 0); | |
402 | } | |
403 | } | |
404 | ||
405 | ||
406 | static inline void mmc_delay(unsigned int ms) | |
407 | { | |
408 | if (ms < HZ / 1000) { | |
409 | yield(); | |
410 | mdelay(ms); | |
411 | } else { | |
412 | msleep_interruptible (ms); | |
413 | } | |
414 | } | |
415 | ||
416 | /* | |
417 | * Mask off any voltages we don't support and select | |
418 | * the lowest voltage | |
419 | */ | |
420 | static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr) | |
421 | { | |
422 | int bit; | |
423 | ||
424 | ocr &= host->ocr_avail; | |
425 | ||
426 | bit = ffs(ocr); | |
427 | if (bit) { | |
428 | bit -= 1; | |
429 | ||
430 | ocr = 3 << bit; | |
431 | ||
432 | host->ios.vdd = bit; | |
920e70c5 | 433 | mmc_set_ios(host); |
1da177e4 LT |
434 | } else { |
435 | ocr = 0; | |
436 | } | |
437 | ||
438 | return ocr; | |
439 | } | |
440 | ||
441 | #define UNSTUFF_BITS(resp,start,size) \ | |
442 | ({ \ | |
443 | const int __size = size; \ | |
444 | const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \ | |
445 | const int __off = 3 - ((start) / 32); \ | |
446 | const int __shft = (start) & 31; \ | |
447 | u32 __res; \ | |
448 | \ | |
449 | __res = resp[__off] >> __shft; \ | |
450 | if (__size + __shft > 32) \ | |
451 | __res |= resp[__off-1] << ((32 - __shft) % 32); \ | |
452 | __res & __mask; \ | |
453 | }) | |
454 | ||
455 | /* | |
456 | * Given the decoded CSD structure, decode the raw CID to our CID structure. | |
457 | */ | |
458 | static void mmc_decode_cid(struct mmc_card *card) | |
459 | { | |
460 | u32 *resp = card->raw_cid; | |
461 | ||
462 | memset(&card->cid, 0, sizeof(struct mmc_cid)); | |
463 | ||
335eadf2 PO |
464 | if (mmc_card_sd(card)) { |
465 | /* | |
466 | * SD doesn't currently have a version field so we will | |
467 | * have to assume we can parse this. | |
468 | */ | |
469 | card->cid.manfid = UNSTUFF_BITS(resp, 120, 8); | |
470 | card->cid.oemid = UNSTUFF_BITS(resp, 104, 16); | |
471 | card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); | |
472 | card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); | |
473 | card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); | |
474 | card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); | |
475 | card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); | |
476 | card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4); | |
477 | card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4); | |
478 | card->cid.serial = UNSTUFF_BITS(resp, 24, 32); | |
479 | card->cid.year = UNSTUFF_BITS(resp, 12, 8); | |
480 | card->cid.month = UNSTUFF_BITS(resp, 8, 4); | |
481 | ||
482 | card->cid.year += 2000; /* SD cards year offset */ | |
a00fc090 | 483 | } else { |
335eadf2 PO |
484 | /* |
485 | * The selection of the format here is based upon published | |
486 | * specs from sandisk and from what people have reported. | |
487 | */ | |
488 | switch (card->csd.mmca_vsn) { | |
489 | case 0: /* MMC v1.0 - v1.2 */ | |
490 | case 1: /* MMC v1.4 */ | |
491 | card->cid.manfid = UNSTUFF_BITS(resp, 104, 24); | |
492 | card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); | |
493 | card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); | |
494 | card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); | |
495 | card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); | |
496 | card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); | |
497 | card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8); | |
498 | card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8); | |
499 | card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4); | |
500 | card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4); | |
501 | card->cid.serial = UNSTUFF_BITS(resp, 16, 24); | |
502 | card->cid.month = UNSTUFF_BITS(resp, 12, 4); | |
503 | card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997; | |
504 | break; | |
505 | ||
506 | case 2: /* MMC v2.0 - v2.2 */ | |
507 | case 3: /* MMC v3.1 - v3.3 */ | |
cb757b4e | 508 | case 4: /* MMC v4 */ |
335eadf2 PO |
509 | card->cid.manfid = UNSTUFF_BITS(resp, 120, 8); |
510 | card->cid.oemid = UNSTUFF_BITS(resp, 104, 16); | |
511 | card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); | |
512 | card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); | |
513 | card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); | |
514 | card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); | |
515 | card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); | |
516 | card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8); | |
517 | card->cid.serial = UNSTUFF_BITS(resp, 16, 32); | |
518 | card->cid.month = UNSTUFF_BITS(resp, 12, 4); | |
519 | card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997; | |
520 | break; | |
521 | ||
522 | default: | |
523 | printk("%s: card has unknown MMCA version %d\n", | |
524 | mmc_hostname(card->host), card->csd.mmca_vsn); | |
525 | mmc_card_set_bad(card); | |
526 | break; | |
527 | } | |
1da177e4 LT |
528 | } |
529 | } | |
530 | ||
531 | /* | |
532 | * Given a 128-bit response, decode to our card CSD structure. | |
533 | */ | |
534 | static void mmc_decode_csd(struct mmc_card *card) | |
535 | { | |
536 | struct mmc_csd *csd = &card->csd; | |
537 | unsigned int e, m, csd_struct; | |
538 | u32 *resp = card->raw_csd; | |
539 | ||
335eadf2 PO |
540 | if (mmc_card_sd(card)) { |
541 | csd_struct = UNSTUFF_BITS(resp, 126, 2); | |
542 | if (csd_struct != 0) { | |
543 | printk("%s: unrecognised CSD structure version %d\n", | |
544 | mmc_hostname(card->host), csd_struct); | |
545 | mmc_card_set_bad(card); | |
546 | return; | |
547 | } | |
548 | ||
549 | m = UNSTUFF_BITS(resp, 115, 4); | |
550 | e = UNSTUFF_BITS(resp, 112, 3); | |
551 | csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10; | |
552 | csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100; | |
553 | ||
554 | m = UNSTUFF_BITS(resp, 99, 4); | |
555 | e = UNSTUFF_BITS(resp, 96, 3); | |
556 | csd->max_dtr = tran_exp[e] * tran_mant[m]; | |
557 | csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); | |
558 | ||
559 | e = UNSTUFF_BITS(resp, 47, 3); | |
560 | m = UNSTUFF_BITS(resp, 62, 12); | |
561 | csd->capacity = (1 + m) << (e + 2); | |
562 | ||
563 | csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); | |
a6f6c96b RK |
564 | csd->read_partial = UNSTUFF_BITS(resp, 79, 1); |
565 | csd->write_misalign = UNSTUFF_BITS(resp, 78, 1); | |
566 | csd->read_misalign = UNSTUFF_BITS(resp, 77, 1); | |
37be4e78 | 567 | csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3); |
a6f6c96b RK |
568 | csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); |
569 | csd->write_partial = UNSTUFF_BITS(resp, 21, 1); | |
a00fc090 | 570 | } else { |
335eadf2 PO |
571 | /* |
572 | * We only understand CSD structure v1.1 and v1.2. | |
573 | * v1.2 has extra information in bits 15, 11 and 10. | |
574 | */ | |
575 | csd_struct = UNSTUFF_BITS(resp, 126, 2); | |
576 | if (csd_struct != 1 && csd_struct != 2) { | |
577 | printk("%s: unrecognised CSD structure version %d\n", | |
578 | mmc_hostname(card->host), csd_struct); | |
579 | mmc_card_set_bad(card); | |
580 | return; | |
581 | } | |
1da177e4 | 582 | |
335eadf2 PO |
583 | csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4); |
584 | m = UNSTUFF_BITS(resp, 115, 4); | |
585 | e = UNSTUFF_BITS(resp, 112, 3); | |
586 | csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10; | |
587 | csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100; | |
1da177e4 | 588 | |
335eadf2 PO |
589 | m = UNSTUFF_BITS(resp, 99, 4); |
590 | e = UNSTUFF_BITS(resp, 96, 3); | |
591 | csd->max_dtr = tran_exp[e] * tran_mant[m]; | |
592 | csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); | |
1da177e4 | 593 | |
335eadf2 PO |
594 | e = UNSTUFF_BITS(resp, 47, 3); |
595 | m = UNSTUFF_BITS(resp, 62, 12); | |
596 | csd->capacity = (1 + m) << (e + 2); | |
1da177e4 | 597 | |
335eadf2 | 598 | csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); |
a6f6c96b RK |
599 | csd->read_partial = UNSTUFF_BITS(resp, 79, 1); |
600 | csd->write_misalign = UNSTUFF_BITS(resp, 78, 1); | |
601 | csd->read_misalign = UNSTUFF_BITS(resp, 77, 1); | |
37be4e78 | 602 | csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3); |
a6f6c96b RK |
603 | csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); |
604 | csd->write_partial = UNSTUFF_BITS(resp, 21, 1); | |
335eadf2 | 605 | } |
1da177e4 LT |
606 | } |
607 | ||
b57c43ad PO |
608 | /* |
609 | * Given a 64-bit response, decode to our card SCR structure. | |
610 | */ | |
611 | static void mmc_decode_scr(struct mmc_card *card) | |
612 | { | |
613 | struct sd_scr *scr = &card->scr; | |
614 | unsigned int scr_struct; | |
615 | u32 resp[4]; | |
616 | ||
617 | BUG_ON(!mmc_card_sd(card)); | |
618 | ||
619 | resp[3] = card->raw_scr[1]; | |
620 | resp[2] = card->raw_scr[0]; | |
621 | ||
622 | scr_struct = UNSTUFF_BITS(resp, 60, 4); | |
623 | if (scr_struct != 0) { | |
624 | printk("%s: unrecognised SCR structure version %d\n", | |
625 | mmc_hostname(card->host), scr_struct); | |
626 | mmc_card_set_bad(card); | |
627 | return; | |
628 | } | |
629 | ||
630 | scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4); | |
631 | scr->bus_widths = UNSTUFF_BITS(resp, 48, 4); | |
632 | } | |
633 | ||
1da177e4 LT |
634 | /* |
635 | * Locate a MMC card on this MMC host given a raw CID. | |
636 | */ | |
637 | static struct mmc_card *mmc_find_card(struct mmc_host *host, u32 *raw_cid) | |
638 | { | |
639 | struct mmc_card *card; | |
640 | ||
641 | list_for_each_entry(card, &host->cards, node) { | |
642 | if (memcmp(card->raw_cid, raw_cid, sizeof(card->raw_cid)) == 0) | |
643 | return card; | |
644 | } | |
645 | return NULL; | |
646 | } | |
647 | ||
648 | /* | |
649 | * Allocate a new MMC card, and assign a unique RCA. | |
650 | */ | |
651 | static struct mmc_card * | |
652 | mmc_alloc_card(struct mmc_host *host, u32 *raw_cid, unsigned int *frca) | |
653 | { | |
654 | struct mmc_card *card, *c; | |
655 | unsigned int rca = *frca; | |
656 | ||
657 | card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL); | |
658 | if (!card) | |
659 | return ERR_PTR(-ENOMEM); | |
660 | ||
661 | mmc_init_card(card, host); | |
662 | memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid)); | |
663 | ||
664 | again: | |
665 | list_for_each_entry(c, &host->cards, node) | |
666 | if (c->rca == rca) { | |
667 | rca++; | |
668 | goto again; | |
669 | } | |
670 | ||
671 | card->rca = rca; | |
672 | ||
673 | *frca = rca; | |
674 | ||
675 | return card; | |
676 | } | |
677 | ||
678 | /* | |
679 | * Tell attached cards to go to IDLE state | |
680 | */ | |
681 | static void mmc_idle_cards(struct mmc_host *host) | |
682 | { | |
683 | struct mmc_command cmd; | |
684 | ||
865e9f13 | 685 | host->ios.chip_select = MMC_CS_HIGH; |
920e70c5 | 686 | mmc_set_ios(host); |
865e9f13 PO |
687 | |
688 | mmc_delay(1); | |
689 | ||
1da177e4 LT |
690 | cmd.opcode = MMC_GO_IDLE_STATE; |
691 | cmd.arg = 0; | |
e9225176 | 692 | cmd.flags = MMC_RSP_NONE | MMC_CMD_BC; |
1da177e4 LT |
693 | |
694 | mmc_wait_for_cmd(host, &cmd, 0); | |
695 | ||
696 | mmc_delay(1); | |
865e9f13 PO |
697 | |
698 | host->ios.chip_select = MMC_CS_DONTCARE; | |
920e70c5 | 699 | mmc_set_ios(host); |
865e9f13 PO |
700 | |
701 | mmc_delay(1); | |
1da177e4 LT |
702 | } |
703 | ||
704 | /* | |
45f8245b RK |
705 | * Apply power to the MMC stack. This is a two-stage process. |
706 | * First, we enable power to the card without the clock running. | |
707 | * We then wait a bit for the power to stabilise. Finally, | |
708 | * enable the bus drivers and clock to the card. | |
709 | * | |
710 | * We must _NOT_ enable the clock prior to power stablising. | |
711 | * | |
712 | * If a host does all the power sequencing itself, ignore the | |
713 | * initial MMC_POWER_UP stage. | |
1da177e4 LT |
714 | */ |
715 | static void mmc_power_up(struct mmc_host *host) | |
716 | { | |
717 | int bit = fls(host->ocr_avail) - 1; | |
718 | ||
719 | host->ios.vdd = bit; | |
720 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; | |
865e9f13 | 721 | host->ios.chip_select = MMC_CS_DONTCARE; |
1da177e4 | 722 | host->ios.power_mode = MMC_POWER_UP; |
f218278a | 723 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
920e70c5 | 724 | mmc_set_ios(host); |
1da177e4 LT |
725 | |
726 | mmc_delay(1); | |
727 | ||
728 | host->ios.clock = host->f_min; | |
729 | host->ios.power_mode = MMC_POWER_ON; | |
920e70c5 | 730 | mmc_set_ios(host); |
1da177e4 LT |
731 | |
732 | mmc_delay(2); | |
733 | } | |
734 | ||
735 | static void mmc_power_off(struct mmc_host *host) | |
736 | { | |
737 | host->ios.clock = 0; | |
738 | host->ios.vdd = 0; | |
739 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; | |
865e9f13 | 740 | host->ios.chip_select = MMC_CS_DONTCARE; |
1da177e4 | 741 | host->ios.power_mode = MMC_POWER_OFF; |
f218278a | 742 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
920e70c5 | 743 | mmc_set_ios(host); |
1da177e4 LT |
744 | } |
745 | ||
746 | static int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr) | |
747 | { | |
748 | struct mmc_command cmd; | |
749 | int i, err = 0; | |
750 | ||
751 | cmd.opcode = MMC_SEND_OP_COND; | |
752 | cmd.arg = ocr; | |
e9225176 | 753 | cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; |
1da177e4 LT |
754 | |
755 | for (i = 100; i; i--) { | |
756 | err = mmc_wait_for_cmd(host, &cmd, 0); | |
757 | if (err != MMC_ERR_NONE) | |
758 | break; | |
759 | ||
760 | if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0) | |
761 | break; | |
762 | ||
763 | err = MMC_ERR_TIMEOUT; | |
764 | ||
765 | mmc_delay(10); | |
766 | } | |
767 | ||
768 | if (rocr) | |
769 | *rocr = cmd.resp[0]; | |
770 | ||
771 | return err; | |
772 | } | |
773 | ||
335eadf2 PO |
774 | static int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr) |
775 | { | |
776 | struct mmc_command cmd; | |
777 | int i, err = 0; | |
778 | ||
779 | cmd.opcode = SD_APP_OP_COND; | |
780 | cmd.arg = ocr; | |
e9225176 | 781 | cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; |
335eadf2 PO |
782 | |
783 | for (i = 100; i; i--) { | |
784 | err = mmc_wait_for_app_cmd(host, 0, &cmd, CMD_RETRIES); | |
785 | if (err != MMC_ERR_NONE) | |
786 | break; | |
787 | ||
788 | if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0) | |
789 | break; | |
790 | ||
791 | err = MMC_ERR_TIMEOUT; | |
792 | ||
793 | mmc_delay(10); | |
794 | } | |
795 | ||
796 | if (rocr) | |
797 | *rocr = cmd.resp[0]; | |
798 | ||
799 | return err; | |
800 | } | |
801 | ||
1da177e4 LT |
802 | /* |
803 | * Discover cards by requesting their CID. If this command | |
804 | * times out, it is not an error; there are no further cards | |
805 | * to be discovered. Add new cards to the list. | |
806 | * | |
807 | * Create a mmc_card entry for each discovered card, assigning | |
808 | * it an RCA, and save the raw CID for decoding later. | |
809 | */ | |
810 | static void mmc_discover_cards(struct mmc_host *host) | |
811 | { | |
812 | struct mmc_card *card; | |
813 | unsigned int first_rca = 1, err; | |
814 | ||
815 | while (1) { | |
816 | struct mmc_command cmd; | |
817 | ||
818 | cmd.opcode = MMC_ALL_SEND_CID; | |
819 | cmd.arg = 0; | |
e9225176 | 820 | cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; |
1da177e4 LT |
821 | |
822 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); | |
823 | if (err == MMC_ERR_TIMEOUT) { | |
824 | err = MMC_ERR_NONE; | |
825 | break; | |
826 | } | |
827 | if (err != MMC_ERR_NONE) { | |
828 | printk(KERN_ERR "%s: error requesting CID: %d\n", | |
d366b643 | 829 | mmc_hostname(host), err); |
1da177e4 LT |
830 | break; |
831 | } | |
832 | ||
833 | card = mmc_find_card(host, cmd.resp); | |
834 | if (!card) { | |
835 | card = mmc_alloc_card(host, cmd.resp, &first_rca); | |
836 | if (IS_ERR(card)) { | |
837 | err = PTR_ERR(card); | |
838 | break; | |
839 | } | |
840 | list_add(&card->node, &host->cards); | |
841 | } | |
842 | ||
843 | card->state &= ~MMC_STATE_DEAD; | |
844 | ||
335eadf2 PO |
845 | if (host->mode == MMC_MODE_SD) { |
846 | mmc_card_set_sd(card); | |
1da177e4 | 847 | |
335eadf2 PO |
848 | cmd.opcode = SD_SEND_RELATIVE_ADDR; |
849 | cmd.arg = 0; | |
e9225176 | 850 | cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR; |
335eadf2 PO |
851 | |
852 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); | |
853 | if (err != MMC_ERR_NONE) | |
854 | mmc_card_set_dead(card); | |
a00fc090 | 855 | else { |
335eadf2 | 856 | card->rca = cmd.resp[0] >> 16; |
a00fc090 PO |
857 | |
858 | if (!host->ops->get_ro) { | |
859 | printk(KERN_WARNING "%s: host does not " | |
860 | "support reading read-only " | |
861 | "switch. assuming write-enable.\n", | |
862 | mmc_hostname(host)); | |
863 | } else { | |
864 | if (host->ops->get_ro(host)) | |
865 | mmc_card_set_readonly(card); | |
866 | } | |
867 | } | |
868 | } else { | |
335eadf2 PO |
869 | cmd.opcode = MMC_SET_RELATIVE_ADDR; |
870 | cmd.arg = card->rca << 16; | |
e9225176 | 871 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
335eadf2 PO |
872 | |
873 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); | |
874 | if (err != MMC_ERR_NONE) | |
875 | mmc_card_set_dead(card); | |
876 | } | |
1da177e4 LT |
877 | } |
878 | } | |
879 | ||
880 | static void mmc_read_csds(struct mmc_host *host) | |
881 | { | |
882 | struct mmc_card *card; | |
883 | ||
884 | list_for_each_entry(card, &host->cards, node) { | |
885 | struct mmc_command cmd; | |
886 | int err; | |
887 | ||
888 | if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT)) | |
889 | continue; | |
890 | ||
891 | cmd.opcode = MMC_SEND_CSD; | |
892 | cmd.arg = card->rca << 16; | |
e9225176 | 893 | cmd.flags = MMC_RSP_R2 | MMC_CMD_AC; |
1da177e4 LT |
894 | |
895 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); | |
896 | if (err != MMC_ERR_NONE) { | |
897 | mmc_card_set_dead(card); | |
898 | continue; | |
899 | } | |
900 | ||
901 | memcpy(card->raw_csd, cmd.resp, sizeof(card->raw_csd)); | |
902 | ||
903 | mmc_decode_csd(card); | |
904 | mmc_decode_cid(card); | |
905 | } | |
906 | } | |
907 | ||
b57c43ad PO |
908 | static void mmc_read_scrs(struct mmc_host *host) |
909 | { | |
910 | int err; | |
911 | struct mmc_card *card; | |
912 | ||
913 | struct mmc_request mrq; | |
914 | struct mmc_command cmd; | |
915 | struct mmc_data data; | |
916 | ||
917 | struct scatterlist sg; | |
918 | ||
919 | list_for_each_entry(card, &host->cards, node) { | |
920 | if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT)) | |
921 | continue; | |
922 | if (!mmc_card_sd(card)) | |
923 | continue; | |
924 | ||
925 | err = mmc_select_card(host, card); | |
926 | if (err != MMC_ERR_NONE) { | |
927 | mmc_card_set_dead(card); | |
928 | continue; | |
929 | } | |
930 | ||
931 | memset(&cmd, 0, sizeof(struct mmc_command)); | |
932 | ||
933 | cmd.opcode = MMC_APP_CMD; | |
934 | cmd.arg = card->rca << 16; | |
e9225176 | 935 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
b57c43ad PO |
936 | |
937 | err = mmc_wait_for_cmd(host, &cmd, 0); | |
938 | if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD)) { | |
939 | mmc_card_set_dead(card); | |
940 | continue; | |
941 | } | |
942 | ||
943 | memset(&cmd, 0, sizeof(struct mmc_command)); | |
944 | ||
945 | cmd.opcode = SD_APP_SEND_SCR; | |
946 | cmd.arg = 0; | |
e9225176 | 947 | cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; |
b57c43ad PO |
948 | |
949 | memset(&data, 0, sizeof(struct mmc_data)); | |
950 | ||
951 | data.timeout_ns = card->csd.tacc_ns * 10; | |
952 | data.timeout_clks = card->csd.tacc_clks * 10; | |
953 | data.blksz_bits = 3; | |
954 | data.blocks = 1; | |
955 | data.flags = MMC_DATA_READ; | |
956 | data.sg = &sg; | |
957 | data.sg_len = 1; | |
958 | ||
959 | memset(&mrq, 0, sizeof(struct mmc_request)); | |
960 | ||
961 | mrq.cmd = &cmd; | |
962 | mrq.data = &data; | |
963 | ||
964 | sg_init_one(&sg, (u8*)card->raw_scr, 8); | |
965 | ||
e781de44 PO |
966 | mmc_wait_for_req(host, &mrq); |
967 | ||
968 | if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) { | |
b57c43ad PO |
969 | mmc_card_set_dead(card); |
970 | continue; | |
971 | } | |
972 | ||
973 | card->raw_scr[0] = ntohl(card->raw_scr[0]); | |
974 | card->raw_scr[1] = ntohl(card->raw_scr[1]); | |
975 | ||
976 | mmc_decode_scr(card); | |
977 | } | |
978 | ||
979 | mmc_deselect_cards(host); | |
980 | } | |
981 | ||
1da177e4 LT |
982 | static unsigned int mmc_calculate_clock(struct mmc_host *host) |
983 | { | |
984 | struct mmc_card *card; | |
985 | unsigned int max_dtr = host->f_max; | |
986 | ||
987 | list_for_each_entry(card, &host->cards, node) | |
988 | if (!mmc_card_dead(card) && max_dtr > card->csd.max_dtr) | |
989 | max_dtr = card->csd.max_dtr; | |
990 | ||
920e70c5 RK |
991 | pr_debug("%s: selected %d.%03dMHz transfer rate\n", |
992 | mmc_hostname(host), | |
c6563178 | 993 | max_dtr / 1000000, (max_dtr / 1000) % 1000); |
1da177e4 LT |
994 | |
995 | return max_dtr; | |
996 | } | |
997 | ||
998 | /* | |
999 | * Check whether cards we already know about are still present. | |
1000 | * We do this by requesting status, and checking whether a card | |
1001 | * responds. | |
1002 | * | |
1003 | * A request for status does not cause a state change in data | |
1004 | * transfer mode. | |
1005 | */ | |
1006 | static void mmc_check_cards(struct mmc_host *host) | |
1007 | { | |
1008 | struct list_head *l, *n; | |
1009 | ||
1010 | mmc_deselect_cards(host); | |
1011 | ||
1012 | list_for_each_safe(l, n, &host->cards) { | |
1013 | struct mmc_card *card = mmc_list_to_card(l); | |
1014 | struct mmc_command cmd; | |
1015 | int err; | |
1016 | ||
1017 | cmd.opcode = MMC_SEND_STATUS; | |
1018 | cmd.arg = card->rca << 16; | |
e9225176 | 1019 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
1da177e4 LT |
1020 | |
1021 | err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES); | |
1022 | if (err == MMC_ERR_NONE) | |
1023 | continue; | |
1024 | ||
1025 | mmc_card_set_dead(card); | |
1026 | } | |
1027 | } | |
1028 | ||
1029 | static void mmc_setup(struct mmc_host *host) | |
1030 | { | |
1031 | if (host->ios.power_mode != MMC_POWER_ON) { | |
1032 | int err; | |
1033 | u32 ocr; | |
1034 | ||
a00fc090 | 1035 | host->mode = MMC_MODE_SD; |
335eadf2 | 1036 | |
1da177e4 LT |
1037 | mmc_power_up(host); |
1038 | mmc_idle_cards(host); | |
1039 | ||
a00fc090 | 1040 | err = mmc_send_app_op_cond(host, 0, &ocr); |
335eadf2 PO |
1041 | |
1042 | /* | |
a00fc090 PO |
1043 | * If we fail to detect any SD cards then try |
1044 | * searching for MMC cards. | |
335eadf2 | 1045 | */ |
a00fc090 PO |
1046 | if (err != MMC_ERR_NONE) { |
1047 | host->mode = MMC_MODE_MMC; | |
1048 | ||
1049 | err = mmc_send_op_cond(host, 0, &ocr); | |
335eadf2 PO |
1050 | if (err != MMC_ERR_NONE) |
1051 | return; | |
335eadf2 | 1052 | } |
1da177e4 LT |
1053 | |
1054 | host->ocr = mmc_select_voltage(host, ocr); | |
1055 | ||
1056 | /* | |
1057 | * Since we're changing the OCR value, we seem to | |
1058 | * need to tell some cards to go back to the idle | |
1059 | * state. We wait 1ms to give cards time to | |
1060 | * respond. | |
1061 | */ | |
1062 | if (host->ocr) | |
1063 | mmc_idle_cards(host); | |
1064 | } else { | |
1065 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; | |
1066 | host->ios.clock = host->f_min; | |
920e70c5 | 1067 | mmc_set_ios(host); |
1da177e4 LT |
1068 | |
1069 | /* | |
1070 | * We should remember the OCR mask from the existing | |
1071 | * cards, and detect the new cards OCR mask, combine | |
1072 | * the two and re-select the VDD. However, if we do | |
1073 | * change VDD, we should do an idle, and then do a | |
1074 | * full re-initialisation. We would need to notify | |
1075 | * drivers so that they can re-setup the cards as | |
1076 | * well, while keeping their queues at bay. | |
1077 | * | |
1078 | * For the moment, we take the easy way out - if the | |
1079 | * new cards don't like our currently selected VDD, | |
1080 | * they drop off the bus. | |
1081 | */ | |
1082 | } | |
1083 | ||
1084 | if (host->ocr == 0) | |
1085 | return; | |
1086 | ||
1087 | /* | |
1088 | * Send the selected OCR multiple times... until the cards | |
1089 | * all get the idea that they should be ready for CMD2. | |
1090 | * (My SanDisk card seems to need this.) | |
1091 | */ | |
335eadf2 PO |
1092 | if (host->mode == MMC_MODE_SD) |
1093 | mmc_send_app_op_cond(host, host->ocr, NULL); | |
1094 | else | |
1095 | mmc_send_op_cond(host, host->ocr, NULL); | |
1da177e4 LT |
1096 | |
1097 | mmc_discover_cards(host); | |
1098 | ||
1099 | /* | |
1100 | * Ok, now switch to push-pull mode. | |
1101 | */ | |
1102 | host->ios.bus_mode = MMC_BUSMODE_PUSHPULL; | |
920e70c5 | 1103 | mmc_set_ios(host); |
1da177e4 LT |
1104 | |
1105 | mmc_read_csds(host); | |
b57c43ad PO |
1106 | |
1107 | if (host->mode == MMC_MODE_SD) | |
1108 | mmc_read_scrs(host); | |
1da177e4 LT |
1109 | } |
1110 | ||
1111 | ||
1112 | /** | |
1113 | * mmc_detect_change - process change of state on a MMC socket | |
1114 | * @host: host which changed state. | |
8dc00335 | 1115 | * @delay: optional delay to wait before detection (jiffies) |
1da177e4 LT |
1116 | * |
1117 | * All we know is that card(s) have been inserted or removed | |
1118 | * from the socket(s). We don't know which socket or cards. | |
1119 | */ | |
8dc00335 | 1120 | void mmc_detect_change(struct mmc_host *host, unsigned long delay) |
1da177e4 | 1121 | { |
8dc00335 RP |
1122 | if (delay) |
1123 | schedule_delayed_work(&host->detect, delay); | |
1124 | else | |
1125 | schedule_work(&host->detect); | |
1da177e4 LT |
1126 | } |
1127 | ||
1128 | EXPORT_SYMBOL(mmc_detect_change); | |
1129 | ||
1130 | ||
1131 | static void mmc_rescan(void *data) | |
1132 | { | |
1133 | struct mmc_host *host = data; | |
1134 | struct list_head *l, *n; | |
1135 | ||
1136 | mmc_claim_host(host); | |
1137 | ||
1138 | if (host->ios.power_mode == MMC_POWER_ON) | |
1139 | mmc_check_cards(host); | |
1140 | ||
1141 | mmc_setup(host); | |
1142 | ||
1143 | if (!list_empty(&host->cards)) { | |
1144 | /* | |
1145 | * (Re-)calculate the fastest clock rate which the | |
1146 | * attached cards and the host support. | |
1147 | */ | |
1148 | host->ios.clock = mmc_calculate_clock(host); | |
920e70c5 | 1149 | mmc_set_ios(host); |
1da177e4 LT |
1150 | } |
1151 | ||
1152 | mmc_release_host(host); | |
1153 | ||
1154 | list_for_each_safe(l, n, &host->cards) { | |
1155 | struct mmc_card *card = mmc_list_to_card(l); | |
1156 | ||
1157 | /* | |
1158 | * If this is a new and good card, register it. | |
1159 | */ | |
1160 | if (!mmc_card_present(card) && !mmc_card_dead(card)) { | |
1161 | if (mmc_register_card(card)) | |
1162 | mmc_card_set_dead(card); | |
1163 | else | |
1164 | mmc_card_set_present(card); | |
1165 | } | |
1166 | ||
1167 | /* | |
1168 | * If this card is dead, destroy it. | |
1169 | */ | |
1170 | if (mmc_card_dead(card)) { | |
1171 | list_del(&card->node); | |
1172 | mmc_remove_card(card); | |
1173 | } | |
1174 | } | |
1175 | ||
1176 | /* | |
1177 | * If we discover that there are no cards on the | |
1178 | * bus, turn off the clock and power down. | |
1179 | */ | |
1180 | if (list_empty(&host->cards)) | |
1181 | mmc_power_off(host); | |
1182 | } | |
1183 | ||
1184 | ||
1185 | /** | |
1186 | * mmc_alloc_host - initialise the per-host structure. | |
1187 | * @extra: sizeof private data structure | |
1188 | * @dev: pointer to host device model structure | |
1189 | * | |
1190 | * Initialise the per-host structure. | |
1191 | */ | |
1192 | struct mmc_host *mmc_alloc_host(int extra, struct device *dev) | |
1193 | { | |
1194 | struct mmc_host *host; | |
1195 | ||
00b137cf | 1196 | host = mmc_alloc_host_sysfs(extra, dev); |
1da177e4 | 1197 | if (host) { |
1da177e4 LT |
1198 | spin_lock_init(&host->lock); |
1199 | init_waitqueue_head(&host->wq); | |
1200 | INIT_LIST_HEAD(&host->cards); | |
1201 | INIT_WORK(&host->detect, mmc_rescan, host); | |
1202 | ||
1da177e4 LT |
1203 | /* |
1204 | * By default, hosts do not support SGIO or large requests. | |
1205 | * They have to set these according to their abilities. | |
1206 | */ | |
1207 | host->max_hw_segs = 1; | |
1208 | host->max_phys_segs = 1; | |
1209 | host->max_sectors = 1 << (PAGE_CACHE_SHIFT - 9); | |
1210 | host->max_seg_size = PAGE_CACHE_SIZE; | |
1211 | } | |
1212 | ||
1213 | return host; | |
1214 | } | |
1215 | ||
1216 | EXPORT_SYMBOL(mmc_alloc_host); | |
1217 | ||
1218 | /** | |
1219 | * mmc_add_host - initialise host hardware | |
1220 | * @host: mmc host | |
1221 | */ | |
1222 | int mmc_add_host(struct mmc_host *host) | |
1223 | { | |
00b137cf | 1224 | int ret; |
1da177e4 | 1225 | |
00b137cf RK |
1226 | ret = mmc_add_host_sysfs(host); |
1227 | if (ret == 0) { | |
1228 | mmc_power_off(host); | |
8dc00335 | 1229 | mmc_detect_change(host, 0); |
00b137cf | 1230 | } |
1da177e4 | 1231 | |
00b137cf | 1232 | return ret; |
1da177e4 LT |
1233 | } |
1234 | ||
1235 | EXPORT_SYMBOL(mmc_add_host); | |
1236 | ||
1237 | /** | |
1238 | * mmc_remove_host - remove host hardware | |
1239 | * @host: mmc host | |
1240 | * | |
1241 | * Unregister and remove all cards associated with this host, | |
1242 | * and power down the MMC bus. | |
1243 | */ | |
1244 | void mmc_remove_host(struct mmc_host *host) | |
1245 | { | |
1246 | struct list_head *l, *n; | |
1247 | ||
1248 | list_for_each_safe(l, n, &host->cards) { | |
1249 | struct mmc_card *card = mmc_list_to_card(l); | |
1250 | ||
1251 | mmc_remove_card(card); | |
1252 | } | |
1253 | ||
1254 | mmc_power_off(host); | |
00b137cf | 1255 | mmc_remove_host_sysfs(host); |
1da177e4 LT |
1256 | } |
1257 | ||
1258 | EXPORT_SYMBOL(mmc_remove_host); | |
1259 | ||
1260 | /** | |
1261 | * mmc_free_host - free the host structure | |
1262 | * @host: mmc host | |
1263 | * | |
1264 | * Free the host once all references to it have been dropped. | |
1265 | */ | |
1266 | void mmc_free_host(struct mmc_host *host) | |
1267 | { | |
1268 | flush_scheduled_work(); | |
00b137cf | 1269 | mmc_free_host_sysfs(host); |
1da177e4 LT |
1270 | } |
1271 | ||
1272 | EXPORT_SYMBOL(mmc_free_host); | |
1273 | ||
1274 | #ifdef CONFIG_PM | |
1275 | ||
1276 | /** | |
1277 | * mmc_suspend_host - suspend a host | |
1278 | * @host: mmc host | |
1279 | * @state: suspend mode (PM_SUSPEND_xxx) | |
1280 | */ | |
e5378ca8 | 1281 | int mmc_suspend_host(struct mmc_host *host, pm_message_t state) |
1da177e4 LT |
1282 | { |
1283 | mmc_claim_host(host); | |
1284 | mmc_deselect_cards(host); | |
1285 | mmc_power_off(host); | |
1286 | mmc_release_host(host); | |
1287 | ||
1288 | return 0; | |
1289 | } | |
1290 | ||
1291 | EXPORT_SYMBOL(mmc_suspend_host); | |
1292 | ||
1293 | /** | |
1294 | * mmc_resume_host - resume a previously suspended host | |
1295 | * @host: mmc host | |
1296 | */ | |
1297 | int mmc_resume_host(struct mmc_host *host) | |
1298 | { | |
896937ad | 1299 | mmc_rescan(host); |
1da177e4 LT |
1300 | |
1301 | return 0; | |
1302 | } | |
1303 | ||
1304 | EXPORT_SYMBOL(mmc_resume_host); | |
1305 | ||
1306 | #endif | |
1307 | ||
1308 | MODULE_LICENSE("GPL"); |