Commit | Line | Data |
---|---|---|
7ea239d9 | 1 | /* |
70f10482 | 2 | * linux/drivers/mmc/core/mmc.c |
7ea239d9 PO |
3 | * |
4 | * Copyright (C) 2003-2004 Russell King, All Rights Reserved. | |
5 | * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved. | |
6 | * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved. | |
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 | ||
13 | #include <linux/err.h> | |
5a0e3ad6 | 14 | #include <linux/slab.h> |
7ea239d9 PO |
15 | |
16 | #include <linux/mmc/host.h> | |
17 | #include <linux/mmc/card.h> | |
18 | #include <linux/mmc/mmc.h> | |
19 | ||
20 | #include "core.h" | |
4101c16a | 21 | #include "bus.h" |
7ea239d9 PO |
22 | #include "mmc_ops.h" |
23 | ||
24 | static const unsigned int tran_exp[] = { | |
25 | 10000, 100000, 1000000, 10000000, | |
26 | 0, 0, 0, 0 | |
27 | }; | |
28 | ||
29 | static const unsigned char tran_mant[] = { | |
30 | 0, 10, 12, 13, 15, 20, 25, 30, | |
31 | 35, 40, 45, 50, 55, 60, 70, 80, | |
32 | }; | |
33 | ||
34 | static const unsigned int tacc_exp[] = { | |
35 | 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, | |
36 | }; | |
37 | ||
38 | static const unsigned int tacc_mant[] = { | |
39 | 0, 10, 12, 13, 15, 20, 25, 30, | |
40 | 35, 40, 45, 50, 55, 60, 70, 80, | |
41 | }; | |
42 | ||
43 | #define UNSTUFF_BITS(resp,start,size) \ | |
44 | ({ \ | |
45 | const int __size = size; \ | |
46 | const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \ | |
47 | const int __off = 3 - ((start) / 32); \ | |
48 | const int __shft = (start) & 31; \ | |
49 | u32 __res; \ | |
50 | \ | |
51 | __res = resp[__off] >> __shft; \ | |
52 | if (__size + __shft > 32) \ | |
53 | __res |= resp[__off-1] << ((32 - __shft) % 32); \ | |
54 | __res & __mask; \ | |
55 | }) | |
56 | ||
57 | /* | |
58 | * Given the decoded CSD structure, decode the raw CID to our CID structure. | |
59 | */ | |
bd766312 | 60 | static int mmc_decode_cid(struct mmc_card *card) |
7ea239d9 PO |
61 | { |
62 | u32 *resp = card->raw_cid; | |
63 | ||
64 | /* | |
65 | * The selection of the format here is based upon published | |
66 | * specs from sandisk and from what people have reported. | |
67 | */ | |
68 | switch (card->csd.mmca_vsn) { | |
69 | case 0: /* MMC v1.0 - v1.2 */ | |
70 | case 1: /* MMC v1.4 */ | |
71 | card->cid.manfid = UNSTUFF_BITS(resp, 104, 24); | |
72 | card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); | |
73 | card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); | |
74 | card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); | |
75 | card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); | |
76 | card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); | |
77 | card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8); | |
78 | card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8); | |
79 | card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4); | |
80 | card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4); | |
81 | card->cid.serial = UNSTUFF_BITS(resp, 16, 24); | |
82 | card->cid.month = UNSTUFF_BITS(resp, 12, 4); | |
83 | card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997; | |
84 | break; | |
85 | ||
86 | case 2: /* MMC v2.0 - v2.2 */ | |
87 | case 3: /* MMC v3.1 - v3.3 */ | |
88 | case 4: /* MMC v4 */ | |
89 | card->cid.manfid = UNSTUFF_BITS(resp, 120, 8); | |
90 | card->cid.oemid = UNSTUFF_BITS(resp, 104, 16); | |
91 | card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8); | |
92 | card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8); | |
93 | card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8); | |
94 | card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8); | |
95 | card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8); | |
96 | card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8); | |
97 | card->cid.serial = UNSTUFF_BITS(resp, 16, 32); | |
98 | card->cid.month = UNSTUFF_BITS(resp, 12, 4); | |
99 | card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997; | |
100 | break; | |
101 | ||
102 | default: | |
facba917 | 103 | printk(KERN_ERR "%s: card has unknown MMCA version %d\n", |
7ea239d9 | 104 | mmc_hostname(card->host), card->csd.mmca_vsn); |
bd766312 | 105 | return -EINVAL; |
7ea239d9 | 106 | } |
bd766312 PO |
107 | |
108 | return 0; | |
7ea239d9 PO |
109 | } |
110 | ||
111 | /* | |
112 | * Given a 128-bit response, decode to our card CSD structure. | |
113 | */ | |
bd766312 | 114 | static int mmc_decode_csd(struct mmc_card *card) |
7ea239d9 PO |
115 | { |
116 | struct mmc_csd *csd = &card->csd; | |
6da24b78 | 117 | unsigned int e, m; |
7ea239d9 PO |
118 | u32 *resp = card->raw_csd; |
119 | ||
120 | /* | |
121 | * We only understand CSD structure v1.1 and v1.2. | |
122 | * v1.2 has extra information in bits 15, 11 and 10. | |
6da24b78 | 123 | * We also support eMMC v4.4 & v4.41. |
7ea239d9 | 124 | */ |
6da24b78 KP |
125 | csd->structure = UNSTUFF_BITS(resp, 126, 2); |
126 | if (csd->structure == 0) { | |
facba917 | 127 | printk(KERN_ERR "%s: unrecognised CSD structure version %d\n", |
6da24b78 | 128 | mmc_hostname(card->host), csd->structure); |
bd766312 | 129 | return -EINVAL; |
7ea239d9 PO |
130 | } |
131 | ||
132 | csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4); | |
133 | m = UNSTUFF_BITS(resp, 115, 4); | |
134 | e = UNSTUFF_BITS(resp, 112, 3); | |
135 | csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10; | |
136 | csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100; | |
137 | ||
138 | m = UNSTUFF_BITS(resp, 99, 4); | |
139 | e = UNSTUFF_BITS(resp, 96, 3); | |
140 | csd->max_dtr = tran_exp[e] * tran_mant[m]; | |
141 | csd->cmdclass = UNSTUFF_BITS(resp, 84, 12); | |
142 | ||
143 | e = UNSTUFF_BITS(resp, 47, 3); | |
144 | m = UNSTUFF_BITS(resp, 62, 12); | |
145 | csd->capacity = (1 + m) << (e + 2); | |
146 | ||
147 | csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4); | |
148 | csd->read_partial = UNSTUFF_BITS(resp, 79, 1); | |
149 | csd->write_misalign = UNSTUFF_BITS(resp, 78, 1); | |
150 | csd->read_misalign = UNSTUFF_BITS(resp, 77, 1); | |
151 | csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3); | |
152 | csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); | |
153 | csd->write_partial = UNSTUFF_BITS(resp, 21, 1); | |
bd766312 PO |
154 | |
155 | return 0; | |
7ea239d9 PO |
156 | } |
157 | ||
158 | /* | |
89a73cf5 | 159 | * Read and decode extended CSD. |
7ea239d9 | 160 | */ |
89a73cf5 | 161 | static int mmc_read_ext_csd(struct mmc_card *card) |
7ea239d9 PO |
162 | { |
163 | int err; | |
164 | u8 *ext_csd; | |
165 | ||
166 | BUG_ON(!card); | |
167 | ||
7ea239d9 | 168 | if (card->csd.mmca_vsn < CSD_SPEC_VER_4) |
17b0429d | 169 | return 0; |
7ea239d9 PO |
170 | |
171 | /* | |
172 | * As the ext_csd is so large and mostly unused, we don't store the | |
173 | * raw block in mmc_card. | |
174 | */ | |
175 | ext_csd = kmalloc(512, GFP_KERNEL); | |
176 | if (!ext_csd) { | |
177 | printk(KERN_ERR "%s: could not allocate a buffer to " | |
adf66a0d | 178 | "receive the ext_csd.\n", mmc_hostname(card->host)); |
17b0429d | 179 | return -ENOMEM; |
7ea239d9 PO |
180 | } |
181 | ||
182 | err = mmc_send_ext_csd(card, ext_csd); | |
17b0429d | 183 | if (err) { |
d08ebedd WM |
184 | /* If the host or the card can't do the switch, |
185 | * fail more gracefully. */ | |
186 | if ((err != -EINVAL) | |
187 | && (err != -ENOSYS) | |
188 | && (err != -EFAULT)) | |
adf66a0d PO |
189 | goto out; |
190 | ||
7ea239d9 PO |
191 | /* |
192 | * High capacity cards should have this "magic" size | |
193 | * stored in their CSD. | |
194 | */ | |
195 | if (card->csd.capacity == (4096 * 512)) { | |
196 | printk(KERN_ERR "%s: unable to read EXT_CSD " | |
197 | "on a possible high capacity card. " | |
198 | "Card will be ignored.\n", | |
199 | mmc_hostname(card->host)); | |
200 | } else { | |
201 | printk(KERN_WARNING "%s: unable to read " | |
202 | "EXT_CSD, performance might " | |
203 | "suffer.\n", | |
204 | mmc_hostname(card->host)); | |
17b0429d | 205 | err = 0; |
7ea239d9 | 206 | } |
adf66a0d | 207 | |
7ea239d9 PO |
208 | goto out; |
209 | } | |
210 | ||
6da24b78 KP |
211 | /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */ |
212 | if (card->csd.structure == 3) { | |
213 | int ext_csd_struct = ext_csd[EXT_CSD_STRUCTURE]; | |
214 | if (ext_csd_struct > 2) { | |
215 | printk(KERN_ERR "%s: unrecognised EXT_CSD structure " | |
216 | "version %d\n", mmc_hostname(card->host), | |
217 | ext_csd_struct); | |
218 | err = -EINVAL; | |
219 | goto out; | |
220 | } | |
221 | } | |
222 | ||
b1ebe384 | 223 | card->ext_csd.rev = ext_csd[EXT_CSD_REV]; |
11723ab1 | 224 | if (card->ext_csd.rev > 5) { |
6da24b78 KP |
225 | printk(KERN_ERR "%s: unrecognised EXT_CSD revision %d\n", |
226 | mmc_hostname(card->host), card->ext_csd.rev); | |
00cedfa6 FM |
227 | err = -EINVAL; |
228 | goto out; | |
d7604d76 PO |
229 | } |
230 | ||
b1ebe384 | 231 | if (card->ext_csd.rev >= 2) { |
d7604d76 PO |
232 | card->ext_csd.sectors = |
233 | ext_csd[EXT_CSD_SEC_CNT + 0] << 0 | | |
234 | ext_csd[EXT_CSD_SEC_CNT + 1] << 8 | | |
235 | ext_csd[EXT_CSD_SEC_CNT + 2] << 16 | | |
236 | ext_csd[EXT_CSD_SEC_CNT + 3] << 24; | |
fc8a0985 HP |
237 | |
238 | /* Cards with density > 2GiB are sector addressed */ | |
239 | if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512) | |
d7604d76 PO |
240 | mmc_card_set_blockaddr(card); |
241 | } | |
7ea239d9 | 242 | |
7198f3c9 | 243 | switch (ext_csd[EXT_CSD_CARD_TYPE] & EXT_CSD_CARD_TYPE_MASK) { |
7ea239d9 PO |
244 | case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26: |
245 | card->ext_csd.hs_max_dtr = 52000000; | |
246 | break; | |
247 | case EXT_CSD_CARD_TYPE_26: | |
248 | card->ext_csd.hs_max_dtr = 26000000; | |
249 | break; | |
250 | default: | |
251 | /* MMC v4 spec says this cannot happen */ | |
252 | printk(KERN_WARNING "%s: card is mmc v4 but doesn't " | |
253 | "support any high-speed modes.\n", | |
254 | mmc_hostname(card->host)); | |
7ea239d9 PO |
255 | } |
256 | ||
b1ebe384 JL |
257 | if (card->ext_csd.rev >= 3) { |
258 | u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT]; | |
259 | ||
260 | /* Sleep / awake timeout in 100ns units */ | |
261 | if (sa_shift > 0 && sa_shift <= 0x17) | |
262 | card->ext_csd.sa_timeout = | |
263 | 1 << ext_csd[EXT_CSD_S_A_TIMEOUT]; | |
264 | } | |
265 | ||
7ea239d9 PO |
266 | out: |
267 | kfree(ext_csd); | |
268 | ||
269 | return err; | |
270 | } | |
271 | ||
51ec92e2 PO |
272 | MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1], |
273 | card->raw_cid[2], card->raw_cid[3]); | |
274 | MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1], | |
275 | card->raw_csd[2], card->raw_csd[3]); | |
276 | MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year); | |
277 | MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev); | |
278 | MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev); | |
279 | MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid); | |
280 | MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name); | |
281 | MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid); | |
282 | MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial); | |
283 | ||
284 | static struct attribute *mmc_std_attrs[] = { | |
285 | &dev_attr_cid.attr, | |
286 | &dev_attr_csd.attr, | |
287 | &dev_attr_date.attr, | |
288 | &dev_attr_fwrev.attr, | |
289 | &dev_attr_hwrev.attr, | |
290 | &dev_attr_manfid.attr, | |
291 | &dev_attr_name.attr, | |
292 | &dev_attr_oemid.attr, | |
293 | &dev_attr_serial.attr, | |
294 | NULL, | |
295 | }; | |
296 | ||
297 | static struct attribute_group mmc_std_attr_group = { | |
298 | .attrs = mmc_std_attrs, | |
299 | }; | |
300 | ||
a4dbd674 | 301 | static const struct attribute_group *mmc_attr_groups[] = { |
51ec92e2 PO |
302 | &mmc_std_attr_group, |
303 | NULL, | |
304 | }; | |
305 | ||
306 | static struct device_type mmc_type = { | |
307 | .groups = mmc_attr_groups, | |
308 | }; | |
309 | ||
7ea239d9 | 310 | /* |
6abaa0c9 PO |
311 | * Handle the detection and initialisation of a card. |
312 | * | |
8769392b | 313 | * In the case of a resume, "oldcard" will contain the card |
6abaa0c9 | 314 | * we're trying to reinitialise. |
7ea239d9 | 315 | */ |
8c75deae | 316 | static int mmc_init_card(struct mmc_host *host, u32 ocr, |
6abaa0c9 | 317 | struct mmc_card *oldcard) |
7ea239d9 PO |
318 | { |
319 | struct mmc_card *card; | |
320 | int err; | |
321 | u32 cid[4]; | |
322 | unsigned int max_dtr; | |
323 | ||
324 | BUG_ON(!host); | |
d84075c8 | 325 | WARN_ON(!host->claimed); |
7ea239d9 | 326 | |
7ea239d9 PO |
327 | /* |
328 | * Since we're changing the OCR value, we seem to | |
329 | * need to tell some cards to go back to the idle | |
330 | * state. We wait 1ms to give cards time to | |
331 | * respond. | |
332 | */ | |
333 | mmc_go_idle(host); | |
334 | ||
335 | /* The extra bit indicates that we support high capacity */ | |
6abaa0c9 | 336 | err = mmc_send_op_cond(host, ocr | (1 << 30), NULL); |
17b0429d | 337 | if (err) |
6abaa0c9 | 338 | goto err; |
7ea239d9 | 339 | |
af517150 DB |
340 | /* |
341 | * For SPI, enable CRC as appropriate. | |
342 | */ | |
343 | if (mmc_host_is_spi(host)) { | |
344 | err = mmc_spi_set_crc(host, use_spi_crc); | |
345 | if (err) | |
346 | goto err; | |
347 | } | |
348 | ||
7ea239d9 PO |
349 | /* |
350 | * Fetch CID from card. | |
351 | */ | |
af517150 DB |
352 | if (mmc_host_is_spi(host)) |
353 | err = mmc_send_cid(host, cid); | |
354 | else | |
355 | err = mmc_all_send_cid(host, cid); | |
17b0429d | 356 | if (err) |
7ea239d9 PO |
357 | goto err; |
358 | ||
6abaa0c9 | 359 | if (oldcard) { |
adf66a0d PO |
360 | if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) { |
361 | err = -ENOENT; | |
6abaa0c9 | 362 | goto err; |
adf66a0d | 363 | } |
6abaa0c9 PO |
364 | |
365 | card = oldcard; | |
366 | } else { | |
367 | /* | |
368 | * Allocate card structure. | |
369 | */ | |
51ec92e2 | 370 | card = mmc_alloc_card(host, &mmc_type); |
adf66a0d PO |
371 | if (IS_ERR(card)) { |
372 | err = PTR_ERR(card); | |
6abaa0c9 | 373 | goto err; |
adf66a0d | 374 | } |
7ea239d9 | 375 | |
6abaa0c9 PO |
376 | card->type = MMC_TYPE_MMC; |
377 | card->rca = 1; | |
378 | memcpy(card->raw_cid, cid, sizeof(card->raw_cid)); | |
379 | } | |
7ea239d9 PO |
380 | |
381 | /* | |
af517150 | 382 | * For native busses: set card RCA and quit open drain mode. |
7ea239d9 | 383 | */ |
af517150 DB |
384 | if (!mmc_host_is_spi(host)) { |
385 | err = mmc_set_relative_addr(card); | |
386 | if (err) | |
387 | goto free_card; | |
7ea239d9 | 388 | |
af517150 DB |
389 | mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); |
390 | } | |
7ea239d9 | 391 | |
6abaa0c9 PO |
392 | if (!oldcard) { |
393 | /* | |
394 | * Fetch CSD from card. | |
395 | */ | |
396 | err = mmc_send_csd(card, card->raw_csd); | |
17b0429d | 397 | if (err) |
6abaa0c9 | 398 | goto free_card; |
7ea239d9 | 399 | |
bd766312 | 400 | err = mmc_decode_csd(card); |
adf66a0d | 401 | if (err) |
bd766312 PO |
402 | goto free_card; |
403 | err = mmc_decode_cid(card); | |
adf66a0d | 404 | if (err) |
bd766312 | 405 | goto free_card; |
6abaa0c9 | 406 | } |
7ea239d9 PO |
407 | |
408 | /* | |
89a73cf5 | 409 | * Select card, as all following commands rely on that. |
7ea239d9 | 410 | */ |
af517150 DB |
411 | if (!mmc_host_is_spi(host)) { |
412 | err = mmc_select_card(card); | |
413 | if (err) | |
414 | goto free_card; | |
415 | } | |
7ea239d9 | 416 | |
6abaa0c9 PO |
417 | if (!oldcard) { |
418 | /* | |
af517150 | 419 | * Fetch and process extended CSD. |
6abaa0c9 PO |
420 | */ |
421 | err = mmc_read_ext_csd(card); | |
17b0429d | 422 | if (err) |
6abaa0c9 PO |
423 | goto free_card; |
424 | } | |
7ea239d9 | 425 | |
89a73cf5 PO |
426 | /* |
427 | * Activate high speed (if supported) | |
428 | */ | |
429 | if ((card->ext_csd.hs_max_dtr != 0) && | |
430 | (host->caps & MMC_CAP_MMC_HIGHSPEED)) { | |
431 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | |
432 | EXT_CSD_HS_TIMING, 1); | |
ef0b27d4 | 433 | if (err && err != -EBADMSG) |
89a73cf5 PO |
434 | goto free_card; |
435 | ||
ef0b27d4 AH |
436 | if (err) { |
437 | printk(KERN_WARNING "%s: switch to highspeed failed\n", | |
438 | mmc_hostname(card->host)); | |
439 | err = 0; | |
440 | } else { | |
441 | mmc_card_set_highspeed(card); | |
442 | mmc_set_timing(card->host, MMC_TIMING_MMC_HS); | |
443 | } | |
89a73cf5 PO |
444 | } |
445 | ||
7ea239d9 PO |
446 | /* |
447 | * Compute bus speed. | |
448 | */ | |
449 | max_dtr = (unsigned int)-1; | |
450 | ||
451 | if (mmc_card_highspeed(card)) { | |
452 | if (max_dtr > card->ext_csd.hs_max_dtr) | |
453 | max_dtr = card->ext_csd.hs_max_dtr; | |
454 | } else if (max_dtr > card->csd.max_dtr) { | |
455 | max_dtr = card->csd.max_dtr; | |
456 | } | |
457 | ||
458 | mmc_set_clock(host, max_dtr); | |
459 | ||
89a73cf5 PO |
460 | /* |
461 | * Activate wide bus (if supported). | |
462 | */ | |
463 | if ((card->csd.mmca_vsn >= CSD_SPEC_VER_4) && | |
b30f8af3 JL |
464 | (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) { |
465 | unsigned ext_csd_bit, bus_width; | |
466 | ||
467 | if (host->caps & MMC_CAP_8_BIT_DATA) { | |
468 | ext_csd_bit = EXT_CSD_BUS_WIDTH_8; | |
469 | bus_width = MMC_BUS_WIDTH_8; | |
470 | } else { | |
471 | ext_csd_bit = EXT_CSD_BUS_WIDTH_4; | |
472 | bus_width = MMC_BUS_WIDTH_4; | |
473 | } | |
474 | ||
89a73cf5 | 475 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
b30f8af3 JL |
476 | EXT_CSD_BUS_WIDTH, ext_csd_bit); |
477 | ||
ef0b27d4 | 478 | if (err && err != -EBADMSG) |
89a73cf5 PO |
479 | goto free_card; |
480 | ||
ef0b27d4 AH |
481 | if (err) { |
482 | printk(KERN_WARNING "%s: switch to bus width %d " | |
483 | "failed\n", mmc_hostname(card->host), | |
484 | 1 << bus_width); | |
485 | err = 0; | |
486 | } else { | |
487 | mmc_set_bus_width(card->host, bus_width); | |
488 | } | |
89a73cf5 PO |
489 | } |
490 | ||
6abaa0c9 PO |
491 | if (!oldcard) |
492 | host->card = card; | |
493 | ||
17b0429d | 494 | return 0; |
6abaa0c9 PO |
495 | |
496 | free_card: | |
497 | if (!oldcard) | |
498 | mmc_remove_card(card); | |
499 | err: | |
500 | ||
adf66a0d | 501 | return err; |
6abaa0c9 PO |
502 | } |
503 | ||
504 | /* | |
505 | * Host is being removed. Free up the current card. | |
506 | */ | |
507 | static void mmc_remove(struct mmc_host *host) | |
508 | { | |
509 | BUG_ON(!host); | |
510 | BUG_ON(!host->card); | |
511 | ||
512 | mmc_remove_card(host->card); | |
513 | host->card = NULL; | |
514 | } | |
515 | ||
516 | /* | |
517 | * Card detection callback from host. | |
518 | */ | |
519 | static void mmc_detect(struct mmc_host *host) | |
520 | { | |
521 | int err; | |
522 | ||
523 | BUG_ON(!host); | |
524 | BUG_ON(!host->card); | |
525 | ||
526 | mmc_claim_host(host); | |
527 | ||
528 | /* | |
529 | * Just check if our card has been removed. | |
530 | */ | |
531 | err = mmc_send_status(host->card, NULL); | |
532 | ||
533 | mmc_release_host(host); | |
534 | ||
17b0429d | 535 | if (err) { |
4101c16a | 536 | mmc_remove(host); |
6abaa0c9 PO |
537 | |
538 | mmc_claim_host(host); | |
539 | mmc_detach_bus(host); | |
540 | mmc_release_host(host); | |
541 | } | |
542 | } | |
543 | ||
6abaa0c9 PO |
544 | /* |
545 | * Suspend callback from host. | |
546 | */ | |
95cdfb72 | 547 | static int mmc_suspend(struct mmc_host *host) |
6abaa0c9 PO |
548 | { |
549 | BUG_ON(!host); | |
550 | BUG_ON(!host->card); | |
7ea239d9 | 551 | |
6abaa0c9 | 552 | mmc_claim_host(host); |
af517150 DB |
553 | if (!mmc_host_is_spi(host)) |
554 | mmc_deselect_cards(host); | |
6abaa0c9 | 555 | host->card->state &= ~MMC_STATE_HIGHSPEED; |
7ea239d9 | 556 | mmc_release_host(host); |
95cdfb72 NP |
557 | |
558 | return 0; | |
6abaa0c9 | 559 | } |
7ea239d9 | 560 | |
6abaa0c9 PO |
561 | /* |
562 | * Resume callback from host. | |
563 | * | |
564 | * This function tries to determine if the same card is still present | |
565 | * and, if so, restore all state to it. | |
566 | */ | |
95cdfb72 | 567 | static int mmc_resume(struct mmc_host *host) |
6abaa0c9 PO |
568 | { |
569 | int err; | |
570 | ||
571 | BUG_ON(!host); | |
572 | BUG_ON(!host->card); | |
573 | ||
574 | mmc_claim_host(host); | |
8c75deae | 575 | err = mmc_init_card(host, host->ocr, host->card); |
2986d0bf PO |
576 | mmc_release_host(host); |
577 | ||
95cdfb72 | 578 | return err; |
6abaa0c9 PO |
579 | } |
580 | ||
eae1aeee AH |
581 | static void mmc_power_restore(struct mmc_host *host) |
582 | { | |
583 | host->card->state &= ~MMC_STATE_HIGHSPEED; | |
584 | mmc_claim_host(host); | |
585 | mmc_init_card(host, host->ocr, host->card); | |
586 | mmc_release_host(host); | |
587 | } | |
588 | ||
b1ebe384 JL |
589 | static int mmc_sleep(struct mmc_host *host) |
590 | { | |
591 | struct mmc_card *card = host->card; | |
592 | int err = -ENOSYS; | |
593 | ||
594 | if (card && card->ext_csd.rev >= 3) { | |
595 | err = mmc_card_sleepawake(host, 1); | |
596 | if (err < 0) | |
597 | pr_debug("%s: Error %d while putting card into sleep", | |
598 | mmc_hostname(host), err); | |
599 | } | |
600 | ||
601 | return err; | |
602 | } | |
603 | ||
604 | static int mmc_awake(struct mmc_host *host) | |
605 | { | |
606 | struct mmc_card *card = host->card; | |
607 | int err = -ENOSYS; | |
608 | ||
609 | if (card && card->ext_csd.rev >= 3) { | |
610 | err = mmc_card_sleepawake(host, 0); | |
611 | if (err < 0) | |
612 | pr_debug("%s: Error %d while awaking sleeping card", | |
613 | mmc_hostname(host), err); | |
614 | } | |
615 | ||
616 | return err; | |
617 | } | |
618 | ||
6abaa0c9 | 619 | static const struct mmc_bus_ops mmc_ops = { |
b1ebe384 JL |
620 | .awake = mmc_awake, |
621 | .sleep = mmc_sleep, | |
9feae246 AH |
622 | .remove = mmc_remove, |
623 | .detect = mmc_detect, | |
624 | .suspend = NULL, | |
625 | .resume = NULL, | |
eae1aeee | 626 | .power_restore = mmc_power_restore, |
9feae246 AH |
627 | }; |
628 | ||
629 | static const struct mmc_bus_ops mmc_ops_unsafe = { | |
b1ebe384 JL |
630 | .awake = mmc_awake, |
631 | .sleep = mmc_sleep, | |
6abaa0c9 PO |
632 | .remove = mmc_remove, |
633 | .detect = mmc_detect, | |
634 | .suspend = mmc_suspend, | |
635 | .resume = mmc_resume, | |
eae1aeee | 636 | .power_restore = mmc_power_restore, |
6abaa0c9 PO |
637 | }; |
638 | ||
9feae246 AH |
639 | static void mmc_attach_bus_ops(struct mmc_host *host) |
640 | { | |
641 | const struct mmc_bus_ops *bus_ops; | |
642 | ||
bd68e083 | 643 | if (host->caps & MMC_CAP_NONREMOVABLE || !mmc_assume_removable) |
9feae246 AH |
644 | bus_ops = &mmc_ops_unsafe; |
645 | else | |
646 | bus_ops = &mmc_ops; | |
647 | mmc_attach_bus(host, bus_ops); | |
648 | } | |
649 | ||
6abaa0c9 PO |
650 | /* |
651 | * Starting point for MMC card init. | |
652 | */ | |
653 | int mmc_attach_mmc(struct mmc_host *host, u32 ocr) | |
654 | { | |
655 | int err; | |
656 | ||
657 | BUG_ON(!host); | |
d84075c8 | 658 | WARN_ON(!host->claimed); |
6abaa0c9 | 659 | |
9feae246 | 660 | mmc_attach_bus_ops(host); |
6abaa0c9 | 661 | |
af517150 DB |
662 | /* |
663 | * We need to get OCR a different way for SPI. | |
664 | */ | |
665 | if (mmc_host_is_spi(host)) { | |
666 | err = mmc_spi_read_ocr(host, 1, &ocr); | |
667 | if (err) | |
668 | goto err; | |
669 | } | |
670 | ||
6abaa0c9 PO |
671 | /* |
672 | * Sanity check the voltages that the card claims to | |
673 | * support. | |
674 | */ | |
675 | if (ocr & 0x7F) { | |
676 | printk(KERN_WARNING "%s: card claims to support voltages " | |
677 | "below the defined range. These will be ignored.\n", | |
678 | mmc_hostname(host)); | |
679 | ocr &= ~0x7F; | |
680 | } | |
681 | ||
682 | host->ocr = mmc_select_voltage(host, ocr); | |
683 | ||
684 | /* | |
685 | * Can we support the voltage of the card? | |
686 | */ | |
109b5bed PO |
687 | if (!host->ocr) { |
688 | err = -EINVAL; | |
6abaa0c9 | 689 | goto err; |
109b5bed | 690 | } |
6abaa0c9 PO |
691 | |
692 | /* | |
693 | * Detect and init the card. | |
694 | */ | |
8c75deae | 695 | err = mmc_init_card(host, host->ocr, NULL); |
17b0429d | 696 | if (err) |
6abaa0c9 PO |
697 | goto err; |
698 | ||
699 | mmc_release_host(host); | |
700 | ||
4101c16a | 701 | err = mmc_add_card(host->card); |
7ea239d9 | 702 | if (err) |
2986d0bf | 703 | goto remove_card; |
7ea239d9 PO |
704 | |
705 | return 0; | |
706 | ||
2986d0bf | 707 | remove_card: |
6abaa0c9 | 708 | mmc_remove_card(host->card); |
7ea239d9 | 709 | host->card = NULL; |
2986d0bf | 710 | mmc_claim_host(host); |
7ea239d9 PO |
711 | err: |
712 | mmc_detach_bus(host); | |
713 | mmc_release_host(host); | |
714 | ||
109b5bed PO |
715 | printk(KERN_ERR "%s: error %d whilst initialising MMC card\n", |
716 | mmc_hostname(host), err); | |
717 | ||
adf66a0d | 718 | return err; |
7ea239d9 | 719 | } |