blob: a4498d296a932764e95cd8c842e190ce19cf175b [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
Pierre Ossman7ea239d2006-12-31 00:11:32 +010029static const unsigned int tran_exp[] = {
30 10000, 100000, 1000000, 10000000,
31 0, 0, 0, 0
32};
33
34static const unsigned char tran_mant[] = {
35 0, 10, 12, 13, 15, 20, 25, 30,
36 35, 40, 45, 50, 55, 60, 70, 80,
37};
38
39static const unsigned int tacc_exp[] = {
40 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
41};
42
43static const unsigned int tacc_mant[] = {
44 0, 10, 12, 13, 15, 20, 25, 30,
45 35, 40, 45, 50, 55, 60, 70, 80,
46};
47
48#define UNSTUFF_BITS(resp,start,size) \
49 ({ \
50 const int __size = size; \
51 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
52 const int __off = 3 - ((start) / 32); \
53 const int __shft = (start) & 31; \
54 u32 __res; \
55 \
56 __res = resp[__off] >> __shft; \
57 if (__size + __shft > 32) \
58 __res |= resp[__off-1] << ((32 - __shft) % 32); \
59 __res & __mask; \
60 })
61
62/*
63 * Given the decoded CSD structure, decode the raw CID to our CID structure.
64 */
Michal Miroslaw71578a12010-08-10 18:01:40 -070065void mmc_decode_cid(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +010066{
67 u32 *resp = card->raw_cid;
68
69 memset(&card->cid, 0, sizeof(struct mmc_cid));
70
71 /*
72 * SD doesn't currently have a version field so we will
73 * have to assume we can parse this.
74 */
75 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
76 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
77 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
78 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
79 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
80 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
81 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
82 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
83 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
84 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
85 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
86 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
87
88 card->cid.year += 2000; /* SD cards year offset */
89}
90
91/*
92 * Given a 128-bit response, decode to our card CSD structure.
93 */
Pierre Ossmanbd766312007-05-01 16:11:57 +020094static int mmc_decode_csd(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +010095{
96 struct mmc_csd *csd = &card->csd;
97 unsigned int e, m, csd_struct;
98 u32 *resp = card->raw_csd;
99
100 csd_struct = UNSTUFF_BITS(resp, 126, 2);
101
102 switch (csd_struct) {
103 case 0:
104 m = UNSTUFF_BITS(resp, 115, 4);
105 e = UNSTUFF_BITS(resp, 112, 3);
106 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
107 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
108
109 m = UNSTUFF_BITS(resp, 99, 4);
110 e = UNSTUFF_BITS(resp, 96, 3);
111 csd->max_dtr = tran_exp[e] * tran_mant[m];
112 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
113
114 e = UNSTUFF_BITS(resp, 47, 3);
115 m = UNSTUFF_BITS(resp, 62, 12);
116 csd->capacity = (1 + m) << (e + 2);
117
118 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
119 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
120 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
121 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
122 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
123 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
124 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700125
126 if (UNSTUFF_BITS(resp, 46, 1)) {
127 csd->erase_size = 1;
128 } else if (csd->write_blkbits >= 9) {
129 csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1;
130 csd->erase_size <<= csd->write_blkbits - 9;
131 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100132 break;
133 case 1:
134 /*
Arindam Nath3a303512011-05-05 12:19:03 +0530135 * This is a block-addressed SDHC or SDXC card. Most
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100136 * interesting fields are unused and have fixed
137 * values. To avoid getting tripped by buggy cards,
138 * we assume those fixed values ourselves.
139 */
140 mmc_card_set_blockaddr(card);
141
142 csd->tacc_ns = 0; /* Unused */
143 csd->tacc_clks = 0; /* Unused */
144
145 m = UNSTUFF_BITS(resp, 99, 4);
146 e = UNSTUFF_BITS(resp, 96, 3);
147 csd->max_dtr = tran_exp[e] * tran_mant[m];
148 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
Arindam Nath3a303512011-05-05 12:19:03 +0530149 csd->c_size = UNSTUFF_BITS(resp, 48, 22);
150
151 /* SDXC cards have a minimum C_SIZE of 0x00FFFF */
152 if (csd->c_size >= 0xFFFF)
153 mmc_card_set_ext_capacity(card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100154
155 m = UNSTUFF_BITS(resp, 48, 22);
156 csd->capacity = (1 + m) << 10;
157
158 csd->read_blkbits = 9;
159 csd->read_partial = 0;
160 csd->write_misalign = 0;
161 csd->read_misalign = 0;
162 csd->r2w_factor = 4; /* Unused */
163 csd->write_blkbits = 9;
164 csd->write_partial = 0;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700165 csd->erase_size = 1;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100166 break;
167 default:
Girish K Sa3c76eb2011-10-11 11:44:09 +0530168 pr_err("%s: unrecognised CSD structure version %d\n",
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100169 mmc_hostname(card->host), csd_struct);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200170 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100171 }
Pierre Ossmanbd766312007-05-01 16:11:57 +0200172
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700173 card->erase_size = csd->erase_size;
174
Pierre Ossmanbd766312007-05-01 16:11:57 +0200175 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100176}
177
178/*
179 * Given a 64-bit response, decode to our card SCR structure.
180 */
Pierre Ossmanbd766312007-05-01 16:11:57 +0200181static int mmc_decode_scr(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100182{
183 struct sd_scr *scr = &card->scr;
184 unsigned int scr_struct;
185 u32 resp[4];
186
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100187 resp[3] = card->raw_scr[1];
188 resp[2] = card->raw_scr[0];
189
190 scr_struct = UNSTUFF_BITS(resp, 60, 4);
191 if (scr_struct != 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530192 pr_err("%s: unrecognised SCR structure version %d\n",
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100193 mmc_hostname(card->host), scr_struct);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200194 return -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100195 }
196
197 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
198 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
Arindam Nath013909c2011-05-05 12:18:58 +0530199 if (scr->sda_vsn == SCR_SPEC_VER_2)
200 /* Check if Physical Layer Spec v3.0 is supported */
201 scr->sda_spec3 = UNSTUFF_BITS(resp, 47, 1);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200202
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700203 if (UNSTUFF_BITS(resp, 55, 1))
204 card->erased_byte = 0xFF;
205 else
206 card->erased_byte = 0x0;
207
Andrei Warkentinf0d89972011-05-23 15:06:38 -0500208 if (scr->sda_spec3)
209 scr->cmds = UNSTUFF_BITS(resp, 32, 2);
Pierre Ossmanbd766312007-05-01 16:11:57 +0200210 return 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100211}
212
213/*
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700214 * Fetch and process SD Status register.
215 */
216static int mmc_read_ssr(struct mmc_card *card)
217{
218 unsigned int au, es, et, eo;
219 int err, i;
220 u32 *ssr;
221
222 if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530223 pr_warning("%s: card lacks mandatory SD Status "
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700224 "function.\n", mmc_hostname(card->host));
225 return 0;
226 }
227
228 ssr = kmalloc(64, GFP_KERNEL);
229 if (!ssr)
230 return -ENOMEM;
231
232 err = mmc_app_sd_status(card, ssr);
233 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530234 pr_warning("%s: problem reading SD Status "
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700235 "register.\n", mmc_hostname(card->host));
236 err = 0;
237 goto out;
238 }
239
240 for (i = 0; i < 16; i++)
241 ssr[i] = be32_to_cpu(ssr[i]);
242
243 /*
244 * UNSTUFF_BITS only works with four u32s so we have to offset the
245 * bitfield positions accordingly.
246 */
247 au = UNSTUFF_BITS(ssr, 428 - 384, 4);
248 if (au > 0 || au <= 9) {
249 card->ssr.au = 1 << (au + 4);
250 es = UNSTUFF_BITS(ssr, 408 - 384, 16);
251 et = UNSTUFF_BITS(ssr, 402 - 384, 6);
252 eo = UNSTUFF_BITS(ssr, 400 - 384, 2);
253 if (es && et) {
254 card->ssr.erase_timeout = (et * 1000) / es;
255 card->ssr.erase_offset = eo * 1000;
256 }
257 } else {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530258 pr_warning("%s: SD Status: Invalid Allocation Unit "
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700259 "size.\n", mmc_hostname(card->host));
260 }
261out:
262 kfree(ssr);
263 return err;
264}
265
266/*
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200267 * Fetches and decodes switch information
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100268 */
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200269static int mmc_read_switch(struct mmc_card *card)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100270{
271 int err;
272 u8 *status;
273
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200274 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
Pierre Ossman17b04292007-07-22 22:18:46 +0200275 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200276
277 if (!(card->csd.cmdclass & CCC_SWITCH)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530278 pr_warning("%s: card lacks mandatory switch "
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200279 "function, performance might suffer.\n",
280 mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200281 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200282 }
283
Pierre Ossman17b04292007-07-22 22:18:46 +0200284 err = -EIO;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100285
286 status = kmalloc(64, GFP_KERNEL);
287 if (!status) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530288 pr_err("%s: could not allocate a buffer for "
Arindam Nath013909c2011-05-05 12:18:58 +0530289 "switch capabilities.\n",
290 mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200291 return -ENOMEM;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100292 }
293
Arindam Nath013909c2011-05-05 12:18:58 +0530294 /* Find out the supported Bus Speed Modes. */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100295 err = mmc_sd_switch(card, 0, 0, 1, status);
Pierre Ossman17b04292007-07-22 22:18:46 +0200296 if (err) {
Arindam Nath013909c2011-05-05 12:18:58 +0530297 /*
298 * If the host or the card can't do the switch,
299 * fail more gracefully.
300 */
301 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200302 goto out;
303
Girish K Sa3c76eb2011-10-11 11:44:09 +0530304 pr_warning("%s: problem reading Bus Speed modes.\n",
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200305 mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200306 err = 0;
Pierre Ossmanadf66a02007-07-22 23:08:30 +0200307
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100308 goto out;
309 }
310
Qiang Liufffe5d52011-11-08 08:43:08 -0500311 if (status[13] & SD_MODE_HIGH_SPEED)
312 card->sw_caps.hs_max_dtr = HIGH_SPEED_MAX_DTR;
Subhash Jadavanif2815f62011-08-10 11:16:01 +0530313
Arindam Nath013909c2011-05-05 12:18:58 +0530314 if (card->scr.sda_spec3) {
315 card->sw_caps.sd3_bus_mode = status[13];
316
317 /* Find out Driver Strengths supported by the card */
318 err = mmc_sd_switch(card, 0, 2, 1, status);
319 if (err) {
320 /*
321 * If the host or the card can't do the switch,
322 * fail more gracefully.
323 */
324 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
325 goto out;
326
Girish K Sa3c76eb2011-10-11 11:44:09 +0530327 pr_warning("%s: problem reading "
Arindam Nath013909c2011-05-05 12:18:58 +0530328 "Driver Strength.\n",
329 mmc_hostname(card->host));
330 err = 0;
331
332 goto out;
333 }
334
335 card->sw_caps.sd3_drv_type = status[9];
336
337 /* Find out Current Limits supported by the card */
338 err = mmc_sd_switch(card, 0, 3, 1, status);
339 if (err) {
340 /*
341 * If the host or the card can't do the switch,
342 * fail more gracefully.
343 */
344 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
345 goto out;
346
Girish K Sa3c76eb2011-10-11 11:44:09 +0530347 pr_warning("%s: problem reading "
Arindam Nath013909c2011-05-05 12:18:58 +0530348 "Current Limit.\n",
349 mmc_hostname(card->host));
350 err = 0;
351
352 goto out;
353 }
354
355 card->sw_caps.sd3_curr_limit = status[7];
Arindam Nath013909c2011-05-05 12:18:58 +0530356 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100357
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200358out:
359 kfree(status);
360
361 return err;
362}
363
364/*
365 * Test if the card supports high-speed mode and, if so, switch to it.
366 */
Michal Miroslaw71578a12010-08-10 18:01:40 -0700367int mmc_sd_switch_hs(struct mmc_card *card)
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200368{
369 int err;
370 u8 *status;
371
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200372 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
Pierre Ossman17b04292007-07-22 22:18:46 +0200373 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200374
375 if (!(card->csd.cmdclass & CCC_SWITCH))
Pierre Ossman17b04292007-07-22 22:18:46 +0200376 return 0;
Pierre Ossman3373c0a2007-05-31 22:25:11 +0200377
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200378 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
Pierre Ossman17b04292007-07-22 22:18:46 +0200379 return 0;
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200380
381 if (card->sw_caps.hs_max_dtr == 0)
Pierre Ossman17b04292007-07-22 22:18:46 +0200382 return 0;
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200383
Pierre Ossman17b04292007-07-22 22:18:46 +0200384 err = -EIO;
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200385
386 status = kmalloc(64, GFP_KERNEL);
387 if (!status) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530388 pr_err("%s: could not allocate a buffer for "
Pierre Ossmanfacba912007-07-24 21:53:43 +0200389 "switch capabilities.\n", mmc_hostname(card->host));
Pierre Ossman17b04292007-07-22 22:18:46 +0200390 return -ENOMEM;
Pierre Ossman1addfcd2007-05-01 14:46:08 +0200391 }
392
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100393 err = mmc_sd_switch(card, 1, 0, 1, status);
Pierre Ossman17b04292007-07-22 22:18:46 +0200394 if (err)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100395 goto out;
396
397 if ((status[16] & 0xF) != 1) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530398 pr_warning("%s: Problem switching card "
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100399 "into high-speed mode!\n",
400 mmc_hostname(card->host));
Michal Miroslaw71578a12010-08-10 18:01:40 -0700401 err = 0;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100402 } else {
Michal Miroslaw71578a12010-08-10 18:01:40 -0700403 err = 1;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100404 }
405
406out:
407 kfree(status);
408
409 return err;
410}
411
Arindam Nathd6d50a12011-05-05 12:18:59 +0530412static int sd_select_driver_type(struct mmc_card *card, u8 *status)
413{
Philip Rakityca8e99b2011-07-06 08:51:32 -0700414 int host_drv_type = SD_DRIVER_TYPE_B;
415 int card_drv_type = SD_DRIVER_TYPE_B;
416 int drive_strength;
Arindam Nathd6d50a12011-05-05 12:18:59 +0530417 int err;
418
419 /*
420 * If the host doesn't support any of the Driver Types A,C or D,
Philip Rakityca8e99b2011-07-06 08:51:32 -0700421 * or there is no board specific handler then default Driver
422 * Type B is used.
Arindam Nathd6d50a12011-05-05 12:18:59 +0530423 */
424 if (!(card->host->caps & (MMC_CAP_DRIVER_TYPE_A | MMC_CAP_DRIVER_TYPE_C
425 | MMC_CAP_DRIVER_TYPE_D)))
426 return 0;
427
Philip Rakityca8e99b2011-07-06 08:51:32 -0700428 if (!card->host->ops->select_drive_strength)
429 return 0;
Arindam Nathd6d50a12011-05-05 12:18:59 +0530430
Philip Rakityca8e99b2011-07-06 08:51:32 -0700431 if (card->host->caps & MMC_CAP_DRIVER_TYPE_A)
432 host_drv_type |= SD_DRIVER_TYPE_A;
433
434 if (card->host->caps & MMC_CAP_DRIVER_TYPE_C)
435 host_drv_type |= SD_DRIVER_TYPE_C;
436
437 if (card->host->caps & MMC_CAP_DRIVER_TYPE_D)
438 host_drv_type |= SD_DRIVER_TYPE_D;
439
440 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A)
441 card_drv_type |= SD_DRIVER_TYPE_A;
442
443 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
444 card_drv_type |= SD_DRIVER_TYPE_C;
445
446 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_D)
447 card_drv_type |= SD_DRIVER_TYPE_D;
448
449 /*
450 * The drive strength that the hardware can support
451 * depends on the board design. Pass the appropriate
452 * information and let the hardware specific code
453 * return what is possible given the options
454 */
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -0500455 mmc_host_clk_hold(card->host);
Philip Rakityca8e99b2011-07-06 08:51:32 -0700456 drive_strength = card->host->ops->select_drive_strength(
457 card->sw_caps.uhs_max_dtr,
458 host_drv_type, card_drv_type);
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -0500459 mmc_host_clk_release(card->host);
Philip Rakityca8e99b2011-07-06 08:51:32 -0700460
461 err = mmc_sd_switch(card, 1, 2, drive_strength, status);
Arindam Nathd6d50a12011-05-05 12:18:59 +0530462 if (err)
463 return err;
464
Philip Rakityca8e99b2011-07-06 08:51:32 -0700465 if ((status[15] & 0xF) != drive_strength) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530466 pr_warning("%s: Problem setting drive strength!\n",
Arindam Nathd6d50a12011-05-05 12:18:59 +0530467 mmc_hostname(card->host));
468 return 0;
469 }
470
Philip Rakityca8e99b2011-07-06 08:51:32 -0700471 mmc_set_driver_type(card->host, drive_strength);
Arindam Nathd6d50a12011-05-05 12:18:59 +0530472
473 return 0;
474}
475
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530476static void sd_update_bus_speed_mode(struct mmc_card *card)
Arindam Nath49c468f2011-05-05 12:19:01 +0530477{
Arindam Nath49c468f2011-05-05 12:19:01 +0530478 /*
479 * If the host doesn't support any of the UHS-I modes, fallback on
480 * default speed.
481 */
482 if (!(card->host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530483 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50))) {
484 card->sd_bus_speed = 0;
485 return;
486 }
Arindam Nath49c468f2011-05-05 12:19:01 +0530487
488 if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
489 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530490 card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
Arindam Nath49c468f2011-05-05 12:19:01 +0530491 } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
492 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530493 card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
Arindam Nath49c468f2011-05-05 12:19:01 +0530494 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
495 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
496 SD_MODE_UHS_SDR50)) {
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530497 card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
Arindam Nath49c468f2011-05-05 12:19:01 +0530498 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
499 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
500 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530501 card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
Arindam Nath49c468f2011-05-05 12:19:01 +0530502 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
503 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
504 MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
505 SD_MODE_UHS_SDR12)) {
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530506 card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
507 }
508}
509
510static int sd_set_bus_speed_mode(struct mmc_card *card, u8 *status)
511{
512 int err;
513 unsigned int timing = 0;
514
515 switch (card->sd_bus_speed) {
516 case UHS_SDR104_BUS_SPEED:
517 timing = MMC_TIMING_UHS_SDR104;
518 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
519 break;
520 case UHS_DDR50_BUS_SPEED:
521 timing = MMC_TIMING_UHS_DDR50;
522 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
523 break;
524 case UHS_SDR50_BUS_SPEED:
525 timing = MMC_TIMING_UHS_SDR50;
526 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
527 break;
528 case UHS_SDR25_BUS_SPEED:
529 timing = MMC_TIMING_UHS_SDR25;
530 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
531 break;
532 case UHS_SDR12_BUS_SPEED:
533 timing = MMC_TIMING_UHS_SDR12;
534 card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
535 break;
536 default:
537 return 0;
Arindam Nath49c468f2011-05-05 12:19:01 +0530538 }
539
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530540 err = mmc_sd_switch(card, 1, 0, card->sd_bus_speed, status);
Arindam Nath49c468f2011-05-05 12:19:01 +0530541 if (err)
542 return err;
543
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530544 if ((status[16] & 0xF) != card->sd_bus_speed)
Girish K Sa3c76eb2011-10-11 11:44:09 +0530545 pr_warning("%s: Problem setting bus speed mode!\n",
Arindam Nath49c468f2011-05-05 12:19:01 +0530546 mmc_hostname(card->host));
547 else {
548 mmc_set_timing(card->host, timing);
549 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
550 }
551
552 return 0;
553}
554
Arindam Nath5371c922011-05-05 12:19:02 +0530555static int sd_set_current_limit(struct mmc_card *card, u8 *status)
556{
557 int current_limit = 0;
558 int err;
559
560 /*
561 * Current limit switch is only defined for SDR50, SDR104, and DDR50
562 * bus speed modes. For other bus speed modes, we set the default
563 * current limit of 200mA.
564 */
565 if ((card->sd_bus_speed == UHS_SDR50_BUS_SPEED) ||
566 (card->sd_bus_speed == UHS_SDR104_BUS_SPEED) ||
567 (card->sd_bus_speed == UHS_DDR50_BUS_SPEED)) {
568 if (card->host->caps & MMC_CAP_MAX_CURRENT_800) {
569 if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_800)
570 current_limit = SD_SET_CURRENT_LIMIT_800;
571 else if (card->sw_caps.sd3_curr_limit &
572 SD_MAX_CURRENT_600)
573 current_limit = SD_SET_CURRENT_LIMIT_600;
574 else if (card->sw_caps.sd3_curr_limit &
575 SD_MAX_CURRENT_400)
576 current_limit = SD_SET_CURRENT_LIMIT_400;
577 else if (card->sw_caps.sd3_curr_limit &
578 SD_MAX_CURRENT_200)
579 current_limit = SD_SET_CURRENT_LIMIT_200;
580 } else if (card->host->caps & MMC_CAP_MAX_CURRENT_600) {
581 if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_600)
582 current_limit = SD_SET_CURRENT_LIMIT_600;
583 else if (card->sw_caps.sd3_curr_limit &
584 SD_MAX_CURRENT_400)
585 current_limit = SD_SET_CURRENT_LIMIT_400;
586 else if (card->sw_caps.sd3_curr_limit &
587 SD_MAX_CURRENT_200)
588 current_limit = SD_SET_CURRENT_LIMIT_200;
589 } else if (card->host->caps & MMC_CAP_MAX_CURRENT_400) {
590 if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_400)
591 current_limit = SD_SET_CURRENT_LIMIT_400;
592 else if (card->sw_caps.sd3_curr_limit &
593 SD_MAX_CURRENT_200)
594 current_limit = SD_SET_CURRENT_LIMIT_200;
595 } else if (card->host->caps & MMC_CAP_MAX_CURRENT_200) {
596 if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_200)
597 current_limit = SD_SET_CURRENT_LIMIT_200;
598 }
599 } else
600 current_limit = SD_SET_CURRENT_LIMIT_200;
601
602 err = mmc_sd_switch(card, 1, 3, current_limit, status);
603 if (err)
604 return err;
605
606 if (((status[15] >> 4) & 0x0F) != current_limit)
Girish K Sa3c76eb2011-10-11 11:44:09 +0530607 pr_warning("%s: Problem setting current limit!\n",
Arindam Nath5371c922011-05-05 12:19:02 +0530608 mmc_hostname(card->host));
609
610 return 0;
611}
612
Sujit Reddy Thumma1cd61b72012-08-06 11:23:21 +0530613/**
614 * mmc_sd_change_bus_speed() - Change SD card bus frequency at runtime
615 * @host: pointer to mmc host structure
616 * @freq: pointer to desired frequency to be set
617 *
618 * Change the SD card bus frequency at runtime after the card is
619 * initialized. Callers are expected to make sure of the card's
620 * state (DATA/RCV/TRANSFER) beforing changing the frequency at runtime.
621 *
622 * If the frequency to change is greater than max. supported by card,
623 * *freq is changed to max. supported by card and if it is less than min.
624 * supported by host, *freq is changed to min. supported by host.
625 */
626static int mmc_sd_change_bus_speed(struct mmc_host *host, unsigned long *freq)
627{
628 int err = 0;
629 struct mmc_card *card;
630
631 mmc_claim_host(host);
632 /*
633 * Assign card pointer after claiming host to avoid race
634 * conditions that may arise during removal of the card.
635 */
636 card = host->card;
637
638 /* sanity checks */
639 if (!card || !freq) {
640 err = -EINVAL;
641 goto out;
642 }
643
644 if (mmc_card_uhs(card)) {
645 if (*freq > card->sw_caps.uhs_max_dtr)
646 *freq = card->sw_caps.uhs_max_dtr;
647 } else {
648 if (*freq > mmc_sd_get_max_clock(card))
649 *freq = mmc_sd_get_max_clock(card);
650 }
651
652 if (*freq < host->f_min)
653 *freq = host->f_min;
654
655 mmc_set_clock(host, (unsigned int) (*freq));
656
657 if (!mmc_host_is_spi(card->host) && mmc_sd_card_uhs(card)
658 && card->host->ops->execute_tuning) {
659 /*
660 * We try to probe host driver for tuning for any
661 * frequency, it is host driver responsibility to
662 * perform actual tuning only when required.
663 */
664 mmc_host_clk_hold(card->host);
665 err = card->host->ops->execute_tuning(card->host,
666 MMC_SEND_TUNING_BLOCK);
667 mmc_host_clk_release(card->host);
668
Sahitya Tummala64f92e12013-06-13 10:34:48 +0530669 if (err) {
670 pr_warn("%s: %s: tuning execution failed %d. Restoring to previous clock %lu\n",
671 mmc_hostname(card->host), __func__, err,
672 host->clk_scaling.curr_freq);
673 mmc_set_clock(host, host->clk_scaling.curr_freq);
674 }
Sujit Reddy Thumma1cd61b72012-08-06 11:23:21 +0530675 }
676
677out:
678 mmc_release_host(host);
679 return err;
680}
681
Arindam Nathd6d50a12011-05-05 12:18:59 +0530682/*
683 * UHS-I specific initialization procedure
684 */
685static int mmc_sd_init_uhs_card(struct mmc_card *card)
686{
687 int err;
688 u8 *status;
689
690 if (!card->scr.sda_spec3)
691 return 0;
692
693 if (!(card->csd.cmdclass & CCC_SWITCH))
694 return 0;
695
696 status = kmalloc(64, GFP_KERNEL);
697 if (!status) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530698 pr_err("%s: could not allocate a buffer for "
Arindam Nathd6d50a12011-05-05 12:18:59 +0530699 "switch capabilities.\n", mmc_hostname(card->host));
700 return -ENOMEM;
701 }
702
703 /* Set 4-bit bus width */
704 if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
705 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
706 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
707 if (err)
708 goto out;
709
710 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
711 }
712
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530713 /*
714 * Select the bus speed mode depending on host
715 * and card capability.
716 */
717 sd_update_bus_speed_mode(card);
718
Arindam Nathd6d50a12011-05-05 12:18:59 +0530719 /* Set the driver strength for the card */
720 err = sd_select_driver_type(card, status);
Arindam Nath49c468f2011-05-05 12:19:01 +0530721 if (err)
722 goto out;
723
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530724 /* Set current limit for the card */
725 err = sd_set_current_limit(card, status);
Arindam Nath5371c922011-05-05 12:19:02 +0530726 if (err)
727 goto out;
728
Subhash Jadavani93c712f2011-08-09 12:19:31 +0530729 /* Set bus speed mode of the card */
730 err = sd_set_bus_speed_mode(card, status);
Arindam Nathb513ea22011-05-05 12:19:04 +0530731 if (err)
732 goto out;
733
734 /* SPI mode doesn't define CMD19 */
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -0500735 if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning) {
736 mmc_host_clk_hold(card->host);
Girish K Sa4924c72012-01-11 14:04:52 -0500737 err = card->host->ops->execute_tuning(card->host,
738 MMC_SEND_TUNING_BLOCK);
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -0500739 mmc_host_clk_release(card->host);
740 }
Arindam Nathd6d50a12011-05-05 12:18:59 +0530741
742out:
743 kfree(status);
744
745 return err;
746}
747
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100748MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
749 card->raw_cid[2], card->raw_cid[3]);
750MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
751 card->raw_csd[2], card->raw_csd[3]);
752MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
753MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700754MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
755MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100756MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
757MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
758MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
759MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
760MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
761MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
762
763
764static struct attribute *sd_std_attrs[] = {
765 &dev_attr_cid.attr,
766 &dev_attr_csd.attr,
767 &dev_attr_scr.attr,
768 &dev_attr_date.attr,
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700769 &dev_attr_erase_size.attr,
770 &dev_attr_preferred_erase_size.attr,
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100771 &dev_attr_fwrev.attr,
772 &dev_attr_hwrev.attr,
773 &dev_attr_manfid.attr,
774 &dev_attr_name.attr,
775 &dev_attr_oemid.attr,
776 &dev_attr_serial.attr,
777 NULL,
778};
779
780static struct attribute_group sd_std_attr_group = {
781 .attrs = sd_std_attrs,
782};
783
David Brownella4dbd672009-06-24 10:06:31 -0700784static const struct attribute_group *sd_attr_groups[] = {
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100785 &sd_std_attr_group,
786 NULL,
787};
788
Michal Miroslaw71578a12010-08-10 18:01:40 -0700789struct device_type sd_type = {
Pierre Ossman51ec92e2008-03-21 23:54:50 +0100790 .groups = sd_attr_groups,
791};
792
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100793/*
Michal Miroslaw71578a12010-08-10 18:01:40 -0700794 * Fetch CID from card.
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200795 */
Arindam Nathd6d50a12011-05-05 12:18:59 +0530796int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid, u32 *rocr)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200797{
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200798 int err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200799
800 /*
801 * Since we're changing the OCR value, we seem to
802 * need to tell some cards to go back to the idle
803 * state. We wait 1ms to give cards time to
804 * respond.
805 */
806 mmc_go_idle(host);
807
808 /*
809 * If SD_SEND_IF_COND indicates an SD 2.0
810 * compliant card and we should set bit 30
811 * of the ocr to indicate that we can handle
812 * block-addressed SDHC cards.
813 */
814 err = mmc_send_if_cond(host, ocr);
Pierre Ossman17b04292007-07-22 22:18:46 +0200815 if (!err)
Arindam Nathf2119df2011-05-05 12:18:57 +0530816 ocr |= SD_OCR_CCS;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200817
Arindam Nathf2119df2011-05-05 12:18:57 +0530818 /*
819 * If the host supports one of UHS-I modes, request the card
820 * to switch to 1.8V signaling level.
821 */
822 if (host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
823 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50))
824 ocr |= SD_OCR_S18R;
825
826 /* If the host can supply more than 150mA, XPC should be set to 1. */
827 if (host->caps & (MMC_CAP_SET_XPC_330 | MMC_CAP_SET_XPC_300 |
828 MMC_CAP_SET_XPC_180))
829 ocr |= SD_OCR_XPC;
830
831try_again:
Arindam Nathd6d50a12011-05-05 12:18:59 +0530832 err = mmc_send_app_op_cond(host, ocr, rocr);
Pierre Ossman17b04292007-07-22 22:18:46 +0200833 if (err)
Michal Miroslaw71578a12010-08-10 18:01:40 -0700834 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200835
Arindam Nathf2119df2011-05-05 12:18:57 +0530836 /*
837 * In case CCS and S18A in the response is set, start Signal Voltage
838 * Switch procedure. SPI mode doesn't support CMD11.
839 */
Arindam Nathd6d50a12011-05-05 12:18:59 +0530840 if (!mmc_host_is_spi(host) && rocr &&
841 ((*rocr & 0x41000000) == 0x41000000)) {
Philip Rakity261bbd42011-05-13 11:17:17 +0530842 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180, true);
Arindam Nathf2119df2011-05-05 12:18:57 +0530843 if (err) {
844 ocr &= ~SD_OCR_S18R;
845 goto try_again;
846 }
847 }
848
David Brownellaf517152007-08-08 09:11:32 -0700849 if (mmc_host_is_spi(host))
850 err = mmc_send_cid(host, cid);
851 else
852 err = mmc_all_send_cid(host, cid);
Michal Miroslaw71578a12010-08-10 18:01:40 -0700853
854 return err;
855}
856
857int mmc_sd_get_csd(struct mmc_host *host, struct mmc_card *card)
858{
859 int err;
860
861 /*
862 * Fetch CSD from card.
863 */
864 err = mmc_send_csd(card, card->raw_csd);
Pierre Ossman17b04292007-07-22 22:18:46 +0200865 if (err)
Michal Miroslaw71578a12010-08-10 18:01:40 -0700866 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200867
Michal Miroslaw71578a12010-08-10 18:01:40 -0700868 err = mmc_decode_csd(card);
869 if (err)
870 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200871
Michal Miroslaw71578a12010-08-10 18:01:40 -0700872 return 0;
873}
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200874
Michal Miroslaw71578a12010-08-10 18:01:40 -0700875int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
876 bool reinit)
877{
878 int err;
San Mehat8ed5a662008-12-04 11:18:00 -0800879#ifdef CONFIG_MMC_PARANOID_SD_INIT
880 int retries;
881#endif
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200882
Michal Miroslaw71578a12010-08-10 18:01:40 -0700883 if (!reinit) {
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200884 /*
885 * Fetch SCR from card.
886 */
887 err = mmc_app_send_scr(card, card->raw_scr);
Pierre Ossman17b04292007-07-22 22:18:46 +0200888 if (err)
Michal Miroslaw71578a12010-08-10 18:01:40 -0700889 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200890
Pierre Ossmanbd766312007-05-01 16:11:57 +0200891 err = mmc_decode_scr(card);
Michal Miroslaw71578a12010-08-10 18:01:40 -0700892 if (err)
893 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200894
895 /*
Adrian Hunterdfe86cb2010-08-11 14:17:46 -0700896 * Fetch and process SD Status register.
897 */
898 err = mmc_read_ssr(card);
899 if (err)
900 return err;
901
902 /* Erase init depends on CSD and SSR */
903 mmc_init_erase(card);
904
905 /*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200906 * Fetch switch information from card.
907 */
San Mehatc80f5462008-11-11 09:35:36 -0800908#ifdef CONFIG_MMC_PARANOID_SD_INIT
909 for (retries = 1; retries <= 3; retries++) {
910 err = mmc_read_switch(card);
911 if (!err) {
912 if (retries > 1) {
913 printk(KERN_WARNING
914 "%s: recovered\n",
915 mmc_hostname(host));
916 }
917 break;
918 } else {
919 printk(KERN_WARNING
920 "%s: read switch failed (attempt %d)\n",
921 mmc_hostname(host), retries);
922 }
923 }
924#else
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200925 err = mmc_read_switch(card);
San Mehatc80f5462008-11-11 09:35:36 -0800926#endif
927
Pierre Ossman17b04292007-07-22 22:18:46 +0200928 if (err)
Michal Miroslaw71578a12010-08-10 18:01:40 -0700929 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200930 }
931
932 /*
Wolfgang Muees9d9f25c2009-04-07 14:48:16 +0100933 * For SPI, enable CRC as appropriate.
934 * This CRC enable is located AFTER the reading of the
935 * card registers because some SDHC cards are not able
936 * to provide valid CRCs for non-512-byte blocks.
937 */
938 if (mmc_host_is_spi(host)) {
939 err = mmc_spi_set_crc(host, use_spi_crc);
940 if (err)
Michal Miroslaw71578a12010-08-10 18:01:40 -0700941 return err;
Wolfgang Muees9d9f25c2009-04-07 14:48:16 +0100942 }
943
944 /*
Michal Miroslaw71578a12010-08-10 18:01:40 -0700945 * Check if read-only switch is active.
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200946 */
Michal Miroslaw71578a12010-08-10 18:01:40 -0700947 if (!reinit) {
948 int ro = -1;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200949
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -0500950 if (host->ops->get_ro) {
Stephen Boyd3f113aa2013-02-11 14:30:14 -0800951 mmc_host_clk_hold(card->host);
Michal Miroslaw71578a12010-08-10 18:01:40 -0700952 ro = host->ops->get_ro(host);
Stephen Boyd3f113aa2013-02-11 14:30:14 -0800953 mmc_host_clk_release(card->host);
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -0500954 }
Michal Miroslaw71578a12010-08-10 18:01:40 -0700955
956 if (ro < 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530957 pr_warning("%s: host does not "
Michal Miroslaw71578a12010-08-10 18:01:40 -0700958 "support reading read-only "
959 "switch. assuming write-enable.\n",
960 mmc_hostname(host));
961 } else if (ro > 0) {
962 mmc_card_set_readonly(card);
963 }
964 }
965
966 return 0;
967}
968
969unsigned mmc_sd_get_max_clock(struct mmc_card *card)
970{
971 unsigned max_dtr = (unsigned int)-1;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200972
973 if (mmc_card_highspeed(card)) {
974 if (max_dtr > card->sw_caps.hs_max_dtr)
975 max_dtr = card->sw_caps.hs_max_dtr;
976 } else if (max_dtr > card->csd.max_dtr) {
977 max_dtr = card->csd.max_dtr;
978 }
979
Michal Miroslaw71578a12010-08-10 18:01:40 -0700980 return max_dtr;
981}
982
983void mmc_sd_go_highspeed(struct mmc_card *card)
984{
985 mmc_card_set_highspeed(card);
986 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
987}
988
989/*
990 * Handle the detection and initialisation of a card.
991 *
992 * In the case of a resume, "oldcard" will contain the card
993 * we're trying to reinitialise.
994 */
995static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
996 struct mmc_card *oldcard)
997{
998 struct mmc_card *card;
999 int err;
1000 u32 cid[4];
Arindam Nathd6d50a12011-05-05 12:18:59 +05301001 u32 rocr = 0;
Michal Miroslaw71578a12010-08-10 18:01:40 -07001002
1003 BUG_ON(!host);
1004 WARN_ON(!host->claimed);
1005
Ulf Hanssone7747472012-03-01 13:18:05 +01001006 /* The initialization should be done at 3.3 V I/O voltage. */
1007 mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, 0);
1008
Arindam Nathd6d50a12011-05-05 12:18:59 +05301009 err = mmc_sd_get_cid(host, ocr, cid, &rocr);
Michal Miroslaw71578a12010-08-10 18:01:40 -07001010 if (err)
1011 return err;
1012
1013 if (oldcard) {
1014 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
1015 return -ENOENT;
1016
1017 card = oldcard;
1018 } else {
1019 /*
1020 * Allocate card structure.
1021 */
1022 card = mmc_alloc_card(host, &sd_type);
1023 if (IS_ERR(card))
1024 return PTR_ERR(card);
1025
1026 card->type = MMC_TYPE_SD;
1027 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
1028 }
1029
1030 /*
1031 * For native busses: get card RCA and quit open drain mode.
1032 */
1033 if (!mmc_host_is_spi(host)) {
1034 err = mmc_send_relative_addr(host, &card->rca);
1035 if (err)
1036 return err;
Michal Miroslaw71578a12010-08-10 18:01:40 -07001037 }
1038
1039 if (!oldcard) {
1040 err = mmc_sd_get_csd(host, card);
1041 if (err)
1042 return err;
1043
1044 mmc_decode_cid(card);
1045 }
1046
1047 /*
1048 * Select card, as all following commands rely on that.
1049 */
1050 if (!mmc_host_is_spi(host)) {
1051 err = mmc_select_card(card);
1052 if (err)
1053 return err;
1054 }
1055
1056 err = mmc_sd_setup_card(host, card, oldcard != NULL);
1057 if (err)
1058 goto free_card;
1059
Arindam Nathd6d50a12011-05-05 12:18:59 +05301060 /* Initialization sequence for UHS-I cards */
1061 if (rocr & SD_ROCR_S18A) {
1062 err = mmc_sd_init_uhs_card(card);
Pierre Ossman17b04292007-07-22 22:18:46 +02001063 if (err)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001064 goto free_card;
Arindam Nath3a303512011-05-05 12:19:03 +05301065
1066 /* Card is an ultra-high-speed card */
Philip Rakitya303c532011-11-14 19:14:38 -08001067 mmc_card_set_uhs(card);
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301068
1069 /*
1070 * Since initialization is now complete, enable preset
1071 * value registers for UHS-I cards.
1072 */
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -05001073 if (host->ops->enable_preset_value) {
Stephen Boyd3f113aa2013-02-11 14:30:14 -08001074 mmc_host_clk_hold(card->host);
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301075 host->ops->enable_preset_value(host, true);
Stephen Boyd3f113aa2013-02-11 14:30:14 -08001076 mmc_host_clk_release(card->host);
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -05001077 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05301078 } else {
1079 /*
1080 * Attempt to change to high-speed (if supported)
1081 */
1082 err = mmc_sd_switch_hs(card);
1083 if (err > 0)
1084 mmc_sd_go_highspeed(card);
1085 else if (err)
1086 goto free_card;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001087
Arindam Nathd6d50a12011-05-05 12:18:59 +05301088 /*
1089 * Set bus speed.
1090 */
1091 mmc_set_clock(host, mmc_sd_get_max_clock(card));
1092
1093 /*
1094 * Switch to wider bus (if supported).
1095 */
1096 if ((host->caps & MMC_CAP_4_BIT_DATA) &&
1097 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
1098 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
1099 if (err)
1100 goto free_card;
1101
1102 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
1103 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001104 }
1105
Michal Miroslaw71578a12010-08-10 18:01:40 -07001106 host->card = card;
Pierre Ossman17b04292007-07-22 22:18:46 +02001107 return 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001108
1109free_card:
1110 if (!oldcard)
1111 mmc_remove_card(card);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001112
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001113 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001114}
1115
1116/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001117 * Host is being removed. Free up the current card.
1118 */
1119static void mmc_sd_remove(struct mmc_host *host)
1120{
1121 BUG_ON(!host);
1122 BUG_ON(!host->card);
1123
1124 mmc_remove_card(host->card);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001125
1126 mmc_claim_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001127 host->card = NULL;
Sujit Reddy Thumma51d57b32012-10-11 17:17:03 +05301128 mmc_exit_clk_scaling(host);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001129 mmc_release_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001130}
1131
1132/*
Adrian Hunterd3049502011-11-28 16:22:00 +02001133 * Card detection - card is alive.
1134 */
1135static int mmc_sd_alive(struct mmc_host *host)
1136{
1137 return mmc_send_status(host->card, NULL);
1138}
1139
1140/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001141 * Card detection callback from host.
1142 */
1143static void mmc_sd_detect(struct mmc_host *host)
1144{
San Mehat8ed5a662008-12-04 11:18:00 -08001145 int err = 0;
1146#ifdef CONFIG_MMC_PARANOID_SD_INIT
1147 int retries = 5;
1148#endif
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001149
1150 BUG_ON(!host);
1151 BUG_ON(!host->card);
Asutosh Dasbbc84782013-02-11 15:31:35 +05301152
1153 mmc_rpm_hold(host, &host->card->dev);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001154 mmc_claim_host(host);
1155
1156 /*
1157 * Just check if our card has been removed.
1158 */
San Mehat8ed5a662008-12-04 11:18:00 -08001159#ifdef CONFIG_MMC_PARANOID_SD_INIT
1160 while(retries) {
1161 err = mmc_send_status(host->card, NULL);
1162 if (err) {
1163 retries--;
1164 udelay(5);
1165 continue;
1166 }
1167 break;
1168 }
1169 if (!retries) {
1170 printk(KERN_ERR "%s(%s): Unable to re-detect card (%d)\n",
1171 __func__, mmc_hostname(host), err);
Asutosh Dasccf601d2013-03-26 17:21:32 +05301172 err = _mmc_detect_card_removed(host);
San Mehat8ed5a662008-12-04 11:18:00 -08001173 }
1174#else
Adrian Hunterd3049502011-11-28 16:22:00 +02001175 err = _mmc_detect_card_removed(host);
San Mehat8ed5a662008-12-04 11:18:00 -08001176#endif
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001177 mmc_release_host(host);
1178
Asutosh Dasbbc84782013-02-11 15:31:35 +05301179 /*
1180 * if detect fails, the device would be removed anyway;
1181 * the rpm framework would mark the device state suspended.
1182 */
1183 if (!err)
1184 mmc_rpm_release(host, &host->card->dev);
1185
Pierre Ossman17b04292007-07-22 22:18:46 +02001186 if (err) {
Pierre Ossman4101c162007-05-19 13:39:01 +02001187 mmc_sd_remove(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001188
1189 mmc_claim_host(host);
1190 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -04001191 mmc_power_off(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001192 mmc_release_host(host);
1193 }
1194}
1195
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001196/*
1197 * Suspend callback from host.
1198 */
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001199static int mmc_sd_suspend(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001200{
1201 BUG_ON(!host);
1202 BUG_ON(!host->card);
1203
Sujit Reddy Thumma51d57b32012-10-11 17:17:03 +05301204 /*
1205 * Disable clock scaling before suspend and enable it after resume so
1206 * as to avoid clock scaling decisions kicking in during this window.
1207 */
1208 mmc_disable_clk_scaling(host);
1209
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001210 mmc_claim_host(host);
David Brownellaf517152007-08-08 09:11:32 -07001211 if (!mmc_host_is_spi(host))
1212 mmc_deselect_cards(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001213 host->card->state &= ~MMC_STATE_HIGHSPEED;
1214 mmc_release_host(host);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001215
1216 return 0;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001217}
1218
1219/*
1220 * Resume callback from host.
1221 *
1222 * This function tries to determine if the same card is still present
1223 * and, if so, restore all state to it.
1224 */
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001225static int mmc_sd_resume(struct mmc_host *host)
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001226{
1227 int err;
San Mehat05063bf2008-12-01 08:52:34 -08001228#ifdef CONFIG_MMC_PARANOID_SD_INIT
1229 int retries;
1230#endif
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001231
1232 BUG_ON(!host);
1233 BUG_ON(!host->card);
1234
1235 mmc_claim_host(host);
San Mehat05063bf2008-12-01 08:52:34 -08001236#ifdef CONFIG_MMC_PARANOID_SD_INIT
1237 retries = 5;
1238 while (retries) {
1239 err = mmc_sd_init_card(host, host->ocr, host->card);
1240
1241 if (err) {
1242 printk(KERN_ERR "%s: Re-init card rc = %d (retries = %d)\n",
1243 mmc_hostname(host), err, retries);
San Mehat05063bf2008-12-01 08:52:34 -08001244 retries--;
Subhash Jadavani4dd5c442012-03-13 00:33:00 +05301245 mmc_power_off(host);
1246 usleep_range(5000, 5500);
1247 mmc_power_up(host);
1248 mmc_select_voltage(host, host->ocr);
San Mehat05063bf2008-12-01 08:52:34 -08001249 continue;
1250 }
1251 break;
1252 }
1253#else
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001254 err = mmc_sd_init_card(host, host->ocr, host->card);
San Mehat05063bf2008-12-01 08:52:34 -08001255#endif
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001256 mmc_release_host(host);
1257
Sujit Reddy Thumma51d57b32012-10-11 17:17:03 +05301258 /*
1259 * We have done full initialization of the card,
1260 * reset the clk scale stats and current frequency.
1261 */
1262 if (mmc_can_scale_clk(host))
1263 mmc_init_clk_scaling(host);
1264
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001265 return err;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001266}
1267
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001268static int mmc_sd_power_restore(struct mmc_host *host)
Adrian Huntereae1aee2009-09-22 16:44:33 -07001269{
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001270 int ret;
1271
Sujit Reddy Thumma51d57b32012-10-11 17:17:03 +05301272 /* Disable clk scaling to avoid switching frequencies intermittently */
1273 mmc_disable_clk_scaling(host);
1274
Adrian Huntereae1aee2009-09-22 16:44:33 -07001275 host->card->state &= ~MMC_STATE_HIGHSPEED;
1276 mmc_claim_host(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001277 ret = mmc_sd_init_card(host, host->ocr, host->card);
Adrian Huntereae1aee2009-09-22 16:44:33 -07001278 mmc_release_host(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001279
Sujit Reddy Thumma51d57b32012-10-11 17:17:03 +05301280 if (mmc_can_scale_clk(host))
1281 mmc_init_clk_scaling(host);
1282
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001283 return ret;
Adrian Huntereae1aee2009-09-22 16:44:33 -07001284}
1285
Adrian Hunter9feae242009-09-22 16:44:32 -07001286static const struct mmc_bus_ops mmc_sd_ops = {
1287 .remove = mmc_sd_remove,
1288 .detect = mmc_sd_detect,
1289 .suspend = NULL,
1290 .resume = NULL,
Adrian Huntereae1aee2009-09-22 16:44:33 -07001291 .power_restore = mmc_sd_power_restore,
Adrian Hunterd3049502011-11-28 16:22:00 +02001292 .alive = mmc_sd_alive,
Sujit Reddy Thumma1cd61b72012-08-06 11:23:21 +05301293 .change_bus_speed = mmc_sd_change_bus_speed,
Adrian Hunter9feae242009-09-22 16:44:32 -07001294};
1295
1296static const struct mmc_bus_ops mmc_sd_ops_unsafe = {
1297 .remove = mmc_sd_remove,
1298 .detect = mmc_sd_detect,
1299 .suspend = mmc_sd_suspend,
1300 .resume = mmc_sd_resume,
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 void mmc_sd_attach_bus_ops(struct mmc_host *host)
1307{
1308 const struct mmc_bus_ops *bus_ops;
1309
Matt Fleming71d7d3d2010-09-27 09:42:19 +01001310 if (!mmc_card_is_removable(host))
Adrian Hunter9feae242009-09-22 16:44:32 -07001311 bus_ops = &mmc_sd_ops_unsafe;
1312 else
1313 bus_ops = &mmc_sd_ops;
1314 mmc_attach_bus(host, bus_ops);
1315}
1316
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001317/*
1318 * Starting point for SD card init.
1319 */
Andy Ross807e8e42011-01-03 10:36:56 -08001320int mmc_attach_sd(struct mmc_host *host)
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001321{
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001322 int err;
Andy Ross807e8e42011-01-03 10:36:56 -08001323 u32 ocr;
San Mehat8ed5a662008-12-04 11:18:00 -08001324#ifdef CONFIG_MMC_PARANOID_SD_INIT
1325 int retries;
1326#endif
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001327
1328 BUG_ON(!host);
Pierre Ossmand84075c82007-08-09 13:23:56 +02001329 WARN_ON(!host->claimed);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001330
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301331 /* Disable preset value enable if already set since last time */
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -05001332 if (host->ops->enable_preset_value) {
1333 mmc_host_clk_hold(host);
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301334 host->ops->enable_preset_value(host, false);
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -05001335 mmc_host_clk_release(host);
1336 }
Arindam Nath4d55c5a2011-05-05 12:19:05 +05301337
Andy Ross807e8e42011-01-03 10:36:56 -08001338 err = mmc_send_app_op_cond(host, 0, &ocr);
1339 if (err)
1340 return err;
1341
Adrian Hunter9feae242009-09-22 16:44:32 -07001342 mmc_sd_attach_bus_ops(host);
Takashi Iwai8f230f42010-12-08 10:04:30 +01001343 if (host->ocr_avail_sd)
1344 host->ocr_avail = host->ocr_avail_sd;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001345
Philip Langdale55556da2007-03-16 19:39:00 -07001346 /*
David Brownellaf517152007-08-08 09:11:32 -07001347 * We need to get OCR a different way for SPI.
1348 */
1349 if (mmc_host_is_spi(host)) {
1350 mmc_go_idle(host);
1351
1352 err = mmc_spi_read_ocr(host, 0, &ocr);
1353 if (err)
1354 goto err;
1355 }
1356
1357 /*
Philip Langdale55556da2007-03-16 19:39:00 -07001358 * Sanity check the voltages that the card claims to
1359 * support.
1360 */
1361 if (ocr & 0x7F) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301362 pr_warning("%s: card claims to support voltages "
Philip Langdale55556da2007-03-16 19:39:00 -07001363 "below the defined range. These will be ignored.\n",
1364 mmc_hostname(host));
1365 ocr &= ~0x7F;
1366 }
1367
Takashi Iwai8f230f42010-12-08 10:04:30 +01001368 if ((ocr & MMC_VDD_165_195) &&
1369 !(host->ocr_avail_sd & MMC_VDD_165_195)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301370 pr_warning("%s: SD card claims to support the "
Philip Langdale55556da2007-03-16 19:39:00 -07001371 "incompletely defined 'low voltage range'. This "
1372 "will be ignored.\n", mmc_hostname(host));
1373 ocr &= ~MMC_VDD_165_195;
1374 }
1375
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001376 host->ocr = mmc_select_voltage(host, ocr);
1377
1378 /*
1379 * Can we support the voltage(s) of the card(s)?
1380 */
Pierre Ossman109b5be2007-07-23 00:12:10 +02001381 if (!host->ocr) {
1382 err = -EINVAL;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001383 goto err;
Pierre Ossman109b5be2007-07-23 00:12:10 +02001384 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001385
1386 /*
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001387 * Detect and init the card.
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001388 */
San Mehat8ed5a662008-12-04 11:18:00 -08001389#ifdef CONFIG_MMC_PARANOID_SD_INIT
1390 retries = 5;
1391 while (retries) {
1392 err = mmc_sd_init_card(host, host->ocr, NULL);
1393 if (err) {
1394 retries--;
Subhash Jadavani4dd5c442012-03-13 00:33:00 +05301395 mmc_power_off(host);
1396 usleep_range(5000, 5500);
1397 mmc_power_up(host);
1398 mmc_select_voltage(host, host->ocr);
San Mehat8ed5a662008-12-04 11:18:00 -08001399 continue;
1400 }
1401 break;
1402 }
1403
1404 if (!retries) {
1405 printk(KERN_ERR "%s: mmc_sd_init_card() failure (err = %d)\n",
1406 mmc_hostname(host), err);
1407 goto err;
1408 }
1409#else
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001410 err = mmc_sd_init_card(host, host->ocr, NULL);
Pierre Ossman17b04292007-07-22 22:18:46 +02001411 if (err)
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001412 goto err;
San Mehat8ed5a662008-12-04 11:18:00 -08001413#endif
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001414
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001415 mmc_release_host(host);
Pierre Ossman4101c162007-05-19 13:39:01 +02001416 err = mmc_add_card(host->card);
Andy Ross807e8e42011-01-03 10:36:56 -08001417 mmc_claim_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001418 if (err)
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001419 goto remove_card;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001420
Sujit Reddy Thumma1ed78ab2013-04-25 12:39:40 +05301421 mmc_init_clk_scaling(host);
Sujit Reddy Thumma51d57b32012-10-11 17:17:03 +05301422
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001423 return 0;
1424
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001425remove_card:
Andy Ross807e8e42011-01-03 10:36:56 -08001426 mmc_release_host(host);
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001427 mmc_remove_card(host->card);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001428 host->card = NULL;
Pierre Ossman2986d0b2007-07-22 17:52:06 +02001429 mmc_claim_host(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001430err:
1431 mmc_detach_bus(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001432
Girish K Sa3c76eb2011-10-11 11:44:09 +05301433 pr_err("%s: error %d whilst initialising SD card\n",
Pierre Ossman109b5be2007-07-23 00:12:10 +02001434 mmc_hostname(host), err);
1435
Pierre Ossmanadf66a02007-07-22 23:08:30 +02001436 return err;
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001437}
1438