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