blob: cd0eb4ef2f744845370b2c6c0e640b661efe7d03 [file] [log] [blame]
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/core/sd.c
Pierre Ossman7ea239d2006-12-31 00:11:32 +01003 *
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
6 * Copyright (C) 2005-2007 Pierre Ossman, 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Pierre Ossman7ea239d2006-12-31 00:11:32 +010015
16#include <linux/mmc/host.h>
17#include <linux/mmc/card.h>
18#include <linux/mmc/mmc.h>
Pierre Ossman3373c0a2007-05-31 22:25:11 +020019#include <linux/mmc/sd.h>
Pierre Ossman7ea239d2006-12-31 00:11:32 +010020
21#include "core.h"
Pierre Ossman4101c162007-05-19 13:39:01 +020022#include "bus.h"
Pierre Ossman7ea239d2006-12-31 00:11:32 +010023#include "mmc_ops.h"
Mark Brownce101492011-02-10 10:58:37 +000024#include "sd.h"
Pierre Ossman7ea239d2006-12-31 00:11:32 +010025#include "sd_ops.h"
26
Pierre Ossman7ea239d2006-12-31 00:11:32 +010027static const unsigned int tran_exp[] = {
28 10000, 100000, 1000000, 10000000,
29 0, 0, 0, 0
30};
31
32static const unsigned char tran_mant[] = {
33 0, 10, 12, 13, 15, 20, 25, 30,
34 35, 40, 45, 50, 55, 60, 70, 80,
35};
36
37static const unsigned int tacc_exp[] = {
38 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
39};
40
41static const unsigned int tacc_mant[] = {
42 0, 10, 12, 13, 15, 20, 25, 30,
43 35, 40, 45, 50, 55, 60, 70, 80,
44};
45
46#define UNSTUFF_BITS(resp,start,size) \
47 ({ \
48 const int __size = size; \
49 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
50 const int __off = 3 - ((start) / 32); \
51 const int __shft = (start) & 31; \
52 u32 __res; \
53 \
54 __res = resp[__off] >> __shft; \
55 if (__size + __shft > 32) \
56 __res |= resp[__off-1] << ((32 - __shft) % 32); \
57 __res & __mask; \
58 })
59
60/*
61 * Given the decoded CSD structure, decode the raw CID to our CID structure.
62 */
Michal Miroslaw71578a12010-08-10 18:01:40 -070063void mmc_decode_cid(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +010064{
65 u32 *resp = card->raw_cid;
66
67 memset(&card->cid, 0, sizeof(struct mmc_cid));
68
69 /*
70 * SD doesn't currently have a version field so we will
71 * have to assume we can parse this.
72 */
73 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
74 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
75 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
76 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
77 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
78 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
79 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
80 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
81 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
82 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
83 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
84 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
85
86 card->cid.year += 2000; /* SD cards year offset */
87}
88
89/*
90 * Given a 128-bit response, decode to our card CSD structure.
91 */
Pierre Ossmanbd766312007-05-01 16:11:57 +020092static int mmc_decode_csd(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +010093{
94 struct mmc_csd *csd = &card->csd;
95 unsigned int e, m, csd_struct;
96 u32 *resp = card->raw_csd;
97
98 csd_struct = UNSTUFF_BITS(resp, 126, 2);
99
100 switch (csd_struct) {
101 case 0:
102 m = UNSTUFF_BITS(resp, 115, 4);
103 e = UNSTUFF_BITS(resp, 112, 3);
104 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
105 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
106
107 m = UNSTUFF_BITS(resp, 99, 4);
108 e = UNSTUFF_BITS(resp, 96, 3);
109 csd->max_dtr = tran_exp[e] * tran_mant[m];
110 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
111
112 e = UNSTUFF_BITS(resp, 47, 3);
113 m = UNSTUFF_BITS(resp, 62, 12);
114 csd->capacity = (1 + m) << (e + 2);
115
116 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
117 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
118 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
119 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
120 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
121 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
122 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700123
124 if (UNSTUFF_BITS(resp, 46, 1)) {
125 csd->erase_size = 1;
126 } else if (csd->write_blkbits >= 9) {
127 csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1;
128 csd->erase_size <<= csd->write_blkbits - 9;
129 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100130 break;
131 case 1:
132 /*
Arindam Nath3a303512011-05-05 12:19:03 +0530133 * This is a block-addressed SDHC or SDXC card. Most
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100134 * interesting fields are unused and have fixed
135 * values. To avoid getting tripped by buggy cards,
136 * we assume those fixed values ourselves.
137 */
138 mmc_card_set_blockaddr(card);
139
140 csd->tacc_ns = 0; /* Unused */
141 csd->tacc_clks = 0; /* Unused */
142
143 m = UNSTUFF_BITS(resp, 99, 4);
144 e = UNSTUFF_BITS(resp, 96, 3);
145 csd->max_dtr = tran_exp[e] * tran_mant[m];
146 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
Arindam Nath3a303512011-05-05 12:19:03 +0530147 csd->c_size = UNSTUFF_BITS(resp, 48, 22);
148
149 /* SDXC cards have a minimum C_SIZE of 0x00FFFF */
150 if (csd->c_size >= 0xFFFF)
151 mmc_card_set_ext_capacity(card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100152
153 m = UNSTUFF_BITS(resp, 48, 22);
154 csd->capacity = (1 + m) << 10;
155
156 csd->read_blkbits = 9;
157 csd->read_partial = 0;
158 csd->write_misalign = 0;
159 csd->read_misalign = 0;
160 csd->r2w_factor = 4; /* Unused */
161 csd->write_blkbits = 9;
162 csd->write_partial = 0;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700163 csd->erase_size = 1;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100164 break;
165 default:
Pierre Ossmanfacba912007-07-24 21:53:43 +0200166 printk(KERN_ERR "%s: unrecognised CSD structure version %d\n",
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100167 mmc_hostname(card->host), csd_struct);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200168 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100169 }
Pierre Ossmanbd766312007-05-01 16:11:57 +0200170
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700171 card->erase_size = csd->erase_size;
172
Pierre Ossmanbd766312007-05-01 16:11:57 +0200173 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100174}
175
176/*
177 * Given a 64-bit response, decode to our card SCR structure.
178 */
Pierre Ossmanbd766312007-05-01 16:11:57 +0200179static int mmc_decode_scr(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100180{
181 struct sd_scr *scr = &card->scr;
182 unsigned int scr_struct;
183 u32 resp[4];
184
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100185 resp[3] = card->raw_scr[1];
186 resp[2] = card->raw_scr[0];
187
188 scr_struct = UNSTUFF_BITS(resp, 60, 4);
189 if (scr_struct != 0) {
Pierre Ossmanfacba912007-07-24 21:53:43 +0200190 printk(KERN_ERR "%s: unrecognised SCR structure version %d\n",
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100191 mmc_hostname(card->host), scr_struct);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200192 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100193 }
194
195 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
196 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
Arindam Nath013909c2011-05-05 12:18:58 +0530197 if (scr->sda_vsn == SCR_SPEC_VER_2)
198 /* Check if Physical Layer Spec v3.0 is supported */
199 scr->sda_spec3 = UNSTUFF_BITS(resp, 47, 1);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200200
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700201 if (UNSTUFF_BITS(resp, 55, 1))
202 card->erased_byte = 0xFF;
203 else
204 card->erased_byte = 0x0;
205
Andrei Warkentinf0d89972011-05-23 15:06:38 -0500206 if (scr->sda_spec3)
207 scr->cmds = UNSTUFF_BITS(resp, 32, 2);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200208 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100209}
210
211/*
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700212 * Fetch and process SD Status register.
213 */
214static int mmc_read_ssr(struct mmc_card *card)
215{
216 unsigned int au, es, et, eo;
217 int err, i;
218 u32 *ssr;
219
220 if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
221 printk(KERN_WARNING "%s: card lacks mandatory SD Status "
222 "function.\n", mmc_hostname(card->host));
223 return 0;
224 }
225
226 ssr = kmalloc(64, GFP_KERNEL);
227 if (!ssr)
228 return -ENOMEM;
229
230 err = mmc_app_sd_status(card, ssr);
231 if (err) {
232 printk(KERN_WARNING "%s: problem reading SD Status "
233 "register.\n", mmc_hostname(card->host));
234 err = 0;
235 goto out;
236 }
237
238 for (i = 0; i < 16; i++)
239 ssr[i] = be32_to_cpu(ssr[i]);
240
241 /*
242 * UNSTUFF_BITS only works with four u32s so we have to offset the
243 * bitfield positions accordingly.
244 */
245 au = UNSTUFF_BITS(ssr, 428 - 384, 4);
246 if (au > 0 || au <= 9) {
247 card->ssr.au = 1 << (au + 4);
248 es = UNSTUFF_BITS(ssr, 408 - 384, 16);
249 et = UNSTUFF_BITS(ssr, 402 - 384, 6);
250 eo = UNSTUFF_BITS(ssr, 400 - 384, 2);
251 if (es && et) {
252 card->ssr.erase_timeout = (et * 1000) / es;
253 card->ssr.erase_offset = eo * 1000;
254 }
255 } else {
256 printk(KERN_WARNING "%s: SD Status: Invalid Allocation Unit "
257 "size.\n", mmc_hostname(card->host));
258 }
259out:
260 kfree(ssr);
261 return err;
262}
263
264/*
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200265 * Fetches and decodes switch information
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100266 */
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200267static int mmc_read_switch(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100268{
269 int err;
270 u8 *status;
271
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200272 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
Pierre Ossman17b04292007-07-22 22:18:46 +0200273 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200274
275 if (!(card->csd.cmdclass & CCC_SWITCH)) {
276 printk(KERN_WARNING "%s: card lacks mandatory switch "
277 "function, performance might suffer.\n",
278 mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200279 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200280 }
281
Pierre Ossman17b04292007-07-22 22:18:46 +0200282 err = -EIO;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100283
284 status = kmalloc(64, GFP_KERNEL);
285 if (!status) {
Pierre Ossmanfacba912007-07-24 21:53:43 +0200286 printk(KERN_ERR "%s: could not allocate a buffer for "
Arindam Nath013909c2011-05-05 12:18:58 +0530287 "switch capabilities.\n",
288 mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200289 return -ENOMEM;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100290 }
291
Arindam Nath013909c2011-05-05 12:18:58 +0530292 /* Find out the supported Bus Speed Modes. */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100293 err = mmc_sd_switch(card, 0, 0, 1, status);
Pierre Ossman17b04292007-07-22 22:18:46 +0200294 if (err) {
Arindam Nath013909c2011-05-05 12:18:58 +0530295 /*
296 * If the host or the card can't do the switch,
297 * fail more gracefully.
298 */
299 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200300 goto out;
301
Arindam Nath013909c2011-05-05 12:18:58 +0530302 printk(KERN_WARNING "%s: problem reading Bus Speed modes.\n",
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200303 mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200304 err = 0;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200305
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100306 goto out;
307 }
308
Subhash Jadavani6e64d742011-08-05 18:16:58 +0530309 if (status[13] & 0x02)
310 card->sw_caps.hs_max_dtr = 50000000;
311
Arindam Nath013909c2011-05-05 12:18:58 +0530312 if (card->scr.sda_spec3) {
313 card->sw_caps.sd3_bus_mode = status[13];
314
315 /* Find out Driver Strengths supported by the card */
316 err = mmc_sd_switch(card, 0, 2, 1, status);
317 if (err) {
318 /*
319 * If the host or the card can't do the switch,
320 * fail more gracefully.
321 */
322 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
323 goto out;
324
325 printk(KERN_WARNING "%s: problem reading "
326 "Driver Strength.\n",
327 mmc_hostname(card->host));
328 err = 0;
329
330 goto out;
331 }
332
333 card->sw_caps.sd3_drv_type = status[9];
334
335 /* Find out Current Limits supported by the card */
336 err = mmc_sd_switch(card, 0, 3, 1, status);
337 if (err) {
338 /*
339 * If the host or the card can't do the switch,
340 * fail more gracefully.
341 */
342 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
343 goto out;
344
345 printk(KERN_WARNING "%s: problem reading "
346 "Current Limit.\n",
347 mmc_hostname(card->host));
348 err = 0;
349
350 goto out;
351 }
352
353 card->sw_caps.sd3_curr_limit = status[7];
Arindam Nath013909c2011-05-05 12:18:58 +0530354 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100355
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200356out:
357 kfree(status);
358
359 return err;
360}
361
362/*
363 * Test if the card supports high-speed mode and, if so, switch to it.
364 */
Michal Miroslaw71578a12010-08-10 18:01:40 -0700365int mmc_sd_switch_hs(struct mmc_card *card)
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200366{
367 int err;
368 u8 *status;
369
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200370 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
Pierre Ossman17b04292007-07-22 22:18:46 +0200371 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200372
373 if (!(card->csd.cmdclass & CCC_SWITCH))
Pierre Ossman17b04292007-07-22 22:18:46 +0200374 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200375
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200376 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
Pierre Ossman17b04292007-07-22 22:18:46 +0200377 return 0;
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200378
379 if (card->sw_caps.hs_max_dtr == 0)
Pierre Ossman17b04292007-07-22 22:18:46 +0200380 return 0;
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200381
Pierre Ossman17b04292007-07-22 22:18:46 +0200382 err = -EIO;
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200383
384 status = kmalloc(64, GFP_KERNEL);
385 if (!status) {
Pierre Ossmanfacba912007-07-24 21:53:43 +0200386 printk(KERN_ERR "%s: could not allocate a buffer for "
387 "switch capabilities.\n", mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200388 return -ENOMEM;
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200389 }
390
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100391 err = mmc_sd_switch(card, 1, 0, 1, status);
Pierre Ossman17b04292007-07-22 22:18:46 +0200392 if (err)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100393 goto out;
394
395 if ((status[16] & 0xF) != 1) {
396 printk(KERN_WARNING "%s: Problem switching card "
397 "into high-speed mode!\n",
398 mmc_hostname(card->host));
Michal Miroslaw71578a12010-08-10 18:01:40 -0700399 err = 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100400 } else {
Michal Miroslaw71578a12010-08-10 18:01:40 -0700401 err = 1;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100402 }
403
404out:
405 kfree(status);
406
407 return err;
408}
409
Arindam Nathd6d50a12011-05-05 12:18:59 +0530410static int sd_select_driver_type(struct mmc_card *card, u8 *status)
411{
412 int host_drv_type = 0, card_drv_type = 0;
413 int err;
414
415 /*
416 * If the host doesn't support any of the Driver Types A,C or D,
417 * default Driver Type B is used.
418 */
419 if (!(card->host->caps & (MMC_CAP_DRIVER_TYPE_A | MMC_CAP_DRIVER_TYPE_C
420 | MMC_CAP_DRIVER_TYPE_D)))
421 return 0;
422
423 if (card->host->caps & MMC_CAP_DRIVER_TYPE_A) {
424 host_drv_type = MMC_SET_DRIVER_TYPE_A;
425 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A)
426 card_drv_type = MMC_SET_DRIVER_TYPE_A;
427 else if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_B)
428 card_drv_type = MMC_SET_DRIVER_TYPE_B;
429 else if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
430 card_drv_type = MMC_SET_DRIVER_TYPE_C;
431 } else if (card->host->caps & MMC_CAP_DRIVER_TYPE_C) {
432 host_drv_type = MMC_SET_DRIVER_TYPE_C;
433 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
434 card_drv_type = MMC_SET_DRIVER_TYPE_C;
435 } else if (!(card->host->caps & MMC_CAP_DRIVER_TYPE_D)) {
436 /*
437 * If we are here, that means only the default driver type
438 * B is supported by the host.
439 */
440 host_drv_type = MMC_SET_DRIVER_TYPE_B;
441 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_B)
442 card_drv_type = MMC_SET_DRIVER_TYPE_B;
443 else if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
444 card_drv_type = MMC_SET_DRIVER_TYPE_C;
445 }
446
447 err = mmc_sd_switch(card, 1, 2, card_drv_type, status);
448 if (err)
449 return err;
450
451 if ((status[15] & 0xF) != card_drv_type) {
452 printk(KERN_WARNING "%s: Problem setting driver strength!\n",
453 mmc_hostname(card->host));
454 return 0;
455 }
456
457 mmc_set_driver_type(card->host, host_drv_type);
458
459 return 0;
460}
461
Subhash Jadavanic3eb6ef2011-07-13 17:55:56 +0530462static void sd_update_bus_speed_mode(struct mmc_card *card)
Arindam Nath49c468f2011-05-05 12:19:01 +0530463{
Arindam Nath49c468f2011-05-05 12:19:01 +0530464 /*
465 * If the host doesn't support any of the UHS-I modes, fallback on
466 * default speed.
467 */
468 if (!(card->host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
Subhash Jadavanic3eb6ef2011-07-13 17:55:56 +0530469 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50))) {
470 card->sd_bus_speed = 0;
471 return;
472 }
Arindam Nath49c468f2011-05-05 12:19:01 +0530473
474 if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
475 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
Subhash Jadavanic3eb6ef2011-07-13 17:55:56 +0530476 card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
Arindam Nath49c468f2011-05-05 12:19:01 +0530477 } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
478 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
Subhash Jadavanic3eb6ef2011-07-13 17:55:56 +0530479 card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
Arindam Nath49c468f2011-05-05 12:19:01 +0530480 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
481 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
482 SD_MODE_UHS_SDR50)) {
Subhash Jadavanic3eb6ef2011-07-13 17:55:56 +0530483 card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
Arindam Nath49c468f2011-05-05 12:19:01 +0530484 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
485 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
486 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
Subhash Jadavanic3eb6ef2011-07-13 17:55:56 +0530487 card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
Arindam Nath49c468f2011-05-05 12:19:01 +0530488 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
489 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
490 MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
491 SD_MODE_UHS_SDR12)) {
Subhash Jadavanic3eb6ef2011-07-13 17:55:56 +0530492 card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
493 }
494}
495
496static int sd_set_bus_speed_mode(struct mmc_card *card, u8 *status)
497{
498 int err;
499 unsigned int timing = 0;
500
501 switch (card->sd_bus_speed) {
502 case UHS_SDR104_BUS_SPEED:
503 timing = MMC_TIMING_UHS_SDR104;
504 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
505 break;
506 case UHS_DDR50_BUS_SPEED:
507 timing = MMC_TIMING_UHS_DDR50;
508 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
509 break;
510 case UHS_SDR50_BUS_SPEED:
511 timing = MMC_TIMING_UHS_SDR50;
512 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
513 break;
514 case UHS_SDR25_BUS_SPEED:
515 timing = MMC_TIMING_UHS_SDR25;
516 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
517 break;
518 case UHS_SDR12_BUS_SPEED:
519 timing = MMC_TIMING_UHS_SDR12;
520 card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
521 break;
522 default:
523 return 0;
Arindam Nath49c468f2011-05-05 12:19:01 +0530524 }
525
Subhash Jadavanic3eb6ef2011-07-13 17:55:56 +0530526 err = mmc_sd_switch(card, 1, 0, card->sd_bus_speed, status);
Arindam Nath49c468f2011-05-05 12:19:01 +0530527 if (err)
528 return err;
529
Subhash Jadavanic3eb6ef2011-07-13 17:55:56 +0530530 if ((status[16] & 0xF) != card->sd_bus_speed)
Arindam Nath49c468f2011-05-05 12:19:01 +0530531 printk(KERN_WARNING "%s: Problem setting bus speed mode!\n",
532 mmc_hostname(card->host));
533 else {
534 mmc_set_timing(card->host, timing);
535 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
536 }
537
538 return 0;
539}
540
Arindam Nath5371c922011-05-05 12:19:02 +0530541static int sd_set_current_limit(struct mmc_card *card, u8 *status)
542{
543 int current_limit = 0;
544 int err;
545
546 /*
547 * Current limit switch is only defined for SDR50, SDR104, and DDR50
548 * bus speed modes. For other bus speed modes, we set the default
549 * current limit of 200mA.
550 */
551 if ((card->sd_bus_speed == UHS_SDR50_BUS_SPEED) ||
552 (card->sd_bus_speed == UHS_SDR104_BUS_SPEED) ||
553 (card->sd_bus_speed == UHS_DDR50_BUS_SPEED)) {
554 if (card->host->caps & MMC_CAP_MAX_CURRENT_800) {
555 if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_800)
556 current_limit = SD_SET_CURRENT_LIMIT_800;
557 else if (card->sw_caps.sd3_curr_limit &
558 SD_MAX_CURRENT_600)
559 current_limit = SD_SET_CURRENT_LIMIT_600;
560 else if (card->sw_caps.sd3_curr_limit &
561 SD_MAX_CURRENT_400)
562 current_limit = SD_SET_CURRENT_LIMIT_400;
563 else if (card->sw_caps.sd3_curr_limit &
564 SD_MAX_CURRENT_200)
565 current_limit = SD_SET_CURRENT_LIMIT_200;
566 } else if (card->host->caps & MMC_CAP_MAX_CURRENT_600) {
567 if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_600)
568 current_limit = SD_SET_CURRENT_LIMIT_600;
569 else if (card->sw_caps.sd3_curr_limit &
570 SD_MAX_CURRENT_400)
571 current_limit = SD_SET_CURRENT_LIMIT_400;
572 else if (card->sw_caps.sd3_curr_limit &
573 SD_MAX_CURRENT_200)
574 current_limit = SD_SET_CURRENT_LIMIT_200;
575 } else if (card->host->caps & MMC_CAP_MAX_CURRENT_400) {
576 if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_400)
577 current_limit = SD_SET_CURRENT_LIMIT_400;
578 else if (card->sw_caps.sd3_curr_limit &
579 SD_MAX_CURRENT_200)
580 current_limit = SD_SET_CURRENT_LIMIT_200;
581 } else if (card->host->caps & MMC_CAP_MAX_CURRENT_200) {
582 if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_200)
583 current_limit = SD_SET_CURRENT_LIMIT_200;
584 }
585 } else
586 current_limit = SD_SET_CURRENT_LIMIT_200;
587
588 err = mmc_sd_switch(card, 1, 3, current_limit, status);
589 if (err)
590 return err;
591
592 if (((status[15] >> 4) & 0x0F) != current_limit)
593 printk(KERN_WARNING "%s: Problem setting current limit!\n",
594 mmc_hostname(card->host));
595
596 return 0;
597}
598
Arindam Nathd6d50a12011-05-05 12:18:59 +0530599/*
600 * UHS-I specific initialization procedure
601 */
602static int mmc_sd_init_uhs_card(struct mmc_card *card)
603{
604 int err;
605 u8 *status;
606
607 if (!card->scr.sda_spec3)
608 return 0;
609
610 if (!(card->csd.cmdclass & CCC_SWITCH))
611 return 0;
612
613 status = kmalloc(64, GFP_KERNEL);
614 if (!status) {
615 printk(KERN_ERR "%s: could not allocate a buffer for "
616 "switch capabilities.\n", mmc_hostname(card->host));
617 return -ENOMEM;
618 }
619
620 /* Set 4-bit bus width */
621 if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
622 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
623 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
624 if (err)
625 goto out;
626
627 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
628 }
629
Subhash Jadavanic3eb6ef2011-07-13 17:55:56 +0530630 /*
631 * Select the bus speed mode depending on host
632 * and card capability.
633 */
634 sd_update_bus_speed_mode(card);
635
Arindam Nathd6d50a12011-05-05 12:18:59 +0530636 /* Set the driver strength for the card */
637 err = sd_select_driver_type(card, status);
Arindam Nath49c468f2011-05-05 12:19:01 +0530638 if (err)
639 goto out;
640
Subhash Jadavanic3eb6ef2011-07-13 17:55:56 +0530641 /* Set current limit for the card */
642 err = sd_set_current_limit(card, status);
Arindam Nath5371c922011-05-05 12:19:02 +0530643 if (err)
644 goto out;
645
Subhash Jadavanic3eb6ef2011-07-13 17:55:56 +0530646 /* Set bus speed mode of the card */
647 err = sd_set_bus_speed_mode(card, status);
Arindam Nathb513ea22011-05-05 12:19:04 +0530648 if (err)
649 goto out;
650
651 /* SPI mode doesn't define CMD19 */
Sujit Reddy Thummaabb96812011-12-08 14:53:14 +0530652 if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning) {
653 mmc_host_clk_hold(card->host);
Girish K Sa3f41692012-02-29 12:00:09 +0530654 err = card->host->ops->execute_tuning(card->host,
655 MMC_SEND_TUNING_BLOCK);
Sujit Reddy Thummaabb96812011-12-08 14:53:14 +0530656 mmc_host_clk_release(card->host);
657 }
Arindam Nathd6d50a12011-05-05 12:18:59 +0530658
659out:
660 kfree(status);
661
662 return err;
663}
664
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100665MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
666 card->raw_cid[2], card->raw_cid[3]);
667MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
668 card->raw_csd[2], card->raw_csd[3]);
669MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
670MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700671MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
672MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100673MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
674MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
675MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
676MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
677MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
678MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
679
680
681static struct attribute *sd_std_attrs[] = {
682 &dev_attr_cid.attr,
683 &dev_attr_csd.attr,
684 &dev_attr_scr.attr,
685 &dev_attr_date.attr,
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700686 &dev_attr_erase_size.attr,
687 &dev_attr_preferred_erase_size.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100688 &dev_attr_fwrev.attr,
689 &dev_attr_hwrev.attr,
690 &dev_attr_manfid.attr,
691 &dev_attr_name.attr,
692 &dev_attr_oemid.attr,
693 &dev_attr_serial.attr,
694 NULL,
695};
696
697static struct attribute_group sd_std_attr_group = {
698 .attrs = sd_std_attrs,
699};
700
David Brownella4dbd672009-06-24 10:06:31 -0700701static const struct attribute_group *sd_attr_groups[] = {
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100702 &sd_std_attr_group,
703 NULL,
704};
705
Michal Miroslaw71578a12010-08-10 18:01:40 -0700706struct device_type sd_type = {
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100707 .groups = sd_attr_groups,
708};
709
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100710/*
Michal Miroslaw71578a12010-08-10 18:01:40 -0700711 * Fetch CID from card.
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200712 */
Arindam Nathd6d50a12011-05-05 12:18:59 +0530713int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid, u32 *rocr)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200714{
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200715 int err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200716
717 /*
718 * Since we're changing the OCR value, we seem to
719 * need to tell some cards to go back to the idle
720 * state. We wait 1ms to give cards time to
721 * respond.
722 */
723 mmc_go_idle(host);
724
725 /*
726 * If SD_SEND_IF_COND indicates an SD 2.0
727 * compliant card and we should set bit 30
728 * of the ocr to indicate that we can handle
729 * block-addressed SDHC cards.
730 */
731 err = mmc_send_if_cond(host, ocr);
Pierre Ossman17b04292007-07-22 22:18:46 +0200732 if (!err)
Arindam Nathf2119df2011-05-05 12:18:57 +0530733 ocr |= SD_OCR_CCS;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200734
Arindam Nathf2119df2011-05-05 12:18:57 +0530735 /*
736 * If the host supports one of UHS-I modes, request the card
737 * to switch to 1.8V signaling level.
738 */
739 if (host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
740 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50))
741 ocr |= SD_OCR_S18R;
742
743 /* If the host can supply more than 150mA, XPC should be set to 1. */
744 if (host->caps & (MMC_CAP_SET_XPC_330 | MMC_CAP_SET_XPC_300 |
745 MMC_CAP_SET_XPC_180))
746 ocr |= SD_OCR_XPC;
747
748try_again:
Arindam Nathd6d50a12011-05-05 12:18:59 +0530749 err = mmc_send_app_op_cond(host, ocr, rocr);
Pierre Ossman17b04292007-07-22 22:18:46 +0200750 if (err)
Michal Miroslaw71578a12010-08-10 18:01:40 -0700751 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200752
Arindam Nathf2119df2011-05-05 12:18:57 +0530753 /*
754 * In case CCS and S18A in the response is set, start Signal Voltage
755 * Switch procedure. SPI mode doesn't support CMD11.
756 */
Arindam Nathd6d50a12011-05-05 12:18:59 +0530757 if (!mmc_host_is_spi(host) && rocr &&
758 ((*rocr & 0x41000000) == 0x41000000)) {
Philip Rakity261bbd42011-05-13 11:17:17 +0530759 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180, true);
Arindam Nathf2119df2011-05-05 12:18:57 +0530760 if (err) {
761 ocr &= ~SD_OCR_S18R;
762 goto try_again;
763 }
764 }
765
David Brownellaf517152007-08-08 09:11:32 -0700766 if (mmc_host_is_spi(host))
767 err = mmc_send_cid(host, cid);
768 else
769 err = mmc_all_send_cid(host, cid);
Michal Miroslaw71578a12010-08-10 18:01:40 -0700770
771 return err;
772}
773
774int mmc_sd_get_csd(struct mmc_host *host, struct mmc_card *card)
775{
776 int err;
777
778 /*
779 * Fetch CSD from card.
780 */
781 err = mmc_send_csd(card, card->raw_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +0200782 if (err)
Michal Miroslaw71578a12010-08-10 18:01:40 -0700783 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200784
Michal Miroslaw71578a12010-08-10 18:01:40 -0700785 err = mmc_decode_csd(card);
786 if (err)
787 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200788
Michal Miroslaw71578a12010-08-10 18:01:40 -0700789 return 0;
790}
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200791
Michal Miroslaw71578a12010-08-10 18:01:40 -0700792int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
793 bool reinit)
794{
795 int err;
San Mehat01349cb2008-12-04 11:18:00 -0800796#ifdef CONFIG_MMC_PARANOID_SD_INIT
797 int retries;
798#endif
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200799
Michal Miroslaw71578a12010-08-10 18:01:40 -0700800 if (!reinit) {
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200801 /*
802 * Fetch SCR from card.
803 */
804 err = mmc_app_send_scr(card, card->raw_scr);
Pierre Ossman17b04292007-07-22 22:18:46 +0200805 if (err)
Michal Miroslaw71578a12010-08-10 18:01:40 -0700806 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200807
Pierre Ossmanbd766312007-05-01 16:11:57 +0200808 err = mmc_decode_scr(card);
Michal Miroslaw71578a12010-08-10 18:01:40 -0700809 if (err)
810 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200811
812 /*
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700813 * Fetch and process SD Status register.
814 */
815 err = mmc_read_ssr(card);
816 if (err)
817 return err;
818
819 /* Erase init depends on CSD and SSR */
820 mmc_init_erase(card);
821
822 /*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200823 * Fetch switch information from card.
824 */
San Mehatae11f092008-11-11 09:35:36 -0800825#ifdef CONFIG_MMC_PARANOID_SD_INIT
826 for (retries = 1; retries <= 3; retries++) {
827 err = mmc_read_switch(card);
828 if (!err) {
829 if (retries > 1) {
830 printk(KERN_WARNING
831 "%s: recovered\n",
832 mmc_hostname(host));
833 }
834 break;
835 } else {
836 printk(KERN_WARNING
837 "%s: read switch failed (attempt %d)\n",
838 mmc_hostname(host), retries);
839 }
840 }
841#else
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200842 err = mmc_read_switch(card);
San Mehatae11f092008-11-11 09:35:36 -0800843#endif
844
Pierre Ossman17b04292007-07-22 22:18:46 +0200845 if (err)
Michal Miroslaw71578a12010-08-10 18:01:40 -0700846 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200847 }
848
849 /*
Wolfgang Muees9d9f25c2009-04-07 14:48:16 +0100850 * For SPI, enable CRC as appropriate.
851 * This CRC enable is located AFTER the reading of the
852 * card registers because some SDHC cards are not able
853 * to provide valid CRCs for non-512-byte blocks.
854 */
855 if (mmc_host_is_spi(host)) {
856 err = mmc_spi_set_crc(host, use_spi_crc);
857 if (err)
Michal Miroslaw71578a12010-08-10 18:01:40 -0700858 return err;
Wolfgang Muees9d9f25c2009-04-07 14:48:16 +0100859 }
860
861 /*
Michal Miroslaw71578a12010-08-10 18:01:40 -0700862 * Check if read-only switch is active.
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200863 */
Michal Miroslaw71578a12010-08-10 18:01:40 -0700864 if (!reinit) {
865 int ro = -1;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200866
Sujit Reddy Thummaabb96812011-12-08 14:53:14 +0530867 if (host->ops->get_ro) {
868 mmc_host_clk_hold(host);
Michal Miroslaw71578a12010-08-10 18:01:40 -0700869 ro = host->ops->get_ro(host);
Sujit Reddy Thummaabb96812011-12-08 14:53:14 +0530870 mmc_host_clk_release(host);
871 }
Michal Miroslaw71578a12010-08-10 18:01:40 -0700872
873 if (ro < 0) {
874 printk(KERN_WARNING "%s: host does not "
875 "support reading read-only "
876 "switch. assuming write-enable.\n",
877 mmc_hostname(host));
878 } else if (ro > 0) {
879 mmc_card_set_readonly(card);
880 }
881 }
882
883 return 0;
884}
885
886unsigned mmc_sd_get_max_clock(struct mmc_card *card)
887{
888 unsigned max_dtr = (unsigned int)-1;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200889
890 if (mmc_card_highspeed(card)) {
891 if (max_dtr > card->sw_caps.hs_max_dtr)
892 max_dtr = card->sw_caps.hs_max_dtr;
893 } else if (max_dtr > card->csd.max_dtr) {
894 max_dtr = card->csd.max_dtr;
895 }
896
Michal Miroslaw71578a12010-08-10 18:01:40 -0700897 return max_dtr;
898}
899
900void mmc_sd_go_highspeed(struct mmc_card *card)
901{
902 mmc_card_set_highspeed(card);
903 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
904}
905
906/*
907 * Handle the detection and initialisation of a card.
908 *
909 * In the case of a resume, "oldcard" will contain the card
910 * we're trying to reinitialise.
911 */
912static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
913 struct mmc_card *oldcard)
914{
915 struct mmc_card *card;
916 int err;
917 u32 cid[4];
Arindam Nathd6d50a12011-05-05 12:18:59 +0530918 u32 rocr = 0;
Michal Miroslaw71578a12010-08-10 18:01:40 -0700919
920 BUG_ON(!host);
921 WARN_ON(!host->claimed);
922
Arindam Nathd6d50a12011-05-05 12:18:59 +0530923 err = mmc_sd_get_cid(host, ocr, cid, &rocr);
Michal Miroslaw71578a12010-08-10 18:01:40 -0700924 if (err)
925 return err;
926
927 if (oldcard) {
928 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
929 return -ENOENT;
930
931 card = oldcard;
932 } else {
933 /*
934 * Allocate card structure.
935 */
936 card = mmc_alloc_card(host, &sd_type);
937 if (IS_ERR(card))
938 return PTR_ERR(card);
939
940 card->type = MMC_TYPE_SD;
941 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
942 }
943
944 /*
945 * For native busses: get card RCA and quit open drain mode.
946 */
947 if (!mmc_host_is_spi(host)) {
948 err = mmc_send_relative_addr(host, &card->rca);
949 if (err)
950 return err;
951
952 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
953 }
954
955 if (!oldcard) {
956 err = mmc_sd_get_csd(host, card);
957 if (err)
958 return err;
959
960 mmc_decode_cid(card);
961 }
962
963 /*
964 * Select card, as all following commands rely on that.
965 */
966 if (!mmc_host_is_spi(host)) {
967 err = mmc_select_card(card);
968 if (err)
969 return err;
970 }
971
972 err = mmc_sd_setup_card(host, card, oldcard != NULL);
973 if (err)
974 goto free_card;
975
Arindam Nathd6d50a12011-05-05 12:18:59 +0530976 /* Initialization sequence for UHS-I cards */
977 if (rocr & SD_ROCR_S18A) {
978 err = mmc_sd_init_uhs_card(card);
Pierre Ossman17b04292007-07-22 22:18:46 +0200979 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200980 goto free_card;
Arindam Nath3a303512011-05-05 12:19:03 +0530981
982 /* Card is an ultra-high-speed card */
983 mmc_sd_card_set_uhs(card);
Arindam Nath4d55c5a2011-05-05 12:19:05 +0530984
985 /*
986 * Since initialization is now complete, enable preset
987 * value registers for UHS-I cards.
988 */
Sujit Reddy Thummaabb96812011-12-08 14:53:14 +0530989 if (host->ops->enable_preset_value) {
990 mmc_host_clk_hold(host);
Arindam Nath4d55c5a2011-05-05 12:19:05 +0530991 host->ops->enable_preset_value(host, true);
Sujit Reddy Thummaabb96812011-12-08 14:53:14 +0530992 mmc_host_clk_release(host);
993 }
Arindam Nathd6d50a12011-05-05 12:18:59 +0530994 } else {
995 /*
996 * Attempt to change to high-speed (if supported)
997 */
998 err = mmc_sd_switch_hs(card);
999 if (err > 0)
1000 mmc_sd_go_highspeed(card);
1001 else if (err)
1002 goto free_card;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001003
Arindam Nathd6d50a12011-05-05 12:18:59 +05301004 /*
1005 * Set bus speed.
1006 */
1007 mmc_set_clock(host, mmc_sd_get_max_clock(card));
1008
1009 /*
1010 * Switch to wider bus (if supported).
1011 */
1012 if ((host->caps & MMC_CAP_4_BIT_DATA) &&
1013 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
1014 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
1015 if (err)
1016 goto free_card;
1017
1018 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
1019 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001020 }
1021
Michal Miroslaw71578a12010-08-10 18:01:40 -07001022 host->card = card;
Pierre Ossman17b04292007-07-22 22:18:46 +02001023 return 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001024
1025free_card:
1026 if (!oldcard)
1027 mmc_remove_card(card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001028
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001029 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001030}
1031
1032/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001033 * Host is being removed. Free up the current card.
1034 */
1035static void mmc_sd_remove(struct mmc_host *host)
1036{
1037 BUG_ON(!host);
1038 BUG_ON(!host->card);
1039
1040 mmc_remove_card(host->card);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001041
1042 mmc_claim_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001043 host->card = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001044 mmc_release_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001045}
1046
1047/*
Adrian Hunter8b581b02011-11-28 16:22:00 +02001048 * Card detection - card is alive.
1049 */
1050static int mmc_sd_alive(struct mmc_host *host)
1051{
1052 return mmc_send_status(host->card, NULL);
1053}
1054
1055/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001056 * Card detection callback from host.
1057 */
1058static void mmc_sd_detect(struct mmc_host *host)
1059{
San Mehat01349cb2008-12-04 11:18:00 -08001060 int err = 0;
1061#ifdef CONFIG_MMC_PARANOID_SD_INIT
1062 int retries = 5;
1063#endif
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001064
1065 BUG_ON(!host);
1066 BUG_ON(!host->card);
San Mehat01349cb2008-12-04 11:18:00 -08001067
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001068 mmc_claim_host(host);
1069
1070 /*
1071 * Just check if our card has been removed.
1072 */
San Mehat01349cb2008-12-04 11:18:00 -08001073#ifdef CONFIG_MMC_PARANOID_SD_INIT
1074 while(retries) {
Adrian Hunter8b581b02011-11-28 16:22:00 +02001075 err = _mmc_detect_card_removed(host);
San Mehat01349cb2008-12-04 11:18:00 -08001076 if (err) {
1077 retries--;
1078 udelay(5);
1079 continue;
1080 }
1081 break;
1082 }
1083 if (!retries) {
1084 printk(KERN_ERR "%s(%s): Unable to re-detect card (%d)\n",
1085 __func__, mmc_hostname(host), err);
1086 }
1087#else
Adrian Hunter8b581b02011-11-28 16:22:00 +02001088 err = _mmc_detect_card_removed(host);
San Mehat01349cb2008-12-04 11:18:00 -08001089#endif
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001090 mmc_release_host(host);
1091
Pierre Ossman17b04292007-07-22 22:18:46 +02001092 if (err) {
Pierre Ossman4101c162007-05-19 13:39:01 +02001093 mmc_sd_remove(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001094
1095 mmc_claim_host(host);
1096 mmc_detach_bus(host);
1097 mmc_release_host(host);
1098 }
1099}
1100
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001101/*
1102 * Suspend callback from host.
1103 */
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001104static int mmc_sd_suspend(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001105{
1106 BUG_ON(!host);
1107 BUG_ON(!host->card);
1108
1109 mmc_claim_host(host);
David Brownellaf517152007-08-08 09:11:32 -07001110 if (!mmc_host_is_spi(host))
1111 mmc_deselect_cards(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001112 host->card->state &= ~MMC_STATE_HIGHSPEED;
1113 mmc_release_host(host);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001114
1115 return 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001116}
1117
1118/*
1119 * Resume callback from host.
1120 *
1121 * This function tries to determine if the same card is still present
1122 * and, if so, restore all state to it.
1123 */
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001124static int mmc_sd_resume(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001125{
1126 int err;
San Mehata1661952008-12-01 08:52:34 -08001127#ifdef CONFIG_MMC_PARANOID_SD_INIT
1128 int retries;
1129#endif
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001130
1131 BUG_ON(!host);
1132 BUG_ON(!host->card);
1133
1134 mmc_claim_host(host);
San Mehata1661952008-12-01 08:52:34 -08001135#ifdef CONFIG_MMC_PARANOID_SD_INIT
1136 retries = 5;
1137 while (retries) {
1138 err = mmc_sd_init_card(host, host->ocr, host->card);
1139
1140 if (err) {
1141 printk(KERN_ERR "%s: Re-init card rc = %d (retries = %d)\n",
1142 mmc_hostname(host), err, retries);
1143 mdelay(5);
1144 retries--;
1145 continue;
1146 }
1147 break;
1148 }
1149#else
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001150 err = mmc_sd_init_card(host, host->ocr, host->card);
San Mehata1661952008-12-01 08:52:34 -08001151#endif
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001152 mmc_release_host(host);
1153
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001154 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001155}
1156
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001157static int mmc_sd_power_restore(struct mmc_host *host)
Adrian Huntereae1aee2009-09-22 16:44:33 -07001158{
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001159 int ret;
1160
Adrian Huntereae1aee2009-09-22 16:44:33 -07001161 host->card->state &= ~MMC_STATE_HIGHSPEED;
1162 mmc_claim_host(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001163 ret = mmc_sd_init_card(host, host->ocr, host->card);
Adrian Huntereae1aee2009-09-22 16:44:33 -07001164 mmc_release_host(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001165
1166 return ret;
Adrian Huntereae1aee2009-09-22 16:44:33 -07001167}
1168
Adrian Hunter9feae242009-09-22 16:44:32 -07001169static const struct mmc_bus_ops mmc_sd_ops = {
1170 .remove = mmc_sd_remove,
1171 .detect = mmc_sd_detect,
1172 .suspend = NULL,
1173 .resume = NULL,
Adrian Huntereae1aee2009-09-22 16:44:33 -07001174 .power_restore = mmc_sd_power_restore,
Adrian Hunter8b581b02011-11-28 16:22:00 +02001175 .alive = mmc_sd_alive,
Adrian Hunter9feae242009-09-22 16:44:32 -07001176};
1177
1178static const struct mmc_bus_ops mmc_sd_ops_unsafe = {
1179 .remove = mmc_sd_remove,
1180 .detect = mmc_sd_detect,
1181 .suspend = mmc_sd_suspend,
1182 .resume = mmc_sd_resume,
Adrian Huntereae1aee2009-09-22 16:44:33 -07001183 .power_restore = mmc_sd_power_restore,
Adrian Hunter8b581b02011-11-28 16:22:00 +02001184 .alive = mmc_sd_alive,
Adrian Hunter9feae242009-09-22 16:44:32 -07001185};
1186
1187static void mmc_sd_attach_bus_ops(struct mmc_host *host)
1188{
1189 const struct mmc_bus_ops *bus_ops;
1190
Matt Fleming71d7d3d2010-09-27 09:42:19 +01001191 if (!mmc_card_is_removable(host))
Adrian Hunter9feae242009-09-22 16:44:32 -07001192 bus_ops = &mmc_sd_ops_unsafe;
1193 else
1194 bus_ops = &mmc_sd_ops;
1195 mmc_attach_bus(host, bus_ops);
1196}
1197
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001198/*
1199 * Starting point for SD card init.
1200 */
Andy Ross807e8e42011-01-03 10:36:56 -08001201int mmc_attach_sd(struct mmc_host *host)
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001202{
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001203 int err;
Andy Ross807e8e42011-01-03 10:36:56 -08001204 u32 ocr;
San Mehat01349cb2008-12-04 11:18:00 -08001205#ifdef CONFIG_MMC_PARANOID_SD_INIT
1206 int retries;
1207#endif
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001208
1209 BUG_ON(!host);
Pierre Ossmand84075c82007-08-09 13:23:56 +02001210 WARN_ON(!host->claimed);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001211
Arindam Nathf2119df2011-05-05 12:18:57 +05301212 /* Make sure we are at 3.3V signalling voltage */
Philip Rakity261bbd42011-05-13 11:17:17 +05301213 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, false);
Arindam Nathf2119df2011-05-05 12:18:57 +05301214 if (err)
1215 return err;
1216
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301217 /* Disable preset value enable if already set since last time */
Sujit Reddy Thummaabb96812011-12-08 14:53:14 +05301218 if (host->ops->enable_preset_value) {
1219 mmc_host_clk_hold(host);
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301220 host->ops->enable_preset_value(host, false);
Sujit Reddy Thummaabb96812011-12-08 14:53:14 +05301221 mmc_host_clk_release(host);
1222 }
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301223
Andy Ross807e8e42011-01-03 10:36:56 -08001224 err = mmc_send_app_op_cond(host, 0, &ocr);
1225 if (err)
1226 return err;
1227
Adrian Hunter9feae242009-09-22 16:44:32 -07001228 mmc_sd_attach_bus_ops(host);
Takashi Iwai8f230f42010-12-08 10:04:30 +01001229 if (host->ocr_avail_sd)
1230 host->ocr_avail = host->ocr_avail_sd;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001231
Philip Langdale55556da2007-03-16 19:39:00 -07001232 /*
David Brownellaf517152007-08-08 09:11:32 -07001233 * We need to get OCR a different way for SPI.
1234 */
1235 if (mmc_host_is_spi(host)) {
1236 mmc_go_idle(host);
1237
1238 err = mmc_spi_read_ocr(host, 0, &ocr);
1239 if (err)
1240 goto err;
1241 }
1242
1243 /*
Philip Langdale55556da2007-03-16 19:39:00 -07001244 * Sanity check the voltages that the card claims to
1245 * support.
1246 */
1247 if (ocr & 0x7F) {
1248 printk(KERN_WARNING "%s: card claims to support voltages "
1249 "below the defined range. These will be ignored.\n",
1250 mmc_hostname(host));
1251 ocr &= ~0x7F;
1252 }
1253
Takashi Iwai8f230f42010-12-08 10:04:30 +01001254 if ((ocr & MMC_VDD_165_195) &&
1255 !(host->ocr_avail_sd & MMC_VDD_165_195)) {
Philip Langdale55556da2007-03-16 19:39:00 -07001256 printk(KERN_WARNING "%s: SD card claims to support the "
1257 "incompletely defined 'low voltage range'. This "
1258 "will be ignored.\n", mmc_hostname(host));
1259 ocr &= ~MMC_VDD_165_195;
1260 }
1261
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001262 host->ocr = mmc_select_voltage(host, ocr);
1263
1264 /*
1265 * Can we support the voltage(s) of the card(s)?
1266 */
Pierre Ossman109b5be2007-07-23 00:12:10 +02001267 if (!host->ocr) {
1268 err = -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001269 goto err;
Pierre Ossman109b5be2007-07-23 00:12:10 +02001270 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001271
1272 /*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001273 * Detect and init the card.
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001274 */
San Mehat01349cb2008-12-04 11:18:00 -08001275#ifdef CONFIG_MMC_PARANOID_SD_INIT
1276 retries = 5;
1277 while (retries) {
1278 err = mmc_sd_init_card(host, host->ocr, NULL);
1279 if (err) {
1280 retries--;
1281 continue;
1282 }
1283 break;
1284 }
1285
1286 if (!retries) {
1287 printk(KERN_ERR "%s: mmc_sd_init_card() failure (err = %d)\n",
1288 mmc_hostname(host), err);
1289 goto err;
1290 }
1291#else
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001292 err = mmc_sd_init_card(host, host->ocr, NULL);
Pierre Ossman17b04292007-07-22 22:18:46 +02001293 if (err)
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001294 goto err;
San Mehat01349cb2008-12-04 11:18:00 -08001295#endif
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001296
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001297 mmc_release_host(host);
Pierre Ossman4101c162007-05-19 13:39:01 +02001298 err = mmc_add_card(host->card);
Andy Ross807e8e42011-01-03 10:36:56 -08001299 mmc_claim_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001300 if (err)
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001301 goto remove_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001302
1303 return 0;
1304
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001305remove_card:
Andy Ross807e8e42011-01-03 10:36:56 -08001306 mmc_release_host(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001307 mmc_remove_card(host->card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001308 host->card = NULL;
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001309 mmc_claim_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001310err:
1311 mmc_detach_bus(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001312
Pierre Ossman109b5be2007-07-23 00:12:10 +02001313 printk(KERN_ERR "%s: error %d whilst initialising SD card\n",
1314 mmc_hostname(host), err);
1315
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001316 return err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001317}
1318