blob: 3648d880edd588c34cc3130179ab095c0e312d66 [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>
Paul Gortmaker0205a902011-07-15 12:01:27 -040015#include <linux/stat.h>
Pierre Ossman7ea239d2006-12-31 00:11:32 +010016
17#include <linux/mmc/host.h>
18#include <linux/mmc/card.h>
19#include <linux/mmc/mmc.h>
Pierre Ossman3373c0a2007-05-31 22:25:11 +020020#include <linux/mmc/sd.h>
Asutosh Dasbbc84782013-02-11 15:31:35 +053021#include <linux/pm_runtime.h>
Pierre Ossman7ea239d2006-12-31 00:11:32 +010022
23#include "core.h"
Pierre Ossman4101c162007-05-19 13:39:01 +020024#include "bus.h"
Pierre Ossman7ea239d2006-12-31 00:11:32 +010025#include "mmc_ops.h"
Mark Brownce101492011-02-10 10:58:37 +000026#include "sd.h"
Pierre Ossman7ea239d2006-12-31 00:11:32 +010027#include "sd_ops.h"
28
Pratibhasagar Vd8262f62013-09-17 14:25:35 +053029#define UHS_SDR104_MIN_DTR (100 * 1000 * 1000)
30#define UHS_DDR50_MIN_DTR (50 * 1000 * 1000)
31#define UHS_SDR50_MIN_DTR (50 * 1000 * 1000)
32#define UHS_SDR25_MIN_DTR (25 * 1000 * 1000)
33#define UHS_SDR12_MIN_DTR (12.5 * 1000 * 1000)
34
Pierre Ossman7ea239d2006-12-31 00:11:32 +010035static const unsigned int tran_exp[] = {
36 10000, 100000, 1000000, 10000000,
37 0, 0, 0, 0
38};
39
40static const unsigned char tran_mant[] = {
41 0, 10, 12, 13, 15, 20, 25, 30,
42 35, 40, 45, 50, 55, 60, 70, 80,
43};
44
45static const unsigned int tacc_exp[] = {
46 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
47};
48
49static const unsigned int tacc_mant[] = {
50 0, 10, 12, 13, 15, 20, 25, 30,
51 35, 40, 45, 50, 55, 60, 70, 80,
52};
53
54#define UNSTUFF_BITS(resp,start,size) \
55 ({ \
56 const int __size = size; \
57 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
58 const int __off = 3 - ((start) / 32); \
59 const int __shft = (start) & 31; \
60 u32 __res; \
61 \
62 __res = resp[__off] >> __shft; \
63 if (__size + __shft > 32) \
64 __res |= resp[__off-1] << ((32 - __shft) % 32); \
65 __res & __mask; \
66 })
67
68/*
69 * Given the decoded CSD structure, decode the raw CID to our CID structure.
70 */
Michal Miroslaw71578a12010-08-10 18:01:40 -070071void mmc_decode_cid(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +010072{
73 u32 *resp = card->raw_cid;
74
75 memset(&card->cid, 0, sizeof(struct mmc_cid));
76
77 /*
78 * SD doesn't currently have a version field so we will
79 * have to assume we can parse this.
80 */
81 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
82 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
83 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
84 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
85 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
86 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
87 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
88 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
89 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
90 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
91 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
92 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
93
94 card->cid.year += 2000; /* SD cards year offset */
95}
96
97/*
98 * Given a 128-bit response, decode to our card CSD structure.
99 */
Pierre Ossmanbd766312007-05-01 16:11:57 +0200100static int mmc_decode_csd(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100101{
102 struct mmc_csd *csd = &card->csd;
103 unsigned int e, m, csd_struct;
104 u32 *resp = card->raw_csd;
105
106 csd_struct = UNSTUFF_BITS(resp, 126, 2);
107
108 switch (csd_struct) {
109 case 0:
110 m = UNSTUFF_BITS(resp, 115, 4);
111 e = UNSTUFF_BITS(resp, 112, 3);
112 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
113 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
114
115 m = UNSTUFF_BITS(resp, 99, 4);
116 e = UNSTUFF_BITS(resp, 96, 3);
117 csd->max_dtr = tran_exp[e] * tran_mant[m];
118 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
119
120 e = UNSTUFF_BITS(resp, 47, 3);
121 m = UNSTUFF_BITS(resp, 62, 12);
122 csd->capacity = (1 + m) << (e + 2);
123
124 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
125 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
126 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
127 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
128 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
129 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
130 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700131
132 if (UNSTUFF_BITS(resp, 46, 1)) {
133 csd->erase_size = 1;
134 } else if (csd->write_blkbits >= 9) {
135 csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1;
136 csd->erase_size <<= csd->write_blkbits - 9;
137 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100138 break;
139 case 1:
140 /*
Arindam Nath3a303512011-05-05 12:19:03 +0530141 * This is a block-addressed SDHC or SDXC card. Most
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100142 * interesting fields are unused and have fixed
143 * values. To avoid getting tripped by buggy cards,
144 * we assume those fixed values ourselves.
145 */
146 mmc_card_set_blockaddr(card);
147
148 csd->tacc_ns = 0; /* Unused */
149 csd->tacc_clks = 0; /* Unused */
150
151 m = UNSTUFF_BITS(resp, 99, 4);
152 e = UNSTUFF_BITS(resp, 96, 3);
153 csd->max_dtr = tran_exp[e] * tran_mant[m];
154 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
Arindam Nath3a303512011-05-05 12:19:03 +0530155 csd->c_size = UNSTUFF_BITS(resp, 48, 22);
156
157 /* SDXC cards have a minimum C_SIZE of 0x00FFFF */
158 if (csd->c_size >= 0xFFFF)
159 mmc_card_set_ext_capacity(card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100160
161 m = UNSTUFF_BITS(resp, 48, 22);
162 csd->capacity = (1 + m) << 10;
163
164 csd->read_blkbits = 9;
165 csd->read_partial = 0;
166 csd->write_misalign = 0;
167 csd->read_misalign = 0;
168 csd->r2w_factor = 4; /* Unused */
169 csd->write_blkbits = 9;
170 csd->write_partial = 0;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700171 csd->erase_size = 1;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100172 break;
173 default:
Girish K Sa3c76eb2011-10-11 11:44:09 +0530174 pr_err("%s: unrecognised CSD structure version %d\n",
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100175 mmc_hostname(card->host), csd_struct);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200176 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100177 }
Pierre Ossmanbd766312007-05-01 16:11:57 +0200178
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700179 card->erase_size = csd->erase_size;
180
Pierre Ossmanbd766312007-05-01 16:11:57 +0200181 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100182}
183
184/*
185 * Given a 64-bit response, decode to our card SCR structure.
186 */
Pierre Ossmanbd766312007-05-01 16:11:57 +0200187static int mmc_decode_scr(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100188{
189 struct sd_scr *scr = &card->scr;
190 unsigned int scr_struct;
191 u32 resp[4];
192
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100193 resp[3] = card->raw_scr[1];
194 resp[2] = card->raw_scr[0];
195
196 scr_struct = UNSTUFF_BITS(resp, 60, 4);
197 if (scr_struct != 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530198 pr_err("%s: unrecognised SCR structure version %d\n",
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100199 mmc_hostname(card->host), scr_struct);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200200 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100201 }
202
203 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
204 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
Arindam Nath013909c2011-05-05 12:18:58 +0530205 if (scr->sda_vsn == SCR_SPEC_VER_2)
206 /* Check if Physical Layer Spec v3.0 is supported */
207 scr->sda_spec3 = UNSTUFF_BITS(resp, 47, 1);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200208
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700209 if (UNSTUFF_BITS(resp, 55, 1))
210 card->erased_byte = 0xFF;
211 else
212 card->erased_byte = 0x0;
213
Andrei Warkentinf0d89972011-05-23 15:06:38 -0500214 if (scr->sda_spec3)
215 scr->cmds = UNSTUFF_BITS(resp, 32, 2);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200216 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100217}
218
219/*
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700220 * Fetch and process SD Status register.
221 */
222static int mmc_read_ssr(struct mmc_card *card)
223{
224 unsigned int au, es, et, eo;
225 int err, i;
226 u32 *ssr;
227
228 if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530229 pr_warning("%s: card lacks mandatory SD Status "
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700230 "function.\n", mmc_hostname(card->host));
231 return 0;
232 }
233
234 ssr = kmalloc(64, GFP_KERNEL);
235 if (!ssr)
236 return -ENOMEM;
237
238 err = mmc_app_sd_status(card, ssr);
239 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530240 pr_warning("%s: problem reading SD Status "
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700241 "register.\n", mmc_hostname(card->host));
242 err = 0;
243 goto out;
244 }
245
246 for (i = 0; i < 16; i++)
247 ssr[i] = be32_to_cpu(ssr[i]);
248
249 /*
250 * UNSTUFF_BITS only works with four u32s so we have to offset the
251 * bitfield positions accordingly.
252 */
253 au = UNSTUFF_BITS(ssr, 428 - 384, 4);
254 if (au > 0 || au <= 9) {
255 card->ssr.au = 1 << (au + 4);
256 es = UNSTUFF_BITS(ssr, 408 - 384, 16);
257 et = UNSTUFF_BITS(ssr, 402 - 384, 6);
258 eo = UNSTUFF_BITS(ssr, 400 - 384, 2);
259 if (es && et) {
260 card->ssr.erase_timeout = (et * 1000) / es;
261 card->ssr.erase_offset = eo * 1000;
262 }
263 } else {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530264 pr_warning("%s: SD Status: Invalid Allocation Unit "
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700265 "size.\n", mmc_hostname(card->host));
266 }
267out:
268 kfree(ssr);
269 return err;
270}
271
272/*
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200273 * Fetches and decodes switch information
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100274 */
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200275static int mmc_read_switch(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100276{
277 int err;
278 u8 *status;
279
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200280 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
Pierre Ossman17b04292007-07-22 22:18:46 +0200281 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200282
283 if (!(card->csd.cmdclass & CCC_SWITCH)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530284 pr_warning("%s: card lacks mandatory switch "
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200285 "function, performance might suffer.\n",
286 mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200287 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200288 }
289
Pierre Ossman17b04292007-07-22 22:18:46 +0200290 err = -EIO;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100291
292 status = kmalloc(64, GFP_KERNEL);
293 if (!status) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530294 pr_err("%s: could not allocate a buffer for "
Arindam Nath013909c2011-05-05 12:18:58 +0530295 "switch capabilities.\n",
296 mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200297 return -ENOMEM;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100298 }
299
Arindam Nath013909c2011-05-05 12:18:58 +0530300 /* Find out the supported Bus Speed Modes. */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100301 err = mmc_sd_switch(card, 0, 0, 1, status);
Pierre Ossman17b04292007-07-22 22:18:46 +0200302 if (err) {
Arindam Nath013909c2011-05-05 12:18:58 +0530303 /*
304 * If the host or the card can't do the switch,
305 * fail more gracefully.
306 */
307 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200308 goto out;
309
Girish K Sa3c76eb2011-10-11 11:44:09 +0530310 pr_warning("%s: problem reading Bus Speed modes.\n",
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200311 mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200312 err = 0;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200313
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100314 goto out;
315 }
316
Qiang Liufffe5d52011-11-08 08:43:08 -0500317 if (status[13] & SD_MODE_HIGH_SPEED)
318 card->sw_caps.hs_max_dtr = HIGH_SPEED_MAX_DTR;
Subhash Jadavanif2815f62011-08-10 11:16:01 +0530319
Arindam Nath013909c2011-05-05 12:18:58 +0530320 if (card->scr.sda_spec3) {
321 card->sw_caps.sd3_bus_mode = status[13];
322
323 /* Find out Driver Strengths supported by the card */
324 err = mmc_sd_switch(card, 0, 2, 1, status);
325 if (err) {
326 /*
327 * If the host or the card can't do the switch,
328 * fail more gracefully.
329 */
330 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
331 goto out;
332
Girish K Sa3c76eb2011-10-11 11:44:09 +0530333 pr_warning("%s: problem reading "
Arindam Nath013909c2011-05-05 12:18:58 +0530334 "Driver Strength.\n",
335 mmc_hostname(card->host));
336 err = 0;
337
338 goto out;
339 }
340
341 card->sw_caps.sd3_drv_type = status[9];
342
343 /* Find out Current Limits supported by the card */
344 err = mmc_sd_switch(card, 0, 3, 1, status);
345 if (err) {
346 /*
347 * If the host or the card can't do the switch,
348 * fail more gracefully.
349 */
350 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
351 goto out;
352
Girish K Sa3c76eb2011-10-11 11:44:09 +0530353 pr_warning("%s: problem reading "
Arindam Nath013909c2011-05-05 12:18:58 +0530354 "Current Limit.\n",
355 mmc_hostname(card->host));
356 err = 0;
357
358 goto out;
359 }
360
361 card->sw_caps.sd3_curr_limit = status[7];
Arindam Nath013909c2011-05-05 12:18:58 +0530362 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100363
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200364out:
365 kfree(status);
366
367 return err;
368}
369
370/*
371 * Test if the card supports high-speed mode and, if so, switch to it.
372 */
Michal Miroslaw71578a12010-08-10 18:01:40 -0700373int mmc_sd_switch_hs(struct mmc_card *card)
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200374{
375 int err;
376 u8 *status;
377
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200378 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
Pierre Ossman17b04292007-07-22 22:18:46 +0200379 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200380
381 if (!(card->csd.cmdclass & CCC_SWITCH))
Pierre Ossman17b04292007-07-22 22:18:46 +0200382 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200383
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200384 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
Pierre Ossman17b04292007-07-22 22:18:46 +0200385 return 0;
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200386
387 if (card->sw_caps.hs_max_dtr == 0)
Pierre Ossman17b04292007-07-22 22:18:46 +0200388 return 0;
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200389
Pierre Ossman17b04292007-07-22 22:18:46 +0200390 err = -EIO;
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200391
392 status = kmalloc(64, GFP_KERNEL);
393 if (!status) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530394 pr_err("%s: could not allocate a buffer for "
Pierre Ossmanfacba912007-07-24 21:53:43 +0200395 "switch capabilities.\n", mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200396 return -ENOMEM;
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200397 }
398
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100399 err = mmc_sd_switch(card, 1, 0, 1, status);
Pierre Ossman17b04292007-07-22 22:18:46 +0200400 if (err)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100401 goto out;
402
403 if ((status[16] & 0xF) != 1) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530404 pr_warning("%s: Problem switching card "
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100405 "into high-speed mode!\n",
406 mmc_hostname(card->host));
Michal Miroslaw71578a12010-08-10 18:01:40 -0700407 err = 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100408 } else {
Michal Miroslaw71578a12010-08-10 18:01:40 -0700409 err = 1;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100410 }
411
412out:
413 kfree(status);
414
415 return err;
416}
417
Arindam Nathd6d50a12011-05-05 12:18:59 +0530418static int sd_select_driver_type(struct mmc_card *card, u8 *status)
419{
Philip Rakityca8e99b2011-07-06 08:51:32 -0700420 int host_drv_type = SD_DRIVER_TYPE_B;
421 int card_drv_type = SD_DRIVER_TYPE_B;
422 int drive_strength;
Arindam Nathd6d50a12011-05-05 12:18:59 +0530423 int err;
424
425 /*
426 * If the host doesn't support any of the Driver Types A,C or D,
Philip Rakityca8e99b2011-07-06 08:51:32 -0700427 * or there is no board specific handler then default Driver
428 * Type B is used.
Arindam Nathd6d50a12011-05-05 12:18:59 +0530429 */
430 if (!(card->host->caps & (MMC_CAP_DRIVER_TYPE_A | MMC_CAP_DRIVER_TYPE_C
431 | MMC_CAP_DRIVER_TYPE_D)))
432 return 0;
433
Philip Rakityca8e99b2011-07-06 08:51:32 -0700434 if (!card->host->ops->select_drive_strength)
435 return 0;
Arindam Nathd6d50a12011-05-05 12:18:59 +0530436
Philip Rakityca8e99b2011-07-06 08:51:32 -0700437 if (card->host->caps & MMC_CAP_DRIVER_TYPE_A)
438 host_drv_type |= SD_DRIVER_TYPE_A;
439
440 if (card->host->caps & MMC_CAP_DRIVER_TYPE_C)
441 host_drv_type |= SD_DRIVER_TYPE_C;
442
443 if (card->host->caps & MMC_CAP_DRIVER_TYPE_D)
444 host_drv_type |= SD_DRIVER_TYPE_D;
445
446 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A)
447 card_drv_type |= SD_DRIVER_TYPE_A;
448
449 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
450 card_drv_type |= SD_DRIVER_TYPE_C;
451
452 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_D)
453 card_drv_type |= SD_DRIVER_TYPE_D;
454
455 /*
456 * The drive strength that the hardware can support
457 * depends on the board design. Pass the appropriate
458 * information and let the hardware specific code
459 * return what is possible given the options
460 */
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -0500461 mmc_host_clk_hold(card->host);
Philip Rakityca8e99b2011-07-06 08:51:32 -0700462 drive_strength = card->host->ops->select_drive_strength(
463 card->sw_caps.uhs_max_dtr,
464 host_drv_type, card_drv_type);
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -0500465 mmc_host_clk_release(card->host);
Philip Rakityca8e99b2011-07-06 08:51:32 -0700466
467 err = mmc_sd_switch(card, 1, 2, drive_strength, status);
Arindam Nathd6d50a12011-05-05 12:18:59 +0530468 if (err)
469 return err;
470
Philip Rakityca8e99b2011-07-06 08:51:32 -0700471 if ((status[15] & 0xF) != drive_strength) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530472 pr_warning("%s: Problem setting drive strength!\n",
Arindam Nathd6d50a12011-05-05 12:18:59 +0530473 mmc_hostname(card->host));
474 return 0;
475 }
476
Philip Rakityca8e99b2011-07-06 08:51:32 -0700477 mmc_set_driver_type(card->host, drive_strength);
Arindam Nathd6d50a12011-05-05 12:18:59 +0530478
479 return 0;
480}
481
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530482static void sd_update_bus_speed_mode(struct mmc_card *card)
Arindam Nath49c468f2011-05-05 12:19:01 +0530483{
Arindam Nath49c468f2011-05-05 12:19:01 +0530484 /*
485 * If the host doesn't support any of the UHS-I modes, fallback on
486 * default speed.
487 */
488 if (!(card->host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530489 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50))) {
490 card->sd_bus_speed = 0;
491 return;
492 }
Arindam Nath49c468f2011-05-05 12:19:01 +0530493
494 if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
Pratibhasagar Vd8262f62013-09-17 14:25:35 +0530495 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104) &&
496 (card->host->f_max > UHS_SDR104_MIN_DTR)) {
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530497 card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
Arindam Nath49c468f2011-05-05 12:19:01 +0530498 } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
Pratibhasagar Vd8262f62013-09-17 14:25:35 +0530499 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50) &&
500 (card->host->f_max > UHS_DDR50_MIN_DTR)) {
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530501 card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
Arindam Nath49c468f2011-05-05 12:19:01 +0530502 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
503 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
Pratibhasagar Vd8262f62013-09-17 14:25:35 +0530504 SD_MODE_UHS_SDR50) &&
505 (card->host->f_max > UHS_SDR50_MIN_DTR)) {
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530506 card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
Arindam Nath49c468f2011-05-05 12:19:01 +0530507 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
508 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
Pratibhasagar Vd8262f62013-09-17 14:25:35 +0530509 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25) &&
510 (card->host->f_max > UHS_SDR25_MIN_DTR)) {
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530511 card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
Arindam Nath49c468f2011-05-05 12:19:01 +0530512 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
513 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
514 MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
515 SD_MODE_UHS_SDR12)) {
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530516 card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
517 }
518}
519
520static int sd_set_bus_speed_mode(struct mmc_card *card, u8 *status)
521{
522 int err;
523 unsigned int timing = 0;
524
525 switch (card->sd_bus_speed) {
526 case UHS_SDR104_BUS_SPEED:
527 timing = MMC_TIMING_UHS_SDR104;
528 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
529 break;
530 case UHS_DDR50_BUS_SPEED:
531 timing = MMC_TIMING_UHS_DDR50;
532 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
533 break;
534 case UHS_SDR50_BUS_SPEED:
535 timing = MMC_TIMING_UHS_SDR50;
536 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
537 break;
538 case UHS_SDR25_BUS_SPEED:
539 timing = MMC_TIMING_UHS_SDR25;
540 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
541 break;
542 case UHS_SDR12_BUS_SPEED:
543 timing = MMC_TIMING_UHS_SDR12;
544 card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
545 break;
546 default:
547 return 0;
Arindam Nath49c468f2011-05-05 12:19:01 +0530548 }
549
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530550 err = mmc_sd_switch(card, 1, 0, card->sd_bus_speed, status);
Arindam Nath49c468f2011-05-05 12:19:01 +0530551 if (err)
552 return err;
553
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530554 if ((status[16] & 0xF) != card->sd_bus_speed)
Girish K Sa3c76eb2011-10-11 11:44:09 +0530555 pr_warning("%s: Problem setting bus speed mode!\n",
Arindam Nath49c468f2011-05-05 12:19:01 +0530556 mmc_hostname(card->host));
557 else {
558 mmc_set_timing(card->host, timing);
559 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
560 }
561
562 return 0;
563}
564
Arindam Nath5371c922011-05-05 12:19:02 +0530565static int sd_set_current_limit(struct mmc_card *card, u8 *status)
566{
567 int current_limit = 0;
568 int err;
569
570 /*
571 * Current limit switch is only defined for SDR50, SDR104, and DDR50
572 * bus speed modes. For other bus speed modes, we set the default
573 * current limit of 200mA.
574 */
575 if ((card->sd_bus_speed == UHS_SDR50_BUS_SPEED) ||
576 (card->sd_bus_speed == UHS_SDR104_BUS_SPEED) ||
577 (card->sd_bus_speed == UHS_DDR50_BUS_SPEED)) {
578 if (card->host->caps & MMC_CAP_MAX_CURRENT_800) {
579 if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_800)
580 current_limit = SD_SET_CURRENT_LIMIT_800;
581 else if (card->sw_caps.sd3_curr_limit &
582 SD_MAX_CURRENT_600)
583 current_limit = SD_SET_CURRENT_LIMIT_600;
584 else if (card->sw_caps.sd3_curr_limit &
585 SD_MAX_CURRENT_400)
586 current_limit = SD_SET_CURRENT_LIMIT_400;
587 else if (card->sw_caps.sd3_curr_limit &
588 SD_MAX_CURRENT_200)
589 current_limit = SD_SET_CURRENT_LIMIT_200;
590 } else if (card->host->caps & MMC_CAP_MAX_CURRENT_600) {
591 if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_600)
592 current_limit = SD_SET_CURRENT_LIMIT_600;
593 else if (card->sw_caps.sd3_curr_limit &
594 SD_MAX_CURRENT_400)
595 current_limit = SD_SET_CURRENT_LIMIT_400;
596 else if (card->sw_caps.sd3_curr_limit &
597 SD_MAX_CURRENT_200)
598 current_limit = SD_SET_CURRENT_LIMIT_200;
599 } else if (card->host->caps & MMC_CAP_MAX_CURRENT_400) {
600 if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_400)
601 current_limit = SD_SET_CURRENT_LIMIT_400;
602 else if (card->sw_caps.sd3_curr_limit &
603 SD_MAX_CURRENT_200)
604 current_limit = SD_SET_CURRENT_LIMIT_200;
605 } else if (card->host->caps & MMC_CAP_MAX_CURRENT_200) {
606 if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_200)
607 current_limit = SD_SET_CURRENT_LIMIT_200;
608 }
609 } else
610 current_limit = SD_SET_CURRENT_LIMIT_200;
611
612 err = mmc_sd_switch(card, 1, 3, current_limit, status);
613 if (err)
614 return err;
615
616 if (((status[15] >> 4) & 0x0F) != current_limit)
Girish K Sa3c76eb2011-10-11 11:44:09 +0530617 pr_warning("%s: Problem setting current limit!\n",
Arindam Nath5371c922011-05-05 12:19:02 +0530618 mmc_hostname(card->host));
619
620 return 0;
621}
622
Sujit Reddy Thumma1cd61b72012-08-06 11:23:21 +0530623/**
624 * mmc_sd_change_bus_speed() - Change SD card bus frequency at runtime
625 * @host: pointer to mmc host structure
626 * @freq: pointer to desired frequency to be set
627 *
628 * Change the SD card bus frequency at runtime after the card is
629 * initialized. Callers are expected to make sure of the card's
630 * state (DATA/RCV/TRANSFER) beforing changing the frequency at runtime.
631 *
632 * If the frequency to change is greater than max. supported by card,
633 * *freq is changed to max. supported by card and if it is less than min.
634 * supported by host, *freq is changed to min. supported by host.
635 */
636static int mmc_sd_change_bus_speed(struct mmc_host *host, unsigned long *freq)
637{
638 int err = 0;
639 struct mmc_card *card;
640
641 mmc_claim_host(host);
642 /*
643 * Assign card pointer after claiming host to avoid race
644 * conditions that may arise during removal of the card.
645 */
646 card = host->card;
647
648 /* sanity checks */
649 if (!card || !freq) {
650 err = -EINVAL;
651 goto out;
652 }
653
654 if (mmc_card_uhs(card)) {
655 if (*freq > card->sw_caps.uhs_max_dtr)
656 *freq = card->sw_caps.uhs_max_dtr;
657 } else {
658 if (*freq > mmc_sd_get_max_clock(card))
659 *freq = mmc_sd_get_max_clock(card);
660 }
661
662 if (*freq < host->f_min)
663 *freq = host->f_min;
664
665 mmc_set_clock(host, (unsigned int) (*freq));
666
667 if (!mmc_host_is_spi(card->host) && mmc_sd_card_uhs(card)
668 && card->host->ops->execute_tuning) {
669 /*
670 * We try to probe host driver for tuning for any
671 * frequency, it is host driver responsibility to
672 * perform actual tuning only when required.
673 */
674 mmc_host_clk_hold(card->host);
675 err = card->host->ops->execute_tuning(card->host,
676 MMC_SEND_TUNING_BLOCK);
677 mmc_host_clk_release(card->host);
678
Sahitya Tummala64f92e12013-06-13 10:34:48 +0530679 if (err) {
680 pr_warn("%s: %s: tuning execution failed %d. Restoring to previous clock %lu\n",
681 mmc_hostname(card->host), __func__, err,
682 host->clk_scaling.curr_freq);
683 mmc_set_clock(host, host->clk_scaling.curr_freq);
684 }
Sujit Reddy Thumma1cd61b72012-08-06 11:23:21 +0530685 }
686
687out:
688 mmc_release_host(host);
689 return err;
690}
691
Arindam Nathd6d50a12011-05-05 12:18:59 +0530692/*
693 * UHS-I specific initialization procedure
694 */
695static int mmc_sd_init_uhs_card(struct mmc_card *card)
696{
697 int err;
698 u8 *status;
699
700 if (!card->scr.sda_spec3)
701 return 0;
702
703 if (!(card->csd.cmdclass & CCC_SWITCH))
704 return 0;
705
706 status = kmalloc(64, GFP_KERNEL);
707 if (!status) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530708 pr_err("%s: could not allocate a buffer for "
Arindam Nathd6d50a12011-05-05 12:18:59 +0530709 "switch capabilities.\n", mmc_hostname(card->host));
710 return -ENOMEM;
711 }
712
713 /* Set 4-bit bus width */
714 if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
715 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
716 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
717 if (err)
718 goto out;
719
720 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
721 }
722
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530723 /*
724 * Select the bus speed mode depending on host
725 * and card capability.
726 */
727 sd_update_bus_speed_mode(card);
728
Arindam Nathd6d50a12011-05-05 12:18:59 +0530729 /* Set the driver strength for the card */
730 err = sd_select_driver_type(card, status);
Arindam Nath49c468f2011-05-05 12:19:01 +0530731 if (err)
732 goto out;
733
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530734 /* Set current limit for the card */
735 err = sd_set_current_limit(card, status);
Arindam Nath5371c922011-05-05 12:19:02 +0530736 if (err)
737 goto out;
738
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530739 /* Set bus speed mode of the card */
740 err = sd_set_bus_speed_mode(card, status);
Arindam Nathb513ea22011-05-05 12:19:04 +0530741 if (err)
742 goto out;
743
744 /* SPI mode doesn't define CMD19 */
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -0500745 if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning) {
746 mmc_host_clk_hold(card->host);
Girish K Sa4924c72012-01-11 14:04:52 -0500747 err = card->host->ops->execute_tuning(card->host,
748 MMC_SEND_TUNING_BLOCK);
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -0500749 mmc_host_clk_release(card->host);
750 }
Arindam Nathd6d50a12011-05-05 12:18:59 +0530751
752out:
753 kfree(status);
754
755 return err;
756}
757
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100758MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
759 card->raw_cid[2], card->raw_cid[3]);
760MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
761 card->raw_csd[2], card->raw_csd[3]);
762MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
763MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700764MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
765MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100766MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
767MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
768MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
769MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
770MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
771MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
772
773
774static struct attribute *sd_std_attrs[] = {
775 &dev_attr_cid.attr,
776 &dev_attr_csd.attr,
777 &dev_attr_scr.attr,
778 &dev_attr_date.attr,
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700779 &dev_attr_erase_size.attr,
780 &dev_attr_preferred_erase_size.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100781 &dev_attr_fwrev.attr,
782 &dev_attr_hwrev.attr,
783 &dev_attr_manfid.attr,
784 &dev_attr_name.attr,
785 &dev_attr_oemid.attr,
786 &dev_attr_serial.attr,
787 NULL,
788};
789
790static struct attribute_group sd_std_attr_group = {
791 .attrs = sd_std_attrs,
792};
793
David Brownella4dbd672009-06-24 10:06:31 -0700794static const struct attribute_group *sd_attr_groups[] = {
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100795 &sd_std_attr_group,
796 NULL,
797};
798
Michal Miroslaw71578a12010-08-10 18:01:40 -0700799struct device_type sd_type = {
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100800 .groups = sd_attr_groups,
801};
802
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100803/*
Michal Miroslaw71578a12010-08-10 18:01:40 -0700804 * Fetch CID from card.
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200805 */
Arindam Nathd6d50a12011-05-05 12:18:59 +0530806int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid, u32 *rocr)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200807{
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200808 int err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200809
810 /*
811 * Since we're changing the OCR value, we seem to
812 * need to tell some cards to go back to the idle
813 * state. We wait 1ms to give cards time to
814 * respond.
815 */
816 mmc_go_idle(host);
817
818 /*
819 * If SD_SEND_IF_COND indicates an SD 2.0
820 * compliant card and we should set bit 30
821 * of the ocr to indicate that we can handle
822 * block-addressed SDHC cards.
823 */
824 err = mmc_send_if_cond(host, ocr);
Pierre Ossman17b04292007-07-22 22:18:46 +0200825 if (!err)
Arindam Nathf2119df2011-05-05 12:18:57 +0530826 ocr |= SD_OCR_CCS;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200827
Arindam Nathf2119df2011-05-05 12:18:57 +0530828 /*
829 * If the host supports one of UHS-I modes, request the card
830 * to switch to 1.8V signaling level.
831 */
832 if (host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
833 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50))
834 ocr |= SD_OCR_S18R;
835
836 /* If the host can supply more than 150mA, XPC should be set to 1. */
837 if (host->caps & (MMC_CAP_SET_XPC_330 | MMC_CAP_SET_XPC_300 |
838 MMC_CAP_SET_XPC_180))
839 ocr |= SD_OCR_XPC;
840
841try_again:
Arindam Nathd6d50a12011-05-05 12:18:59 +0530842 err = mmc_send_app_op_cond(host, ocr, rocr);
Pierre Ossman17b04292007-07-22 22:18:46 +0200843 if (err)
Michal Miroslaw71578a12010-08-10 18:01:40 -0700844 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200845
Arindam Nathf2119df2011-05-05 12:18:57 +0530846 /*
847 * In case CCS and S18A in the response is set, start Signal Voltage
848 * Switch procedure. SPI mode doesn't support CMD11.
849 */
Arindam Nathd6d50a12011-05-05 12:18:59 +0530850 if (!mmc_host_is_spi(host) && rocr &&
851 ((*rocr & 0x41000000) == 0x41000000)) {
Philip Rakity261bbd42011-05-13 11:17:17 +0530852 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180, true);
Arindam Nathf2119df2011-05-05 12:18:57 +0530853 if (err) {
854 ocr &= ~SD_OCR_S18R;
855 goto try_again;
856 }
857 }
858
David Brownellaf517152007-08-08 09:11:32 -0700859 if (mmc_host_is_spi(host))
860 err = mmc_send_cid(host, cid);
861 else
862 err = mmc_all_send_cid(host, cid);
Michal Miroslaw71578a12010-08-10 18:01:40 -0700863
864 return err;
865}
866
867int mmc_sd_get_csd(struct mmc_host *host, struct mmc_card *card)
868{
869 int err;
870
871 /*
872 * Fetch CSD from card.
873 */
874 err = mmc_send_csd(card, card->raw_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +0200875 if (err)
Michal Miroslaw71578a12010-08-10 18:01:40 -0700876 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200877
Michal Miroslaw71578a12010-08-10 18:01:40 -0700878 err = mmc_decode_csd(card);
879 if (err)
880 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200881
Michal Miroslaw71578a12010-08-10 18:01:40 -0700882 return 0;
883}
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200884
Michal Miroslaw71578a12010-08-10 18:01:40 -0700885int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
886 bool reinit)
887{
888 int err;
San Mehat8ed5a662008-12-04 11:18:00 -0800889#ifdef CONFIG_MMC_PARANOID_SD_INIT
890 int retries;
891#endif
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200892
Michal Miroslaw71578a12010-08-10 18:01:40 -0700893 if (!reinit) {
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200894 /*
895 * Fetch SCR from card.
896 */
897 err = mmc_app_send_scr(card, card->raw_scr);
Pierre Ossman17b04292007-07-22 22:18:46 +0200898 if (err)
Michal Miroslaw71578a12010-08-10 18:01:40 -0700899 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200900
Pierre Ossmanbd766312007-05-01 16:11:57 +0200901 err = mmc_decode_scr(card);
Michal Miroslaw71578a12010-08-10 18:01:40 -0700902 if (err)
903 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200904
905 /*
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700906 * Fetch and process SD Status register.
907 */
908 err = mmc_read_ssr(card);
909 if (err)
910 return err;
911
912 /* Erase init depends on CSD and SSR */
913 mmc_init_erase(card);
914
915 /*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200916 * Fetch switch information from card.
917 */
San Mehatc80f5462008-11-11 09:35:36 -0800918#ifdef CONFIG_MMC_PARANOID_SD_INIT
919 for (retries = 1; retries <= 3; retries++) {
920 err = mmc_read_switch(card);
921 if (!err) {
922 if (retries > 1) {
923 printk(KERN_WARNING
924 "%s: recovered\n",
925 mmc_hostname(host));
926 }
927 break;
928 } else {
929 printk(KERN_WARNING
930 "%s: read switch failed (attempt %d)\n",
931 mmc_hostname(host), retries);
932 }
933 }
934#else
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200935 err = mmc_read_switch(card);
San Mehatc80f5462008-11-11 09:35:36 -0800936#endif
937
Pierre Ossman17b04292007-07-22 22:18:46 +0200938 if (err)
Michal Miroslaw71578a12010-08-10 18:01:40 -0700939 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200940 }
941
942 /*
Wolfgang Muees9d9f25c2009-04-07 14:48:16 +0100943 * For SPI, enable CRC as appropriate.
944 * This CRC enable is located AFTER the reading of the
945 * card registers because some SDHC cards are not able
946 * to provide valid CRCs for non-512-byte blocks.
947 */
948 if (mmc_host_is_spi(host)) {
949 err = mmc_spi_set_crc(host, use_spi_crc);
950 if (err)
Michal Miroslaw71578a12010-08-10 18:01:40 -0700951 return err;
Wolfgang Muees9d9f25c2009-04-07 14:48:16 +0100952 }
953
954 /*
Michal Miroslaw71578a12010-08-10 18:01:40 -0700955 * Check if read-only switch is active.
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200956 */
Michal Miroslaw71578a12010-08-10 18:01:40 -0700957 if (!reinit) {
958 int ro = -1;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200959
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -0500960 if (host->ops->get_ro) {
Stephen Boyd3f113aa2013-02-11 14:30:14 -0800961 mmc_host_clk_hold(card->host);
Michal Miroslaw71578a12010-08-10 18:01:40 -0700962 ro = host->ops->get_ro(host);
Stephen Boyd3f113aa2013-02-11 14:30:14 -0800963 mmc_host_clk_release(card->host);
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -0500964 }
Michal Miroslaw71578a12010-08-10 18:01:40 -0700965
966 if (ro < 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530967 pr_warning("%s: host does not "
Michal Miroslaw71578a12010-08-10 18:01:40 -0700968 "support reading read-only "
969 "switch. assuming write-enable.\n",
970 mmc_hostname(host));
971 } else if (ro > 0) {
972 mmc_card_set_readonly(card);
973 }
974 }
975
976 return 0;
977}
978
979unsigned mmc_sd_get_max_clock(struct mmc_card *card)
980{
981 unsigned max_dtr = (unsigned int)-1;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200982
983 if (mmc_card_highspeed(card)) {
984 if (max_dtr > card->sw_caps.hs_max_dtr)
985 max_dtr = card->sw_caps.hs_max_dtr;
986 } else if (max_dtr > card->csd.max_dtr) {
987 max_dtr = card->csd.max_dtr;
988 }
989
Michal Miroslaw71578a12010-08-10 18:01:40 -0700990 return max_dtr;
991}
992
993void mmc_sd_go_highspeed(struct mmc_card *card)
994{
995 mmc_card_set_highspeed(card);
996 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
997}
998
999/*
1000 * Handle the detection and initialisation of a card.
1001 *
1002 * In the case of a resume, "oldcard" will contain the card
1003 * we're trying to reinitialise.
1004 */
1005static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
1006 struct mmc_card *oldcard)
1007{
1008 struct mmc_card *card;
1009 int err;
1010 u32 cid[4];
Arindam Nathd6d50a12011-05-05 12:18:59 +05301011 u32 rocr = 0;
Michal Miroslaw71578a12010-08-10 18:01:40 -07001012
1013 BUG_ON(!host);
1014 WARN_ON(!host->claimed);
1015
Ulf Hanssone7747472012-03-01 13:18:05 +01001016 /* The initialization should be done at 3.3 V I/O voltage. */
1017 mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, 0);
1018
Arindam Nathd6d50a12011-05-05 12:18:59 +05301019 err = mmc_sd_get_cid(host, ocr, cid, &rocr);
Michal Miroslaw71578a12010-08-10 18:01:40 -07001020 if (err)
1021 return err;
1022
1023 if (oldcard) {
1024 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
1025 return -ENOENT;
1026
1027 card = oldcard;
1028 } else {
1029 /*
1030 * Allocate card structure.
1031 */
1032 card = mmc_alloc_card(host, &sd_type);
1033 if (IS_ERR(card))
1034 return PTR_ERR(card);
1035
1036 card->type = MMC_TYPE_SD;
1037 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
1038 }
1039
1040 /*
1041 * For native busses: get card RCA and quit open drain mode.
1042 */
1043 if (!mmc_host_is_spi(host)) {
1044 err = mmc_send_relative_addr(host, &card->rca);
1045 if (err)
1046 return err;
Michal Miroslaw71578a12010-08-10 18:01:40 -07001047 }
1048
1049 if (!oldcard) {
1050 err = mmc_sd_get_csd(host, card);
1051 if (err)
1052 return err;
1053
1054 mmc_decode_cid(card);
1055 }
1056
1057 /*
1058 * Select card, as all following commands rely on that.
1059 */
1060 if (!mmc_host_is_spi(host)) {
1061 err = mmc_select_card(card);
1062 if (err)
1063 return err;
1064 }
1065
1066 err = mmc_sd_setup_card(host, card, oldcard != NULL);
1067 if (err)
1068 goto free_card;
1069
Arindam Nathd6d50a12011-05-05 12:18:59 +05301070 /* Initialization sequence for UHS-I cards */
1071 if (rocr & SD_ROCR_S18A) {
1072 err = mmc_sd_init_uhs_card(card);
Pierre Ossman17b04292007-07-22 22:18:46 +02001073 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001074 goto free_card;
Arindam Nath3a303512011-05-05 12:19:03 +05301075
1076 /* Card is an ultra-high-speed card */
Philip Rakitya303c532011-11-14 19:14:38 -08001077 mmc_card_set_uhs(card);
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301078
1079 /*
1080 * Since initialization is now complete, enable preset
1081 * value registers for UHS-I cards.
1082 */
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -05001083 if (host->ops->enable_preset_value) {
Stephen Boyd3f113aa2013-02-11 14:30:14 -08001084 mmc_host_clk_hold(card->host);
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301085 host->ops->enable_preset_value(host, true);
Stephen Boyd3f113aa2013-02-11 14:30:14 -08001086 mmc_host_clk_release(card->host);
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -05001087 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05301088 } else {
1089 /*
1090 * Attempt to change to high-speed (if supported)
1091 */
1092 err = mmc_sd_switch_hs(card);
1093 if (err > 0)
1094 mmc_sd_go_highspeed(card);
1095 else if (err)
1096 goto free_card;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001097
Arindam Nathd6d50a12011-05-05 12:18:59 +05301098 /*
1099 * Set bus speed.
1100 */
1101 mmc_set_clock(host, mmc_sd_get_max_clock(card));
1102
1103 /*
1104 * Switch to wider bus (if supported).
1105 */
1106 if ((host->caps & MMC_CAP_4_BIT_DATA) &&
1107 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
1108 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
1109 if (err)
1110 goto free_card;
1111
1112 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
1113 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001114 }
1115
Michal Miroslaw71578a12010-08-10 18:01:40 -07001116 host->card = card;
Pierre Ossman17b04292007-07-22 22:18:46 +02001117 return 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001118
1119free_card:
1120 if (!oldcard)
1121 mmc_remove_card(card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001122
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001123 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001124}
1125
1126/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001127 * Host is being removed. Free up the current card.
1128 */
1129static void mmc_sd_remove(struct mmc_host *host)
1130{
1131 BUG_ON(!host);
1132 BUG_ON(!host->card);
1133
1134 mmc_remove_card(host->card);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001135
1136 mmc_claim_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001137 host->card = NULL;
Sujit Reddy Thumma51d57b32012-10-11 17:17:03 +05301138 mmc_exit_clk_scaling(host);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001139 mmc_release_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001140}
1141
1142/*
Adrian Hunterd3049502011-11-28 16:22:00 +02001143 * Card detection - card is alive.
1144 */
1145static int mmc_sd_alive(struct mmc_host *host)
1146{
1147 return mmc_send_status(host->card, NULL);
1148}
1149
1150/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001151 * Card detection callback from host.
1152 */
1153static void mmc_sd_detect(struct mmc_host *host)
1154{
San Mehat8ed5a662008-12-04 11:18:00 -08001155 int err = 0;
1156#ifdef CONFIG_MMC_PARANOID_SD_INIT
1157 int retries = 5;
1158#endif
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001159
1160 BUG_ON(!host);
1161 BUG_ON(!host->card);
Asutosh Dasbbc84782013-02-11 15:31:35 +05301162
1163 mmc_rpm_hold(host, &host->card->dev);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001164 mmc_claim_host(host);
1165
1166 /*
1167 * Just check if our card has been removed.
1168 */
San Mehat8ed5a662008-12-04 11:18:00 -08001169#ifdef CONFIG_MMC_PARANOID_SD_INIT
1170 while(retries) {
1171 err = mmc_send_status(host->card, NULL);
1172 if (err) {
1173 retries--;
1174 udelay(5);
1175 continue;
1176 }
1177 break;
1178 }
1179 if (!retries) {
1180 printk(KERN_ERR "%s(%s): Unable to re-detect card (%d)\n",
1181 __func__, mmc_hostname(host), err);
Asutosh Dasccf601d2013-03-26 17:21:32 +05301182 err = _mmc_detect_card_removed(host);
San Mehat8ed5a662008-12-04 11:18:00 -08001183 }
1184#else
Adrian Hunterd3049502011-11-28 16:22:00 +02001185 err = _mmc_detect_card_removed(host);
San Mehat8ed5a662008-12-04 11:18:00 -08001186#endif
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001187 mmc_release_host(host);
1188
Asutosh Dasbbc84782013-02-11 15:31:35 +05301189 /*
1190 * if detect fails, the device would be removed anyway;
1191 * the rpm framework would mark the device state suspended.
1192 */
1193 if (!err)
1194 mmc_rpm_release(host, &host->card->dev);
1195
Pierre Ossman17b04292007-07-22 22:18:46 +02001196 if (err) {
Pierre Ossman4101c162007-05-19 13:39:01 +02001197 mmc_sd_remove(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001198
1199 mmc_claim_host(host);
1200 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -04001201 mmc_power_off(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001202 mmc_release_host(host);
1203 }
1204}
1205
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001206/*
1207 * Suspend callback from host.
1208 */
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001209static int mmc_sd_suspend(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001210{
1211 BUG_ON(!host);
1212 BUG_ON(!host->card);
1213
Sujit Reddy Thumma51d57b32012-10-11 17:17:03 +05301214 /*
1215 * Disable clock scaling before suspend and enable it after resume so
1216 * as to avoid clock scaling decisions kicking in during this window.
1217 */
1218 mmc_disable_clk_scaling(host);
1219
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001220 mmc_claim_host(host);
David Brownellaf517152007-08-08 09:11:32 -07001221 if (!mmc_host_is_spi(host))
1222 mmc_deselect_cards(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001223 host->card->state &= ~MMC_STATE_HIGHSPEED;
1224 mmc_release_host(host);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001225
1226 return 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001227}
1228
1229/*
1230 * Resume callback from host.
1231 *
1232 * This function tries to determine if the same card is still present
1233 * and, if so, restore all state to it.
1234 */
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001235static int mmc_sd_resume(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001236{
1237 int err;
San Mehat05063bf2008-12-01 08:52:34 -08001238#ifdef CONFIG_MMC_PARANOID_SD_INIT
1239 int retries;
1240#endif
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001241
1242 BUG_ON(!host);
1243 BUG_ON(!host->card);
1244
1245 mmc_claim_host(host);
San Mehat05063bf2008-12-01 08:52:34 -08001246#ifdef CONFIG_MMC_PARANOID_SD_INIT
1247 retries = 5;
1248 while (retries) {
1249 err = mmc_sd_init_card(host, host->ocr, host->card);
1250
1251 if (err) {
1252 printk(KERN_ERR "%s: Re-init card rc = %d (retries = %d)\n",
1253 mmc_hostname(host), err, retries);
San Mehat05063bf2008-12-01 08:52:34 -08001254 retries--;
Subhash Jadavani4dd5c442012-03-13 00:33:00 +05301255 mmc_power_off(host);
1256 usleep_range(5000, 5500);
1257 mmc_power_up(host);
1258 mmc_select_voltage(host, host->ocr);
San Mehat05063bf2008-12-01 08:52:34 -08001259 continue;
1260 }
1261 break;
1262 }
1263#else
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001264 err = mmc_sd_init_card(host, host->ocr, host->card);
San Mehat05063bf2008-12-01 08:52:34 -08001265#endif
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001266 mmc_release_host(host);
1267
Sujit Reddy Thumma51d57b32012-10-11 17:17:03 +05301268 /*
1269 * We have done full initialization of the card,
1270 * reset the clk scale stats and current frequency.
1271 */
1272 if (mmc_can_scale_clk(host))
1273 mmc_init_clk_scaling(host);
1274
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001275 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001276}
1277
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001278static int mmc_sd_power_restore(struct mmc_host *host)
Adrian Huntereae1aee2009-09-22 16:44:33 -07001279{
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001280 int ret;
1281
Sujit Reddy Thumma51d57b32012-10-11 17:17:03 +05301282 /* Disable clk scaling to avoid switching frequencies intermittently */
1283 mmc_disable_clk_scaling(host);
1284
Adrian Huntereae1aee2009-09-22 16:44:33 -07001285 host->card->state &= ~MMC_STATE_HIGHSPEED;
1286 mmc_claim_host(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001287 ret = mmc_sd_init_card(host, host->ocr, host->card);
Adrian Huntereae1aee2009-09-22 16:44:33 -07001288 mmc_release_host(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001289
Sujit Reddy Thumma51d57b32012-10-11 17:17:03 +05301290 if (mmc_can_scale_clk(host))
1291 mmc_init_clk_scaling(host);
1292
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001293 return ret;
Adrian Huntereae1aee2009-09-22 16:44:33 -07001294}
1295
Adrian Hunter9feae242009-09-22 16:44:32 -07001296static const struct mmc_bus_ops mmc_sd_ops = {
1297 .remove = mmc_sd_remove,
1298 .detect = mmc_sd_detect,
1299 .suspend = NULL,
1300 .resume = NULL,
Adrian Huntereae1aee2009-09-22 16:44:33 -07001301 .power_restore = mmc_sd_power_restore,
Adrian Hunterd3049502011-11-28 16:22:00 +02001302 .alive = mmc_sd_alive,
Sujit Reddy Thumma1cd61b72012-08-06 11:23:21 +05301303 .change_bus_speed = mmc_sd_change_bus_speed,
Adrian Hunter9feae242009-09-22 16:44:32 -07001304};
1305
1306static const struct mmc_bus_ops mmc_sd_ops_unsafe = {
1307 .remove = mmc_sd_remove,
1308 .detect = mmc_sd_detect,
1309 .suspend = mmc_sd_suspend,
1310 .resume = mmc_sd_resume,
Adrian Huntereae1aee2009-09-22 16:44:33 -07001311 .power_restore = mmc_sd_power_restore,
Adrian Hunterd3049502011-11-28 16:22:00 +02001312 .alive = mmc_sd_alive,
Sujit Reddy Thumma1cd61b72012-08-06 11:23:21 +05301313 .change_bus_speed = mmc_sd_change_bus_speed,
Adrian Hunter9feae242009-09-22 16:44:32 -07001314};
1315
1316static void mmc_sd_attach_bus_ops(struct mmc_host *host)
1317{
1318 const struct mmc_bus_ops *bus_ops;
1319
Matt Fleming71d7d3d2010-09-27 09:42:19 +01001320 if (!mmc_card_is_removable(host))
Adrian Hunter9feae242009-09-22 16:44:32 -07001321 bus_ops = &mmc_sd_ops_unsafe;
1322 else
1323 bus_ops = &mmc_sd_ops;
1324 mmc_attach_bus(host, bus_ops);
1325}
1326
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001327/*
1328 * Starting point for SD card init.
1329 */
Andy Ross807e8e42011-01-03 10:36:56 -08001330int mmc_attach_sd(struct mmc_host *host)
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001331{
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001332 int err;
Andy Ross807e8e42011-01-03 10:36:56 -08001333 u32 ocr;
San Mehat8ed5a662008-12-04 11:18:00 -08001334#ifdef CONFIG_MMC_PARANOID_SD_INIT
1335 int retries;
1336#endif
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001337
1338 BUG_ON(!host);
Pierre Ossmand84075c82007-08-09 13:23:56 +02001339 WARN_ON(!host->claimed);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001340
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301341 /* Disable preset value enable if already set since last time */
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -05001342 if (host->ops->enable_preset_value) {
1343 mmc_host_clk_hold(host);
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301344 host->ops->enable_preset_value(host, false);
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -05001345 mmc_host_clk_release(host);
1346 }
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301347
Andy Ross807e8e42011-01-03 10:36:56 -08001348 err = mmc_send_app_op_cond(host, 0, &ocr);
1349 if (err)
1350 return err;
1351
Adrian Hunter9feae242009-09-22 16:44:32 -07001352 mmc_sd_attach_bus_ops(host);
Takashi Iwai8f230f42010-12-08 10:04:30 +01001353 if (host->ocr_avail_sd)
1354 host->ocr_avail = host->ocr_avail_sd;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001355
Philip Langdale55556da2007-03-16 19:39:00 -07001356 /*
David Brownellaf517152007-08-08 09:11:32 -07001357 * We need to get OCR a different way for SPI.
1358 */
1359 if (mmc_host_is_spi(host)) {
1360 mmc_go_idle(host);
1361
1362 err = mmc_spi_read_ocr(host, 0, &ocr);
1363 if (err)
1364 goto err;
1365 }
1366
1367 /*
Philip Langdale55556da2007-03-16 19:39:00 -07001368 * Sanity check the voltages that the card claims to
1369 * support.
1370 */
1371 if (ocr & 0x7F) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301372 pr_warning("%s: card claims to support voltages "
Philip Langdale55556da2007-03-16 19:39:00 -07001373 "below the defined range. These will be ignored.\n",
1374 mmc_hostname(host));
1375 ocr &= ~0x7F;
1376 }
1377
Takashi Iwai8f230f42010-12-08 10:04:30 +01001378 if ((ocr & MMC_VDD_165_195) &&
1379 !(host->ocr_avail_sd & MMC_VDD_165_195)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301380 pr_warning("%s: SD card claims to support the "
Philip Langdale55556da2007-03-16 19:39:00 -07001381 "incompletely defined 'low voltage range'. This "
1382 "will be ignored.\n", mmc_hostname(host));
1383 ocr &= ~MMC_VDD_165_195;
1384 }
1385
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001386 host->ocr = mmc_select_voltage(host, ocr);
1387
1388 /*
1389 * Can we support the voltage(s) of the card(s)?
1390 */
Pierre Ossman109b5be2007-07-23 00:12:10 +02001391 if (!host->ocr) {
1392 err = -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001393 goto err;
Pierre Ossman109b5be2007-07-23 00:12:10 +02001394 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001395
1396 /*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001397 * Detect and init the card.
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001398 */
San Mehat8ed5a662008-12-04 11:18:00 -08001399#ifdef CONFIG_MMC_PARANOID_SD_INIT
1400 retries = 5;
1401 while (retries) {
1402 err = mmc_sd_init_card(host, host->ocr, NULL);
1403 if (err) {
1404 retries--;
Subhash Jadavani4dd5c442012-03-13 00:33:00 +05301405 mmc_power_off(host);
1406 usleep_range(5000, 5500);
1407 mmc_power_up(host);
1408 mmc_select_voltage(host, host->ocr);
San Mehat8ed5a662008-12-04 11:18:00 -08001409 continue;
1410 }
1411 break;
1412 }
1413
1414 if (!retries) {
1415 printk(KERN_ERR "%s: mmc_sd_init_card() failure (err = %d)\n",
1416 mmc_hostname(host), err);
1417 goto err;
1418 }
1419#else
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001420 err = mmc_sd_init_card(host, host->ocr, NULL);
Pierre Ossman17b04292007-07-22 22:18:46 +02001421 if (err)
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001422 goto err;
San Mehat8ed5a662008-12-04 11:18:00 -08001423#endif
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001424
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001425 mmc_release_host(host);
Pierre Ossman4101c162007-05-19 13:39:01 +02001426 err = mmc_add_card(host->card);
Andy Ross807e8e42011-01-03 10:36:56 -08001427 mmc_claim_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001428 if (err)
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001429 goto remove_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001430
Sujit Reddy Thumma1ed78ab2013-04-25 12:39:40 +05301431 mmc_init_clk_scaling(host);
Sujit Reddy Thumma51d57b32012-10-11 17:17:03 +05301432
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001433 return 0;
1434
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001435remove_card:
Andy Ross807e8e42011-01-03 10:36:56 -08001436 mmc_release_host(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001437 mmc_remove_card(host->card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001438 host->card = NULL;
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001439 mmc_claim_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001440err:
1441 mmc_detach_bus(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001442
Girish K Sa3c76eb2011-10-11 11:44:09 +05301443 pr_err("%s: error %d whilst initialising SD card\n",
Pierre Ossman109b5be2007-07-23 00:12:10 +02001444 mmc_hostname(host), err);
1445
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001446 return err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001447}
1448